Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

How to add and retrieve Sessions in JSP and Servlets using Session Management API ?



A simple web application showing how to add and retrieve Sessions in JSP and Servlets using Session Management API

1. mystyle.css (not mandatory) just for look and feel


body {
 font-family: Verdana, Geneva, Arial, helvetica, sans-serif;
 background-color: #FDF4DF;
 font-size: 14px;
 color: #FFFFFF;
}

td {
 font-family: Verdana, Geneva, Arial, helvetica, sans-serif;
 font-size: 14px;
}

th {
 font-family: Verdana, Geneva, Arial, helvetica, sans-serif;
 font-size: 14px;
}

.black {
 color: #000000;
 font-weight: bold;
}



2. register.jsp



1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.

<%@ 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>Registration</title>
<link href="mystyle.css" rel="stylesheet" type="text/css">
</head>
<body>

 <form method="post" action="Register">
  <p>&nbsp;</p>
  <p align="center" class="black">Create a new Account :</p>
  <table align="center" bgcolor="#728DCF">
   <tr>
    <td>First Name</td>
    <td><input type="text" name=firstName size=30 class="smalltext"></td>
   </tr>
   <tr>
    <td>Last Name</td>
    <td><input type="text" name=lastName size=30 class="smalltext"></td>
   </tr>

   <tr>
    <td>Email Address</td>
    <td><input type="text" name=email size=40 class="smalltext"></td>
   </tr>

   <tr>
    <td>Password</td>
    <td><input type="password" name=password size=30
     class="smalltext"></td>
   </tr>
   <tr>
    <td colspan="2" align="center"><input type="submit"
     value="Register" /></td>
   </tr>
  </table>
 </form>

</body>
</html>


3. Register.java (Servlet)


import java.io.IOException;
import java.util.HashMap;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


@WebServlet("/Register")
public class Register extends HttpServlet {


 protected void doGet (
   HttpServletRequest request, 
   HttpServletResponse response
   ) throws ServletException, IOException {

  doPost(request, response);

 }


 protected void doPost(
   HttpServletRequest request, 
   HttpServletResponse response
   ) throws ServletException, IOException {

  String firstName = request.getParameter("firstName");
  String lastName = request.getParameter("lastName");
  String email = request.getParameter("email");
  String password = request.getParameter("password");

  HashMap<String, String> userDetails = new HashMap<String, String>();
  userDetails.put("firstName", firstName);
  userDetails.put("lastName", lastName);
  userDetails.put("email", email);
  userDetails.put("password", password);

  // Creating a Session object and storing 
  // HashMap into it 
  HttpSession session = request.getSession();
  session.setAttribute("user", userDetails);
  // forwarding request to confirm.jsp page
  RequestDispatcher dispatcher = request.getRequestDispatcher("confirm.jsp");
  dispatcher.forward(request, response);

 }

}


4. confirm.jsp



1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.

<%@ 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>Confirmed</title>
<link href="mystyle.css" rel="stylesheet" type="text/css">
</head>
<body>

 <%@ page import="java.util.*;"%>

 <%
  HashMap<String, String> userDetails = (HashMap) session
    .getAttribute("user");
  String firstName = "";
  String lastName = "";
  String emailAddress = "";
  if (userDetails != null) {
   firstName = userDetails.get("firstName");
   lastName = userDetails.get("lastName");
   emailAddress = userDetails.get("email");

  }
  else {

   firstName = null;
   lastName = null;
   emailAddress = null;

  }
 %>

 <p>&nbsp;</p>
 <p align="center" class="black">The information entered by you is
  as follows :</p>

 <table align="center" bgcolor="#728DCF">
  <tr>
   <td>First Name :</td>
   <%-- Below is the JSP expression used to display string value of an expression --%>
   <td><%=firstName%></td>
  </tr>

  <tr>
   <td>Last Name :</td>
   <td><%=lastName%></td>
  </tr>

  <tr>
   <td>Email Address :</td>
   <td><%=emailAddress%></td>
  </tr>
 </table>


</body>
</html>


Output of the program :


1. register.jsp screen view



















2. confirm.jsp screen view




 
© 2021 Learn Java by Examples Template by Hubberspot