0% found this document useful (0 votes)
36 views5 pages

Java Servlet Session Management Example

This document describes how to implement session management in a Java web application using servlets without JDBC. It includes 4 servlets - LoginCheckServlet verifies login credentials and creates a session, ProfileServlet retrieves and displays the user's name from the session, and LogoutServlet invalidates the session. Index.html contains the login form that submits to LoginCheckServlet, which then forwards to ProfileServlet on success or includes Index.html on failure. LogoutServlet invalidates the session and includes Index.html to redirect to the login page.

Uploaded by

Arya Dewan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views5 pages

Java Servlet Session Management Example

This document describes how to implement session management in a Java web application using servlets without JDBC. It includes 4 servlets - LoginCheckServlet verifies login credentials and creates a session, ProfileServlet retrieves and displays the user's name from the session, and LogoutServlet invalidates the session. Index.html contains the login form that submits to LoginCheckServlet, which then forwards to ProfileServlet on success or includes Index.html on failure. LogoutServlet invalidates the session and includes Index.html to redirect to the login page.

Uploaded by

Arya Dewan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

-by RAGHU SIR [SATHYA TECHNOLOGIES , AMEERPET, HYDERABAD]

Session Management using Servlet


Example#1 (NO JDBC)
1. [Link]:-
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h3>Welcome to User Login Page</h3>
<form action="login" method="post">
<pre>
Username : <input type="text" name="un"/>
Password : <input type="password" name="pwd"/>
<input type="submit" value="Login"/>
</pre>
</form>
</body>
</html>

2. LoginCheckServlet:-
package [Link];

import [Link];
import [Link];

import [Link];
import [Link];
import [Link];
import [Link];

1|P ag e
-by RAGHU SIR [SATHYA TECHNOLOGIES , AMEERPET, HYDERABAD]

import [Link];
import [Link];
import [Link];

@WebServlet("/login")
public class LoginCheckServlet
extends HttpServlet
{

@Override
protected void doPost(HttpServletRequest req,
HttpServletResponse resp) throws ServletException,
IOException {

[Link]("text/html");

//1. read form data


String un=[Link]("un");
String pwd=[Link]("pwd");

//2. verify and Dispatch


if("admin".equals(un) && "sathya".equals(pwd)) {
//if valid create new session
HttpSession ses=[Link]();
//set data un=admin
[Link]("un", un);
//goto ProfileServlet
RequestDispatcher
rd=[Link]("/profile");
[Link](req, resp);
}else {
//invalid data
//show error message
PrintWriter out=[Link]();
[Link]("Invalid Un/pwd entered");
//goto [Link]
RequestDispatcher
rd=[Link]("[Link]");
[Link](req, resp);

2|P ag e
-by RAGHU SIR [SATHYA TECHNOLOGIES , AMEERPET, HYDERABAD]

3. Profile Servlet:-
package [Link];

import [Link];
import [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

@WebServlet("/profile")
public class ProfileServlet
extends HttpServlet
{

@Override
protected void doPost(HttpServletRequest req,
HttpServletResponse resp) throws ServletException,
IOException {
[Link]("text/html");

//read old session


3|P ag e
-by RAGHU SIR [SATHYA TECHNOLOGIES , AMEERPET, HYDERABAD]

HttpSession ses=[Link](false);
//read attribute un
Object ob=[Link]("un");
//downcast
String s=(String)ob;
//print welcome message
PrintWriter out=[Link]();
[Link]("Welcome to :"+s);
[Link]("<a href='logout'>Logout</a>");

4. LogoutServlet:-
package [Link];

import [Link];
import [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

@WebServlet("/logout")
public class LogoutServlet
extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest req,
HttpServletResponse resp) throws ServletException,
IOException {
4|P ag e
-by RAGHU SIR [SATHYA TECHNOLOGIES , AMEERPET, HYDERABAD]

[Link]("text/html");

//read old session


HttpSession ses=[Link](false);
//remove session
[Link]();
//print message
PrintWriter out=[Link]();
[Link]("Logout success!!");
//goto Login Page
RequestDispatcher
rd=[Link]("[Link]");
[Link](req, resp);
}

5|P ag e

You might also like