• Struts
  • Spring
  • jQuery
  • Java IO
  • Core Java
  • JSP
  • RubyonRails
  • PHP
  • JavaScript

  • <

    Session in JSP Tutorial

    JSP Session is an object of javax.servlet.http.HttpSession. JSP Session data can set, get and remove using HttpSession. You can retrive session data for a specific time after that it will expired.

    Session can be accessed multiple times until it will not expired.

    Jsp Session setAttribute
    session.setAttribute("username", name);
    

    Jsp Session getAttribute
    session.getAttribute("username");
    

    Jsp Get and Set Session Example


    index.jsp
    <form action="set.jsp">   
    <input type="text" name="name">  
    <input type="submit" value="submit"><br/>  
    </form>   
    


    set.jsp

    We can set session attribute username using setAttribute.

    
    <html>  
    <body>  
    <%   
      
    String name=request.getParameter("name");  
    out.print("Welcome "+name);  
      
    session.setAttribute("username",name);  
      
    
      
    %>  
    <a href="get.jsp">Get Session</a>  
    </body>  
    </html>  
    


    get.jsp

    Get attribute used to retrive session data username.

    <html>
    <body> 
    <%   
      
    String name2=(String)session.getAttribute("username");  
    out.print("Hello "+name2);  
      
    %>  
    </body>
    </html>  
    

    output:
    capture

    capture

    capture
















    © copyright 2017-2021 Completedone pvt ltd.