banner



How To Access Session Data In Jsp Servlet

Session is most frequently used implicit object in JSP. The master usage of it to proceeds access to all the user's data till the user session is active.

Methods of session Implicit Object

  1. setAttribute(Cord, object) –This method is used to save an object in session by assigning a unique string to the object. Later, the object can be accessed from the session by using the same String till the session is active. setAttribute and getAttribute are the ii nigh ofttimes used methods while dealing with session in JSP.
  2. getAttribute(String name) –The object stored by setAttribute method is fetched from session using getAttribute method. For example if we need to access userid on every jsp page till the session is active and then nosotros should store the user-id in session using setAttribute method and can be accessed using getAttribute method whenever needed.
  3. removeAttribute(String proper noun) –The objects which are stored in session tin can exist removed from session using this method. Laissez passer the unique string identifier as removeAttribute's method.
  4. getAttributeNames –It returns all the objects stored in session. Basically, information technology results in an enumeration of objects.
  5. getCreationTime –This method returns the session creation time, the time when session got initiated (became agile).
  6. getId –Servlet container assigns a unique string identifier to session while creation of information technology. getId method returns that unique string identifier.
  7. isNew() –Used to check whether the session is new. It returns Boolean value (true or fake). By and large used to track whether the cookies are enabled on client side. If cookies are disabled the session.isNew() method would always render true.
  8. invalidate() –It kills a session and breaks the association of session with all the stored objects.
  9. getMaxInactiveInterval –Returns session'south maximum inactivate time interval in seconds.
  10. getLastAccessedTime –More often than not used to know the last accessed fourth dimension of a session.

Session Implicit Object Example

The beneath html page would display a text box along with a submit push. The submit action would transfer the command to session.jsp page.

alphabetize.html

<html>  <head> <title>Welcome Folio: Enter your proper noun</championship> </head> <body>  <form action="session.jsp">  <input type="text" name="inputname">  <input blazon="submit" value="click here!!"><br/>  </course>  </body>  </html>

The session.jsp page displays the name which user has entered in the index page and it stores the the same variable in the session object so that it can be fetched on any page until the session becomes inactive.

session.jsp

<html>  <head> <title>Passing the input value to a session variable</championship> </head> <trunk>  <%  String uname=request.getParameter("inputname");  out.print("Welcome "+ uname); session.setAttribute("sessname",uname);  %>  <a href="output.jsp">Check Output Folio Here </a> </body>  </html>

In this folio nosotros are fetching the variable'south value from session object and displaying information technology.

output.jsp

<html>  <head> <title>Output folio: Fetching the value from session</title> </caput> <body>  <%  String name=(String)session.getAttribute("sessname");  out.impress("Hello User: You take entered the proper noun: "+name);  %>  </body>  </html>

Output Screens

jsp-welcome-page

User-jsp-page

output-screen

How To Access Session Data In Jsp Servlet,

Source: https://beginnersbook.com/2013/11/jsp-implicit-object-session-with-examples/

Posted by: herndonpudel1948.blogspot.com

0 Response to "How To Access Session Data In Jsp Servlet"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel