0% found this document useful (0 votes)
384 views8 pages

Login-Logout Using Session - Servlets

This document describes a login and logout application created using servlets and HTTP sessions. The application contains an index page with links to login, logout, and profile pages. The LoginServlet authenticates the user and stores their name in the HTTP session. The ProfileServlet displays the user's name if they are logged in, and the LogoutServlet invalidates the session on logout. Files include HTML pages, servlet classes, and a web.xml configuration file.

Uploaded by

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

Login-Logout Using Session - Servlets

This document describes a login and logout application created using servlets and HTTP sessions. The application contains an index page with links to login, logout, and profile pages. The LoginServlet authenticates the user and stores their name in the HTTP session. The ProfileServlet displays the user's name if they are logged in, and the LogoutServlet invalidates the session on logout. Files include HTML pages, servlet classes, and a web.xml configuration file.

Uploaded by

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

Servlet HttpSession Login and Logout Example

Here, we are going to create a real world login and logout application without using
database code. We are assuming that password is admin123.

In this example, we are creating 3 links: login, logout and profile. User can't go to profile
page until he/she is logged in. If user is logged out, he need to login again to visit profile.

In this application, we have created following files.

1. index.html

2. link.html

3. login.html

4. LoginServlet.java

5. LogoutServlet.java

6. ProfileServlet.java

7. web.xml

index.html

 
<html>  
<head>  
<title>Servlet Login Example</title>  
</head>  
<body>  
  
<h1>Login App using HttpSession</h1>  
<a href="login.html">Login</a>|  
<a href="LogoutServlet">Logout</a>|  
<a href="ProfileServlet">Profile</a>  
  </body>  
</html>  
File: link.html
<a href="login.html">Login</a> |  
<a href="LogoutServlet">Logout</a> |  
<a href="ProfileServlet">Profile</a>  
 

File: login.html
<form action="LoginServlet" method="post">  
Name:<input type="text" name="name"><br>  
Password:<input type="password" name="password"><br>  
<input type="submit" value="login">  
</form>  

File: LoginServlet.java

import java.io.*;
import javax.servlet.*;  
import javax.servlet.http.*;

public class LoginServlet extends HttpServlet {  
    protected void doPost(HttpServletRequest request, HttpServletResponse response)  
                    throws ServletException, IOException {  
        response.setContentType("text/html");  
        PrintWriter out=response.getWriter();  
        request.getRequestDispatcher("link.html").include(request, response);  
          
        String name=request.getParameter("name");  
        String password=request.getParameter("password");  
          
        if(password.equals("admin123")){  
        out.print("Welcome, "+name);  
        HttpSession session=request.getSession();  
        session.setAttribute("name",name);  
        }  
        else{  
            out.print("Sorry, username or password error!");  
            request.getRequestDispatcher("login.html").include(request, response);  
        }  
        out.close();  
    }  
}  

File: LogoutServlet.java

import java.io.IOException;  
import java.io.PrintWriter;  
  
import javax.servlet.ServletException;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
import javax.servlet.http.HttpSession;  
public class LogoutServlet extends HttpServlet {  
        protected void doGet(HttpServletRequest request, HttpServletResponse response)  
                                throws ServletException, IOException {  
            response.setContentType("text/html");  
            PrintWriter out=response.getWriter();  
              
            request.getRequestDispatcher("link.html").include(request, response);  
              
            HttpSession session=request.getSession();  
            session.invalidate();  
              
            out.print("You are successfully logged out!");  
              
            out.close();  
    }  
}  
File: ProfileServlet.java

import java.io.IOException;  
import java.io.PrintWriter;  
import javax.servlet.ServletException;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
import javax.servlet.http.HttpSession;  
public class ProfileServlet extends HttpServlet {  
    protected void doGet(HttpServletRequest request, HttpServletResponse response)  
                      throws ServletException, IOException {  
        response.setContentType("text/html");  
        PrintWriter out=response.getWriter();  
        request.getRequestDispatcher("link.html").include(request, response);  
          
        HttpSession session=request.getSession(false);  
        if(session!=null){  
        String name=(String)session.getAttribute("name");  
          
        out.print("Hello, "+name+" Welcome to Profile");  
        }  
        else{  
            out.println("Please login first");  
out.print(“You can not visit profile page directly”);
            request.getRequestDispatcher("login.html").include(request, response);  
        }  
        out.close();  
    }  
}  

File: web.xml

<?xml version="1.0" encoding="UTF-8"?>  
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/
javaee   
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">  
    
  <servlet>  
    <description></description>  
    <display-name>LoginServlet</display-name>  
    <servlet-name>LoginServlet</servlet-name>  
    <servlet-class>LoginServlet</servlet-class>  
  </servlet>  
  <servlet-mapping>  
    <servlet-name>LoginServlet</servlet-name>  
    <url-pattern>/LoginServlet</url-pattern>  
  </servlet-mapping>  
  <servlet>  
    <description></description>  
    <display-name>ProfileServlet</display-name>  
    <servlet-name>ProfileServlet</servlet-name>  
    <servlet-class>ProfileServlet</servlet-class>  
  </servlet>  
  <servlet-mapping>  
    <servlet-name>ProfileServlet</servlet-name>  
    <url-pattern>/ProfileServlet</url-pattern>  
  </servlet-mapping>  
  <servlet>  
    <description></description>  
    <display-name>LogoutServlet</display-name>  
    <servlet-name>LogoutServlet</servlet-name>  
    <servlet-class>LogoutServlet</servlet-class>  
  </servlet>  
  <servlet-mapping>  
    <servlet-name>LogoutServlet</servlet-name>  
    <url-pattern>/LogoutServlet</url-pattern>  
  </servlet-mapping>  
</web-app>  

You might also like