HttpSession

HttpSession is an inbuilt interface present in javax.servlet.http package, which is another technique used for session management. HttpSession object is used to store entire session with a specific client. We can store, retrieve and remove attribute from HttpSession object. Here when a request is coming from client to the main server, then main server forwards the request to the web container, then web container creates a unique session id for request and gives it back to the client with response. This is a temporary session created by web container. The web container uses this id to identify the particular user. The client sends back the session ID with each request.HttpSession object can be used to perform bind objects, view and manipulate information about a session, such as the session identifier, creation time, and last accessed time.

http_session

How to get HttpSession object:

HttpSession session=request.getSession();

When getSession() method invoking by request object, then it returns HttpSession object. If the session already exists then it returns the existing session otherwise it will create a new session.


How to destroy a session:

session.invalidate();
there is a method invalidate() when invoked by the session object, then a session will be destroyed.
Let's see an example of session management using HttpSession interface for better understanding:

Required files:

  • 1. login.html
  • 2. LoginServlet.java
  • 3. WelcomeServlet.java
  • 4. web.xml
login.html
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<center><h2>Login Form</h2>
    <form action="LoginServlet">
        <table>
            <tr>
                <td>UserName</td>
                <td><input type="text"name="uname"></td>
            </tr>
            <tr>
                <td>Password</td>
                <td><input type="password"name="pass"></td>
            </tr>
            <tr>
                <td><input type="submit"value="login"></td>
            </tr>
        </table>
    </form>
</center>
</body>
</html>
LoginServlet.java
package com.silan;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class LoginServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {

        response.setContentType("text/html");
        PrintWriter out=response.getWriter();

        String s1=request.getParameter("uname");
        String s2=request.getParameter("pass");

        if(s2.equals("admin"))
        {
            out.print("login is successful");
            HttpSession session=request.getSession();
            session.setAttribute("uname", s1);

            out.print("<a href='WelcomeServlet'>Go</a>");
        }
        else
        {
            out.print("sry!!invalid credential");
            RequestDispatcher
            rd=request.getRequestDispatcher("login.html");
            rd.include(request, response);
        }
    }
}
WelcomeServlet.java
package com.silan;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class WelcomeServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {

        response.setContentType("text/html");
        PrintWriter out=response.getWriter();

        HttpSession session=request.getSession(false);
        String s3=(String)session.getAttribute("uname");

        out.print("Hi"+" "+s3);
}

Output:

img

Example Explanation:

In the above example we have a login form, when action will generate that means after giving user name and password value, the user will click on login button, then LoginServlet.java will execute. If the password value is admin then one response message will display that is login is successful, then one session have created containing some information and an hyperlink have created that is Go. This is the objective of LoginServlet.java and when the user will click on Go then WelcomeServlet.java will execute. What ever the session have created in LoginServlet.java and containing information, that information we will get in WelcomeServlet.java as a response.





 PreviousNext