Servlet HttpSession example using eclipse

Servlet HttpSession class shows you a way to identify a user across more than one page request or visit to a Web site and to store information about that user.

The servlet container uses this interface to create a session between an HTTP client and an HTTP server.

The session persists for a specified time period, across more than one connection or page request from the user. A session usually corresponds to one user, who may visit a site many times.

Create session in java class using HttpSession

HttpSession session=request.getSession();

or by adding true or false to getSession() function

HttpSession session=request.getSession(true);

true – return existing session object or it will create new Session if there is no session available
false – return the existing session, if there is no session available it returns null

HttpSession setAttribute

Binds an object to this session, using the name specified. you can setAttribute to the session using created session object as shown below

session.setAttribute("auth","candidjava");

Name should be String and Value can be of any object;

HttpSession getAttribute

Returns the object bound with the specified name in this session, or null if no object is bound under the name. you can getAttribute from the created session

String val=(String)session.getAttribute("auth");

HttpSession invalidate

Invalidates this session then unbinds any objects bound to it.

session.invalidate();

Servlet HttpSession Example

Index page

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>Simple HttpSession example</h1>

<form action="LoginController" method="post">
   Username: <input type="text" name="uname"> <br>
   Password: <input type="password" name="pass"> <br>
   <input type="submit" value="Login">
</form>
</body>
</html>

Login controller

Checkout the below servlet file for creating HttpSession

package com.candidjava;

import java.io.IOException;
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;

/**
 * Servlet implementation class LoginController
 */
@WebServlet("/LoginController")
public class LoginController extends HttpServlet {

   protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
   {
      String un=request.getParameter("uname");
      String pw=request.getParameter("pass");

      HttpSession session= request.getSession();
      session.setAttribute("name", un);

      response.sendRedirect("home.jsp");
   }

}

Success page

Find the below code to display httpSession value in browser

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>Retrieving HttpSession value</h1>
<br>
<%
   String name=(String)session.getAttribute("name");

%>

HttpSession value is <%=name %>
</body>
</html>

 

Output Screenshot

Download Source code

HttpSession example war

HttpSession example zip

 

Leave a Reply

Your email address will not be published. Required fields are marked *