Online Hibernate Training just in Rs. 1000

One week online training in Hibernate ORM framework. Learn Hibernate Framework and enhance your skills. Best Online Hibernate training at 90% discount. Join Now!! Offer is valid Only for limited Period!!! Online Training

Simplest Login and Logout example in JSP

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.


Simplest Login and Logout example in JSP


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.

The code of the program is given below:



<title>User Login</title>
<body Bgcolor ="#0099cc"><hr><hr>

<form method="POST" action="sessionAction.jsp">
  <p><b>UserName:</b> <input type="text" name="UserName" size="10"></p>
  <p><b>Password:</b> &nbsp;&nbsp;<input type="Password" name="Password" 
  <p><input type="submit" value="Submit" name="submit"><input type=
"reset" value="Reset" name="reset"></p><hr><hr>



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.*" %>
<%String str = request.getParameter("UserName");

session.setAttribute("UserName", request.getParameter("UserName"));%>
Welcome to <%= session.getAttribute( "UserName" ) %>
<% if (session.getAttribute("UserName").equals("")){%>
<a href="login.jsp"><b>Login </b></a>
<a href="logout.jsp"><b>Logout</b></a>

The function <%= session.getAttribute( "UserName" ) %> 
is used to retrieve the user name saved in the session.

   <%= session.getAttribute( "UserName" ) %>

The code of the program is given below:

<%@page import="java.util.*" %>

You have logged out. Plese
<a href="login.jsp"><b>Login</b></a>

The function <%session.invalidate();%>  is used to invalidate the user login session.



The output of the program is given below:

The output of the program is given below:

The output of the program is given below:


 Download Source Code


Share on Google+Share on Google+

Simplest Login and Logout example in JSP

Posted on: July 8, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Simplest Login and Logout example in JSP   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
Harish Vats
July 29, 2012

Even by using session.invalidate(), if we press back button the site gets loaded. please help,how to stop it.
August 23, 2012
Nice code...but....

Hi, This is a great and simple code. It works smoothly...but when i want to update,delete,add my table from sql its error..can you help me...
August 23, 2012
Need help

Can you help me it does not work if i update, delete and add.. <[email protected] import="java.util.*" %> <%String str = request.getParameter("userName"); <%session.setAttribute("UserName", request.getParameter("UserName"));%> Welcome to <%= session.getAttribute( "UserName" ) %> <% if (session.getAttribute("UserName").equals("")){%> <a href="login.jsp"><b>Login </b></a> <%} else{%> <a href="logout.jsp"><b>Logout</b></a> <%} %> <%@ page import="java.sql.*" %> <html> <head> <script language="javascript"> function editRecord(id){ var f=document.form; f.method="post"; f.action='edit.jsp?id='+id; f.submit(); } function deleteRecord(id){ var f=document.form; f.method="post"; f.action='delete.jsp?id='+id; f.submit(); } function addRecord(){ var f=document.form; f.method="post"; f.action='add.jsp'; f.submit(); } </script> </head> <body> <br><br> <form method="post" name="form"> <table border="1"> <tr><th>Title</th><th>Author</th><th>Price</th><th>Quantity</th></tr> <% Connection con = null; String url = "jdbc:mysql://localhost:3306/"; String db = "ebookshop"; String driver = "com.mysql.jdbc.Driver"; String userName ="root"; String password="root"; int sumcount=0; Statement st; try{ Class.forName(driver).newInstance(); con = DriverManager.getConnection(url+db,userName,password); String query = "select * from books"; st = con.createStatement(); ResultSet rs = st.executeQuery(query); %> <% while({ %> <tr><td><%=rs.getString(2)%></td> <td><%=rs.getString(3)%></td> <td><%=rs.getString(4)%></td> <td><%=rs.getString(5)%></td> <td><input type="button" name="edit" value="Edit" style="background-color:#49743D;font-weight:bold;color:#ffffff;" onclick="editRecord(<%=rs.getString(1)%>);" ></td> <td><input type="button" name="delete" value="Delete" style="background-color:#ff0000;font-weight:bold;color:#ffffff;" onclick="deleteRecord(<%=rs.getString(1)%>);" ></td> </tr> <% } %> <% } catch(Exception e){ e.printStackTrace(); } %> </table> <br> <input type="button" name="add" value="Add Record" style="background-color:#ff0000;font-weight:bold;color:#ffffff;" onclick="addRecord();" > </form> </body> </html>
Rupesh Ranjan Sahay
August 26, 2012

Your code has bug, when i press back button it should not allow us to go to the information page.It should go to the login page only. Please let me know if i m wrong.
October 7, 2012
about simplest login and logout jsp code

hi there i had executed this code it executes bt wen i add table and simple validation code for the text box in javascript then it will give me error 500 and executes properly. i'm using gooogle chrome & netbean frame work. plz, solve my problem & send me solution through email to my mail id . plzzzzzzzzzzzzz, i urgently need solution.
October 8, 2012
log out problem

Your code is working but after logging out from page. when i clicks on back button of browser i enters in my profile again.
June 24, 2013

thanks dude sharing the information its working for me logout code
January 24, 2014
Still session is there after logout also

Pls help me out how to invalidate the session once you logged out. it means once you logged out , the application should not allow you to go forward/backward untill you login.
April 26, 2014
problem in understanding session.invalidate()

In this code if i press back button from browser then I will be redirected to previous logout option. then how it defines that the session has been validated.