JSP – Session Implicit Object

JSP Session Implicit Object

  • JSP session object is an instance of javax.servlet.http.HttpSession.
  • This object is used to for session tracking or session management.

login.jsp:

<html>
            <head>
                        <title>login</title>
            </head>
            <body>
                        <form action=”welcome.jsp”>
                                    <input type=”text” name=”userName” />
                                    <input type=”submit” value=”login”/>
                        </form>
            </body>
</html>

welcome.jsp:

<html>
            <head>
                        <title>session implicit object example</title>
            </head>
            <body>
                        <%
                                    String userName=request.getParameter(“userName”);
                                    if(userName.equals(“amar”))
                                    {
                                         session.setAttribute(“userName”, userName);
                                         response.sendRedirect(“home.jsp”); 
                                    }
                                    else
                                    {
                                         out.print(“Wrong username.”); 
                                    }
                        %>
            </body>
</html>

home.jsp:

<html>
            <head>
                        <title>home</title>
            </head>
            <body>
                        <h3>This is user’s home page.</h3>
                        <%
                           String userName =
                          (String)session.getAttribute(“userName”);
                           out.print(“Logged in user: ” + userName);
                        %>
            </body>
</html>
Scroll to Top