0% found this document useful (0 votes)
87 views

Session Management Using Servlet Example#1 (NO JDBC) : 1. Index - HTML

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
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
87 views

Session Management Using Servlet Example#1 (NO JDBC) : 1. Index - HTML

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
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

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

Session Management using Servlet


Example#1 (NO JDBC)
1. index.html:-
<!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 com.app;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;

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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

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

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

resp.setContentType("text/html");

//1. read form data


String un=req.getParameter("un");
String pwd=req.getParameter("pwd");

//2. verify and Dispatch


if("admin".equals(un) && "sathya".equals(pwd)) {
//if valid create new session
HttpSession ses=req.getSession();
//set data un=admin
ses.setAttribute("un", un);
//goto ProfileServlet
RequestDispatcher
rd=req.getRequestDispatcher("/profile");
rd.forward(req, resp);
}else {
//invalid data
//show error message
PrintWriter out=resp.getWriter();
out.println("Invalid Un/pwd entered");
//goto index.html
RequestDispatcher
rd=req.getRequestDispatcher("index.html");
rd.include(req, resp);

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

3. Profile Servlet:-
package com.app;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

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

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

//read old session


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

HttpSession ses=req.getSession(false);
//read attribute un
Object ob=ses.getAttribute("un");
//downcast
String s=(String)ob;
//print welcome message
PrintWriter out=resp.getWriter();
out.println("Welcome to :"+s);
out.println("<a href='logout'>Logout</a>");

4. LogoutServlet:-
package com.app;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@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]

resp.setContentType("text/html");

//read old session


HttpSession ses=req.getSession(false);
//remove session
ses.invalidate();
//print message
PrintWriter out=resp.getWriter();
out.println("Logout success!!");
//goto Login Page
RequestDispatcher
rd=req.getRequestDispatcher("index.html");
rd.include(req, resp);
}

5|P ag e

You might also like