JSP - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

JSP Session

JSP Session

shape Description

JSP Session is an implicit object of the type HttpSession. JSP Session object is used to send information from the web browser to server and from the server to a web browser. The main advantage of the Session Object is, it maintains information when there are multiple requests. Session handling becomes important when requested data need to be sustained for further use. When variables are stored in JSP Session object, it will navigate between multiple requests in the application. To maintain information in entire application use of cookies, URL rewriting, Hidden Form Fields are used.

Methods of JSP Session object

shape Methods

setAttribute(String, object)

setAttribute(String, object) method is used to set an object by assigning a unique string to that object, later that object can be used anyway in the application by using the unique string.

getAttribute(String, object)

getAttribute(String) method is used to get the session which is already stored in setAttribute() method and the return type of this method is an object.

removeAttribute(String)

removeAttribute(String) method is used to remove the session object which is stored in the session. To pass a unique string removeAttribute's method can be used.

getAttributesNames()

getAttributesNames() method is used to return all the objects which are stored in the session.

getId()

getId() method returns the unique string identifier from a session in which servlet container is assigned.

getCreationTime()

getCreationTime() method is used when a session is initialized and returns session creation time.

isNew()

isNew() method checks whether a session is new or not, If a session is new it returns true otherwise false.

invalidate()

invalidate() method is used for making session inactive and breaks the session with stored object.

getLastAccessedTime()

getLastAccessedTime() method returns the session last access time.

getMaxInactiveInterval()

getMaxInactiveInterval() method returns maximum inactive time of session.

JSP Session Object

shape Example

JSP Session - Following is an example. index1.html [html]<html> <body> <form action="welcome1.jsp"> <input type="text" name="uname"> <input type="submit" value="go"><br/> </form> </body> </html>[/html] Here just created the text box and also created the go button. welcome1.jsp [html]<html> <body> <% String name=request.getParameter("uname"); out.print("Welcome "+name); session.setAttribute("user",name); %> <a href="second.jsp">second jsp page</a> </body> </html> [/html] request.getParameter("uname") is used to get the parameters from the HTML page.  second.jsp [html]<html> <body> <% String name=(String)session.getAttribute("user"); out.print("Hello "+name); %> </body> </html> [/html] While executing the code index1.html file should be in active mode. Output: After executing the above code successfully,where first write the text as like to SPLESSONS then click on go button. Output will be as follows along with the second.jsp file is as follows. When click second.jsp file, output will be as follows .

Summary

shape Key Points

  • JSP Session - The session is time oriented if time is completed, then the session will be automatically closed.
  • JSP Session - Cookies store the information in the client browser.
  • JSP Session is an implicit object of the type HttpSession.
  • The JSP Session object provides the communication between the server and browser to fetch the data.