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
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..
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.
nice buddy this code is used for making the login form whre we need to use sessions to store user's information.
Hey buddy, You are writing code for login but what would happen if click on the back button through browser after login.