Deployment Descriptor of the Web Application

Chapter 4 - Session Management

    Deployment Descriptor of the Web Application

  • Deployment descriptor of the web application is:

    < ?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_3_0.xsd" id="WebApp_ID" version="3.0" >
      < display-name>ServletCookieExample < /display-name >
      < welcome-file-list &ht
        < welcome-file>login.html < /welcome-file >
      < /welcome-file-list >
    < /web-app >

    ServletCookieExample

    Welcome page of our application is login.html will get authentication details from user:

    < !DOCTYPE html >
    < html >
    < head >
    < meta charset="US-ASCII" >
    < title > Login Page < /title >
    < /head >
    < body >
    < form action="LoginServlet" method="post" >
    Username: < input type="text" name="user" >
    < br >
    Password: < input type="password" name="pwd" >
    < br >
    < input type="submit" value="Login" >
    < /form >
    < /body >
    < /html >
    

    The LoginServlet that takes the login request:

    package com.journaldev.servlet.session;
    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.Cookie;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * Servlet implementation class LoginServlet
     */
    @WebServlet("/LoginServlet")
    public class LoginServlet extends HttpServlet {
     private static final long serialVersionUID = 1L;
     private final String userID = "Pankaj";
     private final String password = "journaldev";
    
     protected void doPost(HttpServletRequest request,
       HttpServletResponse response) throws ServletException, IOException {
    
      // get request parameters for userID and password
      String user = request.getParameter("user");
      String pwd = request.getParameter("pwd");
      
      if(userID.equals(user) && password.equals(pwd)){
       Cookie loginCookie = new Cookie("user",user);
       //setting cookie to expiry in 30 mins
       loginCookie.setMaxAge(30*60);
       response.addCookie(loginCookie);
       response.sendRedirect("LoginSuccess.jsp");
      }else{
       RequestDispatcher rd = getServletContext().getRequestDispatcher("/login.html");
       PrintWriter out= response.getWriter();
       out.println("< font color=red>Either user name or password is wrong.< /font >");

    Either user name or password is wrong.

    ");
       rd.include(request, response);
      }
    
     }
    
    }
    

    cookie are setting to the response and then forwarding it to LoginSuccess.jsp, this cookie will be used to track the session.

    < %@ page language="java" contentType="text/html; charset=US-ASCII"
        pageEncoding="US-ASCII"%  >
    < !DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
    < html >
    < head >
    < meta http-equiv="Content-Type" content="text/html; charset=US-ASCII" >
    < title > Login Success Page < /title >
    < /head >
    < body >
    < %
    String userName = null;
    Cookie[] cookies = request.getCookies();
    if(cookies !=null){
    for(Cookie cookie : cookies){
     if(cookie.getName().equals("user")) userName = cookie.getValue();
    }
    }
    if(userName == null) response.sendRedirect("login.html");
    %>
    < h3>Hi <%=username>, Login successful.< /h3 > < br > < form action="LogoutServlet" method="post" > < input type="submit" value="Logout" > < /form > < /body > < /html > 
    package com.journaldev.servlet.session;
    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.Cookie;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    
    /**
     * Servlet implementation class LogoutServlet
     */
    @WebServlet("/LogoutServlet")
    public class LogoutServlet extends HttpServlet {
     private static final long serialVersionUID = 1L;
           
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         response.setContentType("text/html");
         Cookie loginCookie = null;
         Cookie[] cookies = request.getCookies();
         if(cookies != null){
         for(Cookie cookie : cookies){
          if(cookie.getName().equals("user")){
           loginCookie = cookie;
           break;
          }
         }
         }
         if(loginCookie != null){
          loginCookie.setMaxAge(0);
             response.addCookie(loginCookie);
         }
         response.sendRedirect("login.html");
        }
    
    }
    

© 2015 by Learncertification All Rights Reserved. The certification names are the trademarks of their respective owners. Terms & Privacy Policy