2. SHRI RAWATPURA SARKAR INSTITUTEOF TECHNOLOGY ,NEWRAIPUR (C.G.)
CSE/8th
/Advance JavaLab/PreparedbyVivekKumarSinha
Experiment No: 01
AIM:A. Program for printing hello word:
B.Program for printing system Date & Time JSP/SERLET:
A.class Hello{
public static void main (String args[])
{
System.out.println("Java Hello World");
}
}
B. <%@ page import="java.io.*,java.util.*, javax.servlet.*" %>
<html>
<head>
<title>Display Current Date & Time</title>
</head>
<body>
<center>
<h1>Display Current Date & Time</h1>
</center>
<%
Date date = new Date();
out.print( "<h2 align="center">" +date.toString()+"</h2>");
%>
</body>
</html>
3. SHRI RAWATPURA SARKAR INSTITUTEOF TECHNOLOGY ,NEWRAIPUR (C.G.)
CSE/8th
/Advance JavaLab/PreparedbyVivekKumarSinha
Experiment No: 02
AIM: Write a server side program for finding factorial of number.
import java.io.*;
class Factorial
{
publicstaticvoid main(String args[])
{
String str;
int no,fact;
fact=1;
try{
BufferedReader obj = new BufferedReader(new
InputStreamReader(System.in));
System.out.print("Enter number whose Factorial is to be found : ");
System.out.flush();
str=obj.readLine();
no=Integer.parseInt(str);
while(no > 0)
{
fact=fact*no;
no=no-1;
}
System.out.println("FACTORIAL of a given number is : "+fact);
}
catch(Exception e)
{}
}
Experiment No: 03
AIM: . Write a Server side program in JSP/SERVLET for performing Addition of two no
accept numbers from client side by using HTML form
4. SHRI RAWATPURA SARKAR INSTITUTEOF TECHNOLOGY ,NEWRAIPUR (C.G.)
CSE/8th
/Advance JavaLab/PreparedbyVivekKumarSinha
import java.util.Scanner;
class AddNumbers
{
public static void main(String args[])
{
int x, y, z;
System.out.println("Enter two integers to calculate their sum ");
Scanner in = new Scanner(System.in);
x = in.nextInt();
y = in.nextInt();
z = x + y;
System.out.println("Sum of entered integers = "+z);
}
}
Experiment No: 04
AIM: Write a Server side program in JSP/SERVLET for calculating the simple interest accept
the necessary parameters from client side by using.
5. SHRI RAWATPURA SARKAR INSTITUTEOF TECHNOLOGY ,NEWRAIPUR (C.G.)
CSE/8th
/Advance JavaLab/PreparedbyVivekKumarSinha
<%--
Document : interest
Created on : 30 Oct, 2012, 10:30:44 PM
Author : NESTED CODE
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
Now we have to create the page "intjsp.jsp" to calculate interest
Code for this "intjsp.jsp" page ::
Document : intjsp
Created on : 30 Oct, 2012, 10:41:10 PM
Author : NESTED CODE
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body><br><br><center><pre><H1>
<%
String ns= request.getParameter("principle");
String ns1= request.getParameter("year");
String ns2= request.getParameter("interest");
int n1=Integer.parseInt(ns);
int n2=Integer.parseInt(ns1);
int n3=Integer.parseInt(ns2);
double si=((n1*n2*n3)/100);
double x;
x=n1+si;
%>
<%
out.println("Principal= "+n1);
out.println(" Years= "+n2);
out.println(" Rate of Interest= "+n3);
out.println("<br> ");
out.println(" Simple Interest= "+si);
7. SHRI RAWATPURA SARKAR INSTITUTEOF TECHNOLOGY ,NEWRAIPUR (C.G.)
CSE/8th
/Advance JavaLab/PreparedbyVivekKumarSinha
AIM: Write a Server side program in JSP/SERVLET for solving Quadratic Equation
accept necessary parameters from HTML form
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Quad Solver</title>
</head>
<body>
<FORM action="GreetingServlet" method="POST">
Quadratic Equation Solver for Equations in the form of
Ax^2 + Bx + C<BR>
<BR>
Enter the value of A: <INPUT type="text" name="A" size="15"><BR>
Enter the value of B: <INPUT type="text" name="B" size="15"><BR>
Enter the value of C: <INPUT type="text" name="C" size="15"><BR>
<br>
<input type="submit" value="Calculate!">
<br>
<BR>
<BR>
9. SHRI RAWATPURA SARKAR INSTITUTEOF TECHNOLOGY ,NEWRAIPUR (C.G.)
CSE/8th
/Advance JavaLab/PreparedbyVivekKumarSinha
{
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String A = request.getParameter("A").toString();
String B = request.getParameter("B").toString();
String C = request.getParameter("C").toString();
double a = Double.parseDouble(A);
double b = Double.parseDouble(B);
double c = Double.parseDouble(C);
double D = QuadraticSolver.getD();
double[] roots = QuadraticSolver.quadratic();
out.println("<html>");
out.println("<head>");
out.println("<title>Quadratic Equation Solver</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Quadratic Equation Solver </h1>");
out.println("<h2>Your Equation is: <br><br>" + A + "x^2 + " + B + "x + " + C+
"<br>");
//QuadraticSolver solver = new QuadraticSolver ();
out.println("</h2>");
//double D = QuadraticSolver.getD();
if (a == 0.0)
out.println("<br>This is not a Quadratic quation ! ");
10. SHRI RAWATPURA SARKAR INSTITUTEOF TECHNOLOGY ,NEWRAIPUR (C.G.)
CSE/8th
/Advance JavaLab/PreparedbyVivekKumarSinha
if (b == 0.0)
out.println("<br>There is no solution ! ");
if (a > 0.0 && b > 0) {
out.println("<br><br>There is only 1 solution -C/B : " );
out.println("<br><br>Your Solution is : - "+ c + " / " + b + " equals" + -c/b );
out.println("<br><br> X1 = "+ roots[0] );
out.println("<br><br> X2 = "+ roots[1] );
if(roots !=null){
out.println("<br><br>Solutions are:<br><br> X1 = " + roots[0] + " or X2 = " +
roots[1]+"<br>");
}
Else
{
out.println("<br><br>There Are No Real Solutions !!");
}
out.println("</h2>");
out.println("</body>");
out.println("</html>");
out.close();
}
}
}
QuadraticSolver.java
11. SHRI RAWATPURA SARKAR INSTITUTEOF TECHNOLOGY ,NEWRAIPUR (C.G.)
CSE/8th
/Advance JavaLab/PreparedbyVivekKumarSinha
package com.mycompany.servlet;
import static java.lang.StrictMath.*;
public class QuadraticSolver {
static double a;
static double b;
static double c;
//double D;
double discriminant = 0.0;
double root1;
double root2;
static double[] quadratic() {
double a = 0;
double b = 0;
double c = 0;
// Return the roots of the quadratic equation
double[] roots = new double[2];
double D = Math.pow(b, 2) - (c*1)*(-c);
if (D<0) {
System.out.println("There are no real roots");
System.out.println(D);
return roots;
}
12. SHRI RAWATPURA SARKAR INSTITUTEOF TECHNOLOGY ,NEWRAIPUR (C.G.)
CSE/8th
/Advance JavaLab/PreparedbyVivekKumarSinha
// Use Viete's formula to avoid loss of significance
//double q = -0.5 * (b + copySign(sqrt(d), b));
/roots[0] = q/a;
//roots[1] = c/q;
//return roots;
//roots[0]=-b/2/a+Math.pow(Math.pow(b,2)-4*a*c,0.5)/2/a;
roots[0] = ((-b + sqrt(D)) / (2 * a));
return roots;
}
static double getD() {
double D=0;
D = b * b - 4.0f * a * c;
return D;
}
public static void main(String[] args) {
//double D=0;
// D = Math.pow(b, 2) - (c*a)*(-c);
double D = QuadraticSolver.getD();
double[] roots = quadratic();
System.out.println(roots[0]);
System.out.println(roots[1]);
}
My Directory structure:
MyFirstServlet
13. SHRI RAWATPURA SARKAR INSTITUTEOF TECHNOLOGY ,NEWRAIPUR (C.G.)
CSE/8th
/Advance JavaLab/PreparedbyVivekKumarSinha
Deployment descriptor:MyFirstServlet
Java Resources :src
Com.mycompany.servlet
GreetingServlet.java
QuadraticSolver.Java
Libraries
Java Script resources
Build
WebContent
META-INF
WEB-INF
Classes
Lib
Web.xml
Index.jsp
Experiment No: 06
AIM: Write a Server side program in JSP/SERVLET for Income Tax Calculation
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
14. SHRI RAWATPURA SARKAR INSTITUTEOF TECHNOLOGY ,NEWRAIPUR (C.G.)
CSE/8th
/Advance JavaLab/PreparedbyVivekKumarSinha
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Calculate Interest JSP</title>
</head>
<body>
<%
String fname = request.getParameter("fname");
String lname = request.getParameter("lname");
String gender = request.getParameter("gender");
String profession = request.getParameter("profession");
String prefix = " ";
if (gender.equals("Male")) { prefix = "Mr."; }
else if (gender.equals("Female")) { prefix = "Ms."; } %>
<FONT COLOR = "Blue">Hello <%=prefix
%> <%=fname%> <%=lname%> who works in a
<%=profession %></FONT>
<% String sincome = request.getParameter("income");
float income = Float.parseFloat(sincome);
out.println("<BR>Your Annual Income is <FONT COLOR = Blue> " +income +
"</FONT>");
float tax;
float diff;
if(income <= 100000)
{
out.println("<BR>You are below the Tax Bracket!!");
}
else if(income >100000 && income <= 200000)
{
out.println("<BR>Your Tax Bracket is between Rs.1,00000 to Rs.2,00000");
out.println("<BR><B><U> Tax Rule: </U></B>10% of income above Rs.1
Lakh");
diff = income - 100000;
tax = (float)0.1*diff;
out.println("<BR>Tax to be paid is <FONT COLOR = Blue> "+tax+
"</FONT>");
}
else if(income >200000 && income <= 300000)
{
out.println("<BR>Your Tax Bracket is between between Rs.1,00000 to
Rs.3,00000");
out.println("<BR><B><U>Tax Rule: </U></B> 10% of income upto Rs.1 Lakh
and 20% of rest of income");
15. SHRI RAWATPURA SARKAR INSTITUTEOF TECHNOLOGY ,NEWRAIPUR (C.G.)
CSE/8th
/Advance JavaLab/PreparedbyVivekKumarSinha
diff = income - 200000;
tax = (float)0.2*diff + (float)0.1*100000;
out.println("<BR>Tax to be paid is <FONT COLOR = Blue> "+tax+
"</FONT>");
}
else if(income >100000 && income <= 400000)
{
out.println("<BR>Your Tax Bracket is between Rs.1,00000 to Rs.4,00000");
out.println("<BR><B><U>Tax Rule: </U></B>10% of income upto Rs.1 Lakh
20% of income upto Rs.3 Lakh and 30% of rest of income");
diff = income - 300000;
tax = (float)0.3*diff + (float)0.2*200000 + (float)0.1*100000;
out.println("<BR>Tax to be paid is <FONT COLOR = Blue> "+tax);
}
else if(income > 400000)
{
out.println("<BR>You fall in the tax bracket greater than Rs.4,00000");
diff = income - 400000;
tax = diff + (float)0.3*300000 + (float)0.2*200000 + (float)0.1*100000;
out.println("<BR><B><U>Tax Rule:</U></B> 10% of income upto Rs.1 Lakh
20% of income upto 3Lakh, 30% of income upto Rs.4 lakh and 100% of rest of
income");
out.println("<BR>Tax to be paid is <FONT COLOR = Blue> "+tax+
"</FONT>");
}//end if %>
</body>
</html>
Experiment No: 07
16. SHRI RAWATPURA SARKAR INSTITUTEOF TECHNOLOGY ,NEWRAIPUR (C.G.)
CSE/8th
/Advance JavaLab/PreparedbyVivekKumarSinha
AIM: Program:Write a server side JSP/SERVLET program for checking prime number,
accept number from html file handover the no to JSP/Servlet file process it and return the
result.
<%--
Document : index
Created on : 12 Oct, 2012, 7:35:11 PM
Author : nested code team
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body><br><br><center>
<form action="primejsp.jsp ">
<h1>Enter the no :: <input type=text name=n ><br><br>
<input type=submit value="Submit"></h1>
</form></center>
</body>
</html>
Now we have to create the page "primejsp.jsp" to calculate interest
Code for this "intjsp.jsp" page ::
<%--
Document : primejsp
Created on : 31 Oct, 2012, 11:59:34 AM
Author :NESTED CODE
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body><center><h1>The required Result is:: </h1>
17. SHRI RAWATPURA SARKAR INSTITUTEOF TECHNOLOGY ,NEWRAIPUR (C.G.)
CSE/8th
/Advance JavaLab/PreparedbyVivekKumarSinha
<h2>
<% int n,i,flag=0;
String ns= request.getParameter("n");
n=Integer.parseInt(ns);
if(n>1)
{
for(i=2;i<=n/2;i++)
{
if(n%i==0)
{
flag=1;
break;
}
}
}
if(flag==0)
{
out.println("<pre>");
out.println(n+" is a prime no.");
out.println("</pre>");
}
else
{
out.println("<pre>");
out.println(n+" is not a prime no.");
out.println("</pre>");
}
%>
</h2></center>
</body>
</html>
The Outpur will be like ::
18. SHRI RAWATPURA SARKAR INSTITUTEOF TECHNOLOGY ,NEWRAIPUR (C.G.)
CSE/8th
/Advance JavaLab/PreparedbyVivekKumarSinha
Experiment No: 08
AIM: WAP for swapping of two numbers.
package swap;
import java.util.*;
public class Main
{
public static void main(String[] args)
{
int a,b;
System.out.println("Enter the two numbers: ");
Scanner s= new Scanner(System.in);
a = s.nextInt();
b = s.nextInt();
System.out.println("Number before swapping:" +a+" " +b);
a=a+b;
b=a-b;
a=a-b;
System.out.println("Number after swapping:" +a+" " +b);
}
}