Java Servlet Applications for Login and Calculator
Java Servlet Applications for Login and Calculator
[Link]
<html>
<head>
<title>Calculator</title>
<meta charset="UTF-8">
</head>
<body>
<br><h1>------------Calculator------------</h1><br>
Select an Operation
<input type="reset">
</form>
</body>
</html>
[Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
[Link]("text/html;charset=UTF-8");
[Link]("<!DOCTYPE html>");
[Link]("<html>");
[Link]("<head>");
[Link]("<title>Servlet CalculatorServlet</title>");
[Link]("</head>");
[Link]("<body>");
double n1 = [Link]([Link]("t1"));
double n2 = [Link]([Link]("t2"));
String opr=[Link]("opr");
if([Link]("+"))
result=n1+n2;
if([Link]("-"))
result=n1-n2;
if([Link]("*"))
result=n1*n2;
if([Link]("/"))
result=n1/n2;
[Link]("</body>");
[Link]("</html>");
}
}
Output
B) Aim : Create a servlet for a login page. If the username and password are correct then it
says message “Hello ” else a message “login failed”
[Link]
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="LoginServlet" method="get">
<p>1B. Create a servlet for a login page. If the username and password are
correct then it says
message “Hello username” else a message “login failed” </p><br>
UserName : <input type="text" name="t1"><br>
Password : <input type="password" name="t2"><br>
<h6>username= abhi<br>password= 1234</h6><br>
<input type="reset">
<input type="Submit">
</form>
</body>
</html>
[Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
[Link]
class method
{
}
Output
Practical 2 Aim : Create a registration servlet in Java using JDBC. Accept the details such
as Username, Password, Email, and Country from the user using HTML Form and store the
registration details in the database.
[Link] -
<!DOCTYPE html>
<html>
<head>
<title>Registration form</title>
<meta charset="UTF-8">
</head>
<body>
<form action="RegServlet">
</form>
</body>
</html>
[Link] -
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
[Link]("text/html;charset=UTF-8");
[Link]("<!DOCTYPE html>");
[Link]("<html>");
[Link]("<head>");
[Link]("<title>Servlet RegServlet</title>");
[Link]("</head>");
[Link]("<body>");
String id = [Link]("t1");
String ps = [Link]("p1");
String em = [Link]("e1");
String co = [Link]("c1");
try
[Link]("[Link]").newInstance();
Connection con=(Connection)
[Link]("jdbc:mysql://localhost:3306/db10?
characterEncoding=latin1","root","root");
[Link](1,id);
[Link](2,ps);
[Link](3,em);
[Link](4,co);
catch(Exception e)
[Link](e);
[Link]("</body>");
[Link]("</html>");
Output:
Practical 3 : Aim : Using Request Dispatcher Interface create a Servlet which will validate
the password entered by the user, if the user has entered "Servlet" as password, then he will be
forwarded to Welcome Servlet else the user will stay on the [Link] page and an error
message will be displayed.
[Link]
<html>
<head>
<title>Login Page Information</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="validate" method="post">
<br>
<h1>------Login-------</h1><br>
Name: <input type="text" name="txt1"></br>
Password: <input type="password" name="txt2"><h6>username=
abhi<br>password= servlet</h6></br>
<input type="reset" value="reset">
<input type="submit" value="Submit">
</form>
</body>
</html>
[Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
[Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
Output
Practical 4 A) Aim : Create a servlet that uses Cookies to store the number of
times a user has visited servlet.
[Link]
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<form action="Page1">
</form>
</body>
</html>
[Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
[Link]("text/html;charset=UTF-8");
[Link]("<!DOCTYPE html>");
[Link]("<html>");
[Link]("<head>");
[Link]("<title>Servlet Page1</title>");
[Link]("</head>");
[Link]("<body bgcolor=yellow>");
[Link](ck1);
[Link](ck2);
[Link]("</body>");
[Link]("</html>");
[Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
[Link]("text/html;charset=UTF-8");
[Link]("<!DOCTYPE html>");
[Link]("<html>");
[Link]("<head>");
[Link]("<title>Servlet Page1</title>");
[Link]("</head>");
[Link]("<body bgcolor=yellow>");
[Link](ck1);
[Link](ck2);
[Link]("</body>");
[Link]("</html>");
[Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
[Link]("text/html;charset=UTF-8");
[Link]("<!DOCTYPE html>");
[Link]("<html>");
[Link]("<head>");
[Link]("<title>Servlet Page3</title>");
[Link]("</head>");
[Link]("<body bgcolor=lime>");
Cookie [] ck = [Link]();
for(int i=0;i<[Link];i++)
if(ck[i].getName().equals("Visit"))
[Link](ck[i]);
else
[Link](ck[i].getName()+" = "+ck[i].getValue());
[Link]("</body>");
[Link]("</html>");
[Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
[Link]("text/html;charset=UTF-8");
[Link]("<!DOCTYPE html>");
[Link]("<html>");
[Link]("<head>");
[Link]("<title>Servlet Page4</title>");
[Link]("</head>");
[Link]("<body bgcolor=pink>");
Cookie [] ck = [Link]();
for(int i=0;i<[Link];i++)
if(ck[i].getName().equals("Visit"))
[Link](ck[i]);
else
[Link](ck[i].getName()+" = "+ck[i].getValue());
[Link]("</html>");
Output
B)_ Aim : Create a servlet demonstrating the use of session creation and destruction.
Also check whether the user has visited this page first time or has visited earlier also using
sessions.
[Link]
<html>
<head>
<title>Session Demo</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="Page1" method="get">
Enter User ID <input type="text" name="t1"><br>
<input type="reset"><input type="submit">
</form>
</body>
</html>
[Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
[Link]("text/html;charset=UTF-8");
[Link]("<!DOCTYPE html>");
[Link]("<html>");
[Link]("<head>");
[Link]("<title>Servlet LogoutServlet</title>");
[Link]("</head>");
[Link]("<body>");
[Link] hs = [Link]();
if(hs!=null)
[Link]();
[Link]("<h1>You are Loggged out now....</h1>");
[Link]("</body>");
[Link]("</html>");
[Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
[Link]("text/html;charset=UTF-8");
[Link]("<!DOCTYPE html>");
[Link]("<html>");
[Link]("<head>");
[Link]("<title>Servlet Page1</title>");
[Link]("</head>");
[Link]("<body>");
HttpSession hs = [Link](true);
if([Link]())
{
[Link]("<body bgcolor = yellow>");
[Link]("uname",name);
[Link]("visit", "1");
else
[Link]("<h1>Welcome Again</h1>");
[Link]("visit"))+1;
[Link]("visit","" +visit);
[Link]("</body>");
[Link]("</html>");
[Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
[Link]("text/html;charset=UTF-8");
[Link]("<!DOCTYPE html>");
[Link]("<html>");
[Link]("<head>");
[Link]("<title>Servlet Page2</title>");
[Link]("</head>");
[Link]("<body>");
HttpSession hs = [Link](false);
[Link]("visit"))+1;
[Link]("visit","" +visit);
[Link]("</body>");
[Link]("</html>");
}
}
Output
Practical 5 : Implement the Servlet IO and File applications.
upload
[Link]
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
</form>
</body>
</html>
[Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@MultipartConfig
[Link]("text/html;charset=UTF-8");
[Link]("<!DOCTYPE html>");
[Link]("<html>");
[Link]("<head>");
[Link]("<title>Servlet fileuploadservlet</title>");
[Link]("</head>");
[Link]("<body>");
String path=[Link]("destination");
String filename=[Link]();
OutputStream os = null;
InputStream is = null;
try
is=[Link]();
int read=0;
while((read=[Link]())!=-1)
[Link](read);
catch(FileNotFoundException e)
[Link](e);
[Link]("</body>");
[Link]("</html>");
Output
Download
[Link]
<html>
<head>
</head>
<body>
<br/><br/>
</body>
</html>
[Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
[Link]("text/html;charset=UTF-8");
int len=0;
{
ServletContext context =getServletConfig().getServletContext();
[Link](([Link](filename)!=null)?
[Link](filename):"application/pdf");
while((is!=null)&&((len=[Link](b))!= -1))
[Link](b,0,len);
[Link]();
[Link]();
[Link]();
Output
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<label>What is the variable declared in a class for the use of all methods of the class
called ?</label><br>
</form>
</body>
</html>
[Link] –
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
[Link]("text/html;charset=UTF-8");
[Link]("<!DOCTYPE html>");
[Link]("<html>");
[Link]("<head>");
[Link]("<title>Servlet QuizServlet</title>");
[Link]("</head>");
[Link]("<body>");
int cnt = 0;
try
[Link]("[Link]");
Connection con =
[Link]("jdbc:mysql://localhost:3306/quiz10?
characterEncoding=latin1","root","root");
String s = [Link](1);
paramName = (String)[Link]();
paramValue = [Link](paramName);
ans = paramValue[i];
if([Link](ans))
cnt++;
catch(Exception e)
[Link]("</body>");
[Link]("</html>");
Output :
import [Link].*;
import [Link].*;
import [Link].*;
copyData(source,destination);
[Link]();
[Link]();
while([Link](buffer) != -1)
[Link]();
while([Link]())
[Link](buffer);
[Link]();
}
Practical 7 : Implement the following JSP applications.
A) Aim : Develop a simple JSP application to display values obtained from the use of
intrinsic objects of various types.
[Link]
<%@page import="[Link]"%>
<html>
<head>
<title>JSP Page</title>
</head>
<body>
ID <%=[Link]() %><br>
</body>
</html>
Output
B) Aim : Develop a simple JSP application to pass values from one page to another with
validations. (Name-txt, age-txt, hobbies-checkbox, email-txt, gender-radio button).
[Link]
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<tr>
</tr>
<tr>
</tr>
<tr>
</tr>
<tr>
<td>
</td>
</tr>
<tr>
<td>Select Gender:</td>
<td>
</tr>
<tr>
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
[Link]
<!DOCTYPE html>
<html>
<head>
<title>JSP Page</title>
</head>
<body>
<center>
<div>
<h2>VALIDATE PAGE</h2>
<%
else
try
catch(Exception e)
}
if(hobbies == null || [Link]==0)
if(![Link]().equals(""))
[Link]("<p style='color:red;'>"+errmsg+"</p>");
else
[Link]("<p><strong>Name: </strong>"+name+"</p>");
[Link]("<p><strong>Age: </strong>"+agestr+"</p>");
[Link]("<p><strong>Hobbies: </strong></p>");
[Link]("<p>"+hob+"</p>");
[Link]("<p><strong>Email: </strong>"+email+"</p>");
[Link]("<p><strong>Gender: </strong>"+gender+"</p>");
%>
</div>
</center>
</body>
</html>
Output
Develop Simple Servlet Question Answer Application using Develop Simple Servlet Question
Database. Answer Application using Database.
Develop Simple Servlet Question Answer Application using Develop Simple Servlet Question Answer
Database. Application using Database.
Practical 8 Aim : Create a registration and login JSP application to register and
authenticate the user based on username and password using JDBC.
[Link] –
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="[Link]">
</form>
</body>
</html>
[Link] –
<%@page import="[Link]"%>
<%@page import="[Link]"%>
<%@page import="[Link]"%>
<%@page import="[Link]"%>
<!DOCTYPE html>
<html>
<head>
<title>JSP Page</title>
</head>
<body>
<%
try
[Link]("[Link]");
Connection con =
[Link]("jdbc:mysql://localhost:3306/dbemp10?
characterEncoding=latin1","root", "root");
[Link](1, eno);
ResultSet rs = [Link]();
if([Link]())
[Link](1, sal);
[Link](2, eno);
[Link](1, age);
[Link](2, eno);
[Link](1, name);
[Link](2, eno);
[Link]();
[Link]();
[Link]();
}
else
catch(Exception e)
[Link](e);
%>
</body>
</html>
Output :