This JSP example shows you how to login and logout the session between JSP pages. JSP provide an implicit object that is session in which use to save the data specific by the particular user. Now in this tutorial we are create application takes an user login from that having user name and password, in this example saves an user session that invalidate with session.invalidate() function. We will display the saved data to the user in another page. Fallowing is the code of the simple JSP file is login.jsp that takes the input from user.
<html>
|
The above login page prompts the user to enter his or her name. Once the user clicks on the submit
button that time sessionAction.jsp is called. The JSP sessionAction.jsp retrieves the user name from request attributes and saves into the user session using the function:
| session.setAttribute("UserName", request.getParameter("UserName")); |
The above Action JSP page saves the UserName into the session object and displays a link to next pages that is sessionAction.jsp. When user clicks on the "Next Page to view session value" link, the JSP page sessionAction.jsp displays the user name to the user. Here is the code of sessionAction.jsp:
Here is the code of sessionAction.jsp:
<%@page import="java.util.*" %>
|
The function <%= session.getAttribute( "UserName" ) %>
is used to retrieve the user name saved in the session.
| <%= session.getAttribute( "UserName" ) %> |
<%@page import="java.util.*" %>
|
| <%session.invalidate();%> |
The output of the program is given below:

The output of the program is given below:

The output of the program is given below:

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: Simplest Login and Logout example in JSP View All Comments
Post your Comment