Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
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:

<html>

<head>

<title>User Login</title>
</head>
<br>
<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" size="10"></p>
  <p><input type="submit" value="Submit" name="submit"><input type=
"reset" value="Reset" name="reset"></p><hr><hr>
</form>

</body>

</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.*" %>
<%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>
<%}
%>


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.*" %>

<%session.invalidate();%>
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.

 
    <%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:

 

 Download Source Code

                         

» View all related tutorials
Related Tags: c database security web com rest ant website data io connection user password word uri name using this tab check

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.