Session: A session contains information specific to a particular user across the whole application. When a user enters into a website for the first time, HttpSession is obtained via request.getSession() . You can store the user information into the session object by using setAttribute() method and later when needed this information can be fetched from the session by using getAttribute().
Request Dispatcher: It provides the facility to dispatch the request to the specified resource.
Create Loginform.jsp to get login into the account with form validations.
<form id="Loginform" method="post" action="Form">
<div>
<label>Email:</label>
<input type="text" id="email" name="email"/>
</div>
<div>
<label>Password:</label>
<input type="password" id="pass" name="pass"/>
</div>
<div>
<input type="submit" name="submit" value="Submit"/>
</div>
</form>
The jsp will look like this:

Enter the registered email and password.Click Submit.
The request will be sent to the url mentioned in action attribute in <form> tag i.e, Form.java
In Form.java, create a session object and the object for DAO (user.java) and pass the session object to the user DAO. Check the condition for the name attribute of Submit button(submit).We can use getParameter() method to obtain a parameter value. Fetch the email and password values and send to the ValidateUser method in user.java. The returned value from DAO will be stored in variable status. If the status is > 0 then navigate the request to ShowUser.jsp to show some message using Request Dispatcher.
Form.java
HttpSession session = request.getSession();
user u = new user(session);
if (request.getParameter("submit") != null) {
String email=request.getParameter("email");
String pass=request.getParameter("pass");
int status=u.ValidateUser(email,pass);
if(status>0){
RequestDispatcher rd=request.getRequestDispatcher("ShowUser.jsp");
rd.forward(request, response);
}
}
user.java
Create a session object se. Create constructor user with parameter session. This parameter session comes from the session passed to the user in Handler(Form.java). Assign session to the session object se in constructor. Then you can use this se object to set the session attributes.
private Connection con;
HttpSession se;
public user(HttpSession session) {
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sooksnew_sooksmain1", "root", "root");
se = session;
} catch (Exception e) {
e.printStackTrace();
}
}
Create a method ValidateUser() to fetch the name and slno of the user's entered email and password. If the query returns a value, increment the status variable and add the name and slno to the session attribute by the name id and name. Return the status value.
public int ValidateUser(String email, String pass) {
int status = 0;
try {
Statement s = null;
ResultSet rs = null;
s = con.createStatement();
String query = "select slno,name from sookshmas where email='" + email + "' and password='" + pass + "'";
rs = s.executeQuery(query);
while (rs.next()) {
status++;
se.setAttribute("id", rs.getInt("slno"));
se.setAttribute("name", rs.getString("name"));
}
} catch (Exception e) {
e.printStackTrace();
}
return status;
}
ShowUser.jsp
In this jsp, I am displaying the logged in user name i.e,Sookshmas. First check whether the session attribute is having the value or not. If it is not equal to null then print Welcome Sookshmas.
<style>
#name{
color: red;
font-style: italic;
font-size: 45px;
}
</style>
<%if (session.getAttribute("name") != null) {%>
<h1>Welcome <span id="name"><%=session.getAttribute("name")%></span></h1>
<%}%>
The output will be:

NOTE: To use java code in jsp use scriplet tag <% %>. To print the java value use <%= %>.