Working With Sessions in JSP

Discussion in 'JSP' started by Sanskruti, Apr 16, 2007.

  1. Sanskruti

    Sanskruti New Member

    Joined:
    Jan 7, 2007
    Messages:
    108
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Consultant
    Location:
    Mumbai, India
    This article shows you how to track the session between different JSP pages. In any web application user moves from one page to another and it becomes necessary to track the user data and objects throughout the application. JSP provide an implicit object "session", which can be use to save the data specific the particular to the user.In this article we will create an application that takes the user name from the user and then saves into the user session. We will display the saved data to the user in another page.

    Here is the code of the JSP file (savenameform.jsp) that takes the input from user:
    HTML:
    <%@ page language="java" %>
    <html>
        <head>
            <title>Name Input Form</title>
        </head>
        <body>
            <form method="post" action="savenametosession.jsp">
                <p><b>Enter Your Name: </b><input type="text" name="username"><br>
                <input type="submit" value="Submit">
            </form>
        </body>
    </html>
    The above page prompts the user to enter his/her name. Once the user clicks on the submit button, savenametosession.jsp is called. The JSP savenametosession.jsp retrieves the user name from request attributes and saves into the user session using the function session.setAttribute("username",username);. Here is the code of savenametosession.jsp:
    HTML:
    <%@ page language="java" %>
    <%
        String username=request.getParameter("username");
        if(username==null) username="";
            session.setAttribute("username",username);
    %>
    <html>
        <head>
            <title>Name Saved</title>
        </head>
        <body>
            <p><a href="showsessionvalue.jsp">Next Page to view the session value</a><p>
        </body>
    </html>
    The above JSP saves the user name into the session object and displays a link to next pages (showsessionvalue.jsp). When user clicks on the "Next Page to view session value" link, the JSP page showsessionvalue.jsp displays the user name to the user. Here is the code of showsessionvalue.jsp:
    HTML:
    <%@ page language="java" %>
    <%
        String username=(String) session.getAttribute("username");
        if(username==null) username="";
    %>
    <html>
        <head>
            <title>Show Saved Name</title>
        </head>
        <body>
            <p>Welcome: <%=username%><p>
        </body>
    </html>
    The function session.getAttribute("username") is used to retrieve the user name saved in the session
     
    Last edited: Apr 18, 2007
    alssadi likes this.
  2. parvez.yu

    parvez.yu New Member

    Joined:
    Feb 14, 2008
    Messages:
    100
    Likes Received:
    0
    Trophy Points:
    0
    i will try the code
     
  3. kevinthomas12c

    kevinthomas12c New Member

    Joined:
    Jul 14, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    i need help with sessions in JSP.. i implemented sessions. and it worked correctely..but i got a problem. every time i logout it successfully logouts but if i press the back button on the menu bar it automatically gets redirected back to the page of the user who just logged out... i would like somebody to help..
    pls..
     
  4. gkumar

    gkumar New Member

    Joined:
    Jun 16, 2009
    Messages:
    58
    Likes Received:
    5
    Trophy Points:
    0
    This JSP Tutorial shows you how to track the session between different JSP pages. In any web application user moves from one page to another and it becomes necessary to track the user data and objects throughout the application. JSP provide an implicit object "session", which can be use to save the data specific the particular to the user.

    In this tutorial we will create an application that takes the user name from the user and then saves into the user session. We will display the saved data to the user in another page.

    Here is the code of the JSP file (savenameform.jsp) that takes the input from user:

    Code:
    <%@ page language="java" %>
    <html>
    <head>
    <title>Name Input Form</title>
    </head>
    <body>
    <form method="post" action="savenametosession.jsp">
    <p><b>Enter Your Name: </b><input type="text" name="username"><br>
    <input type="submit" value="Submit">
    
    </form>
    
    </body>
    
    The above page prompts the user to enter his/her name. Once the user clicks on the submit button, savenametosession.jsp is called. The JSP savenametosession.jsp retrieves the user name from request attributes and saves into the user session using the function session.setAttribute("username",username);. Here is the code of savenametosession.jsp:

    Code:
    <%@ page language="java" %>
    <%
    String username=request.getParameter("username");
    if(username==null) username="";
    
    session.setAttribute("username",username);
    %>
    
    <html>
    <head>
    <title>Name Saved</title>
    </head>
    <body>
    <p><a href="showsessionvalue.jsp">Next Page to view the session value</a><p>
    
    </body>
    The above JSP saves the user name into the session object and displays a link to next pages (showsessionvalue.jsp). When user clicks on the "Next Page to view session value" link, the JSP page showsessionvalue.jsp displays the user name to the user. Here is the code of showsessionvalue.jsp:

    Code:
    <%@ page language="java" %>
    <%
    String username=(String) session.getAttribute("username");
    if(username==null) username="";
    %>
    <html>
    <head>
    <title>Show Saved Name</title>
    </head>
    <body>
    <p>Welcome: <%=username%><p>
    
    </body>
    The function session.getAttribute("username") is used to retrieve the user name saved in the session.
     
  5. alssadi

    alssadi Banned

    Joined:
    Dec 11, 2010
    Messages:
    41
    Likes Received:
    3
    Trophy Points:
    0
    Occupation:
    Creative director & web developer
    Location:
    Dubai
    Home Page:
    http://uaeinfographics.blogspot.com/
    what if i click on back botton i think it will still be logied in ?
     
  6. rameshb

    rameshb New Member

    Joined:
    Dec 10, 2010
    Messages:
    35
    Likes Received:
    1
    Trophy Points:
    0
    great me definitely going to try this code.. thanks alot
     
  7. seoabhisek

    seoabhisek Banned

    Joined:
    Jan 22, 2011
    Messages:
    24
    Likes Received:
    1
    Trophy Points:
    0
    nice buddy this code is used for making the login form whre we need to use sessions to store user's information.
     
  8. virender.ets

    virender.ets Banned

    Joined:
    Jan 22, 2011
    Messages:
    26
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    http://www.rajasthancityguide.com/
    Hey buddy,
    You are writing code for login but what would happen if click on the back button through browser after login.
     
  9. tiwvinay

    tiwvinay New Member

    Joined:
    May 7, 2011
    Messages:
    18
    Likes Received:
    0
    Trophy Points:
    0
    thanks for give information in jsp
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice