beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

How to validate and invalidate session in JSP

By Chaitanya Singh | Filed Under: JSP tutorial

We have already seen invalidate() method in session implicit object tutorial. In this post we are going to discuss it in detail. Here we will see how to validate/invalidate a session.

Example

Lets understand this with the help of an example: In the below example we have three jsp pages.

  • index.jsp: It is having four variables which are being stored in session object.
  • display.jsp: It is fetching the attributes (variables) from session and displaying them.
  • errorpage.jsp: It is first calling session.invalidate() in order to invalidate (make the session inactive) the session and then it has a logic to validate the session (checking whether the session is active or not).

index.jsp

<% 
   String firstname="Chaitanya";
   String middlename="Pratap";
   String lastname="Singh";
   int age= 26;
   session.setAttribute( "fname", firstname );
   session.setAttribute( "mname", middlename );
   session.setAttribute( "lname", lastname );
   session.setAttribute( "UAge", age );
%>
<a href="display.jsp">See Details</a>
<a href="errorpage.jsp">Invalidate Session</a>

display.jsp

<%= session.getAttribute( "fname" ) %>
<%= session.getAttribute( "mname" ) %>
<%= session.getAttribute( "lname" ) %>
<%= session.getAttribute( "UAge" ) %>

errorpage.jsp

<%session.invalidate();%>
<% HttpSession nsession = request.getSession(false);
if(nsession!=null) {
   String data=(String)session.getAttribute( "fname" );
   out.println(data);
}
else
  out.println("Session is not active");
%>

Output:
while opening display page, all the attributes are getting displayed on client (browser).
Since we have already called invalidate in the first line of errorpage.jsp, it is displaying the message “Session is not active” on the screen

Points to Note:
1) This will deactivate the session

<%session.invalidate();%>

2) This logic will exceute the if body when the session is active else it would run the else part.

<% HttpSession nsession = request.getSession(false);
if(nsession!=null)
   ...
else
   ...
%>

Enjoyed this post? Try these related posts

  1. Application Implicit Object in JSP with examples
  2. Config Implicit Object in JSP with examples
  3. Jsp Implicit Objects
  4. Session Implicit Object in JSP with examples
  5. How to access body of Custom tags in JSP tutorial
  6. Request Implicit Object in JSP with examples

Leave a Reply Cancel reply

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

JSP Tutorial

  • Basics of JSP
  • Life cycle of JSP
  • JSP in Eclipse IDE
  • Declaration tag
  • Expression tag
  • Scriptlets
  • Directives
  • Include Directive
  • Param Directive
  • Exception handling
  • Action tags
  • Include action
  • Forward action
  • useBean, setProperty & getProperty
  • Implicit Objects
  • Session implicit object
  • Validate session
  • Request implicit object
  • Response implicit object
  • Out implicit object
  • Application implicit object
  • Config implicit object
  • pageContext implicit object
  • Exception implicit object
  • Expression language
  • Custom Tags
  • Custom tag example
  • JSP Interview Q

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2019 BeginnersBook . Privacy Policy . Sitemap