Worksheet-3 3
Worksheet-3 3
3
Student Name:- Bhavik Jain UID: 20BCS4380
Create a JSP application with a facility to (1)Login to the application (2) Register a
new user and (3) Change password for an existing user.
In this example, we are going to take “Guru Registration form” which has the following
fields:
First Name
Last Name
Username
Password
Address
Contact Number
After filling all these details we have submit button, on click of that button all the details will
be stored.
Register_1.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>
</head>
<body>
<tr>
<td>First Name</td>
</tr>
<tr>
<td>Last Name</td>
</tr>
<tr>
<td>UserName</td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password" /></td>
</tr>
<tr>
<td>Address</td>
</tr>
<tr>
<td>Contact No</td>
</tr></table>
</body>
</html>
Code Line 11: Here we are taking a form name which has action i.e. the servlet to which the request will be processed
and servlet name is guru_register.java. The request will be processed through POST method.
Code Line 14-16: Here we are taking input type as text and name is first name
Code Line 18-20: Here we are taking input type as text and name is last name
Code Line 22-24: Here we are taking input type as text and name is username
Code Line 26-28: Here we are taking input type as password(this will hide the password when typed) and name as
password
Code Line 30-32: Here we are taking input type as text and name as address
Code Line 34-36: Here we are taking input type as text and name as contact
Code Line 37: Here we are taking a button of type submit and value is also submit. On click of this button the action will
go to corresponding guru_register servlet where all the parameter values will be passed in the request.
Guru_register.java
packagedemotest;
importjava.io.IOException;
importjavax.servlet.RequestDispatcher;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
/**
*/
RequestDispatcherreq = request.getRequestDispatcher("register_1.jsp");
req.include(request, response);
else
RequestDispatcherreq = request.getRequestDispatcher("register_2.jsp");
req.forward(request, response);
Code Line 18: This action doPost() method which will be called when we mention POST in action attribute in the above
JSP form.
Code Line 20-25:Here we are fetching the values from request i.efirst_name, last_name , username, password, address
and contact using request.getParameter.
Code Line 27-32: Here we are taking if condition where we check any of the parameters which are fetched from request
as whether they are empty or not. If any of the parameter is empty then it will enter this condition ( first_name.isEmpty() ||
last_name.isEmpty || username.isEmpty || password.isEmpty || address.isEmpty || contact.isEmpty()) and we have to
fetch RequestDispatcher object using request object which will forward request to register_1.jsp. Here we are also
including request and response objects.
Code Line 33-37: This case will execute when any of the parameter is not empty .We will have to fetch requestDispatcher
object using request object which will forward request to register_2.jsp.Here we are forwarding request and response
objects.
Register_2.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
<head>
</head>
<body>
<a><b>Welcome User!!!!</b></a>
</body>
</html>
Code Line 10: Here we are saying welcome user. This JSP will be called when all the parameters are filled.
When you execute the above code , you get the following output:
Output:
When we click on register_1.jsp, we will get a form which will have details like first name, last name, username, password,
address, contact. All the details have been filled. When we click on submit button then we get the message as “Welcome
User”
In this example, we have taken Login form where we have two fields “username” and “password” with a submit button.
When we click on submit button then we get welcome message with a logout button.
Register_3.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>
</head>
<body>
<tr>
<td>UserName</td>
</tr>
<tr>
<td>Password</td>
</tr>
</table>
</body>
</html>
Code Line 10:Here we are taking a form name which has action i.e. servlet to which it has passed is guru_login.java. The
method through which it will pass its POST.
Code Line 13-16: Here we are taking an input field “username” which is of the type text.
Code Line 17-20: Here we are taking an input field “password” which is of the type password.
Code Line 22: Here we are taking a “submit” button with the value”Login” on which we click then it goes to servlet
guru_login where both the fields are taken using request object.
Guru_login.java(servlet)
packagedemotest;
importjava.io.IOException;
importjavax.servlet.RequestDispatcher;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
/**
*/
publicguru_login() {
super();
RequestDispatcherreq = request.getRequestDispatcher("register_3.jsp");
req.include(request, response);
else
RequestDispatcherreq = request.getRequestDispatcher("register_4.jsp");
req.forward(request, response);
Code Line 14: Here we are taking guru_login servlet which extends HttpServlet.
Code Line 21: Here we are using doPost() method as in the form we are using POST method.
Code Line 23-24: Here we taking parameters using request object i.e. username and password.
Code Line 25-29: In this way, we are taking “if” condition where we are checking username and password whether they
are empty or not.In this case if it is empty then we are getting requestdispatcher object which forwards to register_3.jsp
with request and response objects.
Code Line 30-34: This will be executed if both are not empty then it forwards the request to register_4.jsp with request
and response objects.
Register_4.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>
</head>
<body>
<tr><td>
<tr></tr><tr><td></td><td></td><td><a href="register_3.jsp"><b>Logout</b></a></td></tr>
</table>
</body>
</html>
Code Line 12: Here we are getting parameter “username” from the request object in the string object username.
Code Line 14: Here we link to logout the form which redirects to register_3.jsp.
When you execute the above code then you get the following output:
Output:
Here when we click on register_3.jsp we get two fields”username” and “password” with a login button.
After clicking on the Login button you get the below message with a button of Logout.