Skip to content
Related Articles

Related Articles

Servlet – Credential Cookies

View Discussion
Improve Article
Save Article
Like Article
  • Difficulty Level : Expert
  • Last Updated : 25 Feb, 2022

A cookie is a type of data that is kept on the client’s computer. In order to detect recurring users, you must go through three phases.

  1. A collection of cookies is sent to the browser by the server script. Name, age, or identity number, for example.
  2. This information is saved on the local system by the browser for future use.
  3. When the browser submits a request to the webserver the following time, it transmits the cookies information to the server, which the server uses to identify the user.

We’ll use servlet cookies to develop a login and logout example here. The following files were developed for this application.

  1. index.html
  2. link.html
  3. login.html
  4. ServletLogin.java
  5. ServletLogout.java
  6. ServletProfile.java
  7. web.xml

Example

The project structures will look like this.

index.html:

HTML




<!DOCTYPE html>  
<html>  
<head>  
<meta charset="ISO-8859-1">  
<title>GeeksForgeeks Credential Cookies Example</title>  
</head>  
<body>  
    
  <h1>Welcome to GeeksForGeeks</h1>  
  <a href="login.html">Login</a>|  
  <a href="ServletLogout">Logout</a>|  
  <a href="ServletProfile">Profile</a>  
    
</body>  


link.html:

HTML




<a href="login.html">Login</a>  
<a href="ServletLogout">Logout</a>   
<a href="ServletProfile">Profile</a>  
<hr>  


login.html:

HTML




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


ServletLogin.java:

Java




import java.io.IOException;  
import java.io.PrintWriter;  
import javax.servlet.ServletException;  
import javax.servlet.http.Cookie;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
public class ServletLogin 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("geeksforgeeks")){  
            out.print("You are successfully logged in to GFG!");  
            out.print("<br>Welcome, "+name);  
                
            Cookie ck=new Cookie("name",name);  
            response.addCookie(ck);  
        }else{  
            out.print("sorry, username or password is wrong");  
            request.getRequestDispatcher("login.html").include(request, response);  
        }  
        out.close();  
    }    
}


ServletLogout.java:

Java




import java.io.IOException;  
import java.io.PrintWriter;  
import javax.servlet.ServletException;  
import javax.servlet.http.Cookie;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
public class ServletLogout 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);  
            
        Cookie ck=new Cookie("name","");  
        ck.setMaxAge(0);  
        response.addCookie(ck);  
            
        out.print("you are successfully logged out!");  
    }  
}


ServletProfile.java:

Java




import java.io.IOException;  
import java.io.PrintWriter;  
import javax.servlet.ServletException;  
import javax.servlet.http.Cookie;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
public class ServletProfile 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);  
            
        Cookie ck[]=request.getCookies();  
        if(ck!=null){  
         String name=ck[0].getValue();  
        if(!name.equals("")||name!=null){  
            out.print("<b>Welcome to GfG Profile</b>");  
            out.print("<br>Welcome, "+name);  
        }  
        }else{  
            out.print("Login first");  
            request.getRequestDispatcher("login.html").include(request, response);  
        }  
        out.close();  
    }  
}


web.xml:

XML




<web-app>   
  <servlet>  
    <description></description>  
    <display-name>ServletLogin</display-name>  
    <servlet-name>ServletLogin</servlet-name>  
    <servlet-class>ServletLogin</servlet-class>  
  </servlet>  
  <servlet-mapping>  
    <servlet-name>ServletLogin</servlet-name>  
    <url-pattern>/ServletLogin</url-pattern>  
  </servlet-mapping>  
  <servlet>  
    <description></description>  
    <display-name>ServletProfile</display-name>  
    <servlet-name>ServletProfile</servlet-name>  
    <servlet-class>ServletProfile</servlet-class>  
  </servlet>  
  <servlet-mapping>  
    <servlet-name>ServletProfile</servlet-name>  
    <url-pattern>/ServletProfile</url-pattern>  
  </servlet-mapping>  
  <servlet>  
    <description></description>  
    <display-name>ServletLogout</display-name>  
    <servlet-name>ServletLogout</servlet-name>  
    <servlet-class>ServletLogout</servlet-class>  
  </servlet>  
  <servlet-mapping>  
    <servlet-name>ServletLogout</servlet-name>  
    <url-pattern>/ServletLogout</url-pattern>  
  </servlet-mapping>  
</web-app>


Output:

Run the index.html file and you will get the following output

click on profile without login

now click on the login link and type username and password

This screen will be shown after successful login

Now click on profile again after login you can see the profile page now

After clicking on the logout this screen will be shown


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!