Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

Cookies Login and Logout


A cookie is a kind of information that is stored at client side.

In the previous page, we learned a lot about cookie e.g. how to create cookie, how to delete cookie, how to get cookie etc.

Here, we are going to create a login and logout example using servlet cookies.

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

File: index.html

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset=“ISO-8859-1”>
  5. <title>Servlet Login Example</title>
  6. </head>
  7. <body>
  8. <h1>Welcome to Login App by Cookie</h1>
  9. <a href=“login.html”>Login</a>|
  10. <a href=“LogoutServlet”>Logout</a>|
  11. <a href=“ProfileServlet”>Profile</a>
  12. </body>
  13. </html>

File: link.html

  1. <a href=“login.html”>Login</a> |
  2. <a href=“LogoutServlet”>Logout</a> |
  3. <a href=“ProfileServlet”>Profile</a>
  4. <hr>

File: login.html

  1. <form action=“LoginServlet” method=“post”>
  2. Name:<input type=“text” name=“name”><br>
  3. Password:<input type=“password” name=“password”><br>
  4. <input type=“submit” value=“login”>
  5. </form>

File: LoginServlet.java

  1. package com.Kreationnext;
  2. import java.io.IOException;
  3. import java.io.PrintWriter;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.http.Cookie;
  6. import javax.servlet.http.HttpServlet;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. public class LoginServlet extends HttpServlet {
  10.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  11.                            throws ServletException, IOException {
  12.         response.setContentType(“text/html”);
  13.         PrintWriter out=response.getWriter();
  14.         request.getRequestDispatcher(“link.html”).include(request, response);
  15.         String name=request.getParameter(“name”);
  16.         String password=request.getParameter(“password”);
  17.         if(password.equals(“admin123”)){
  18.             out.print(“You are successfully logged in!”);
  19.             out.print(“<br>Welcome, “+name);
  20.             Cookie ck=new Cookie(“name”,name);
  21.             response.addCookie(ck);
  22.         }else{
  23.             out.print(“sorry, username or password error!”);
  24.             request.getRequestDispatcher(“login.html”).include(request, response);
  25.         }
  26.         out.close();
  27.     }
  28. }

File: LogoutServlet.java

  1. package com.Kreationnext;
  2. import java.io.IOException;
  3. import java.io.PrintWriter;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.http.Cookie;
  6. import javax.servlet.http.HttpServlet;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. public class LogoutServlet extends HttpServlet {
  10.     protected void doGet(HttpServletRequest request, HttpServletResponse response)
  11.                         throws ServletException, IOException {
  12.         response.setContentType(“text/html”);
  13.         PrintWriter out=response.getWriter();
  14.         request.getRequestDispatcher(“link.html”).include(request, response);
  15.         Cookie ck=new Cookie(“name”,“”);
  16.         ck.setMaxAge(0);
  17.         response.addCookie(ck);
  18.         out.print(“you are successfully logged out!”);
  19.     }
  20. }

File: ProfileServlet.java

  1. package com.Kreationnext;
  2. import java.io.IOException;
  3. import java.io.PrintWriter;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.http.Cookie;
  6. import javax.servlet.http.HttpServlet;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. public class ProfileServlet extends HttpServlet {
  10.     protected void doGet(HttpServletRequest request, HttpServletResponse response)
  11.                           throws ServletException, IOException {
  12.         response.setContentType(“text/html”);
  13.         PrintWriter out=response.getWriter();
  14.         request.getRequestDispatcher(“link.html”).include(request, response);
  15.         Cookie ck[]=request.getCookies();
  16.         if(ck!=null){
  17.          String name=ck[0].getValue();
  18.         if(!name.equals(“”)||name!=null){
  19.             out.print(“<b>Welcome to Profile</b>”);
  20.             out.print(“<br>Welcome, “+name);
  21.         }
  22.         }else{
  23.             out.print(“Please login first”);
  24.             request.getRequestDispatcher(“login.html”).include(request, response);
  25.         }
  26.         out.close();
  27.     }
  28. }

File: web.xml

  1. <?xml version=“1.0” encoding=“UTF-8”?>
  2. <web-app xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
  3. xmlns=“http://java.sun.com/xml/ns/javaee” xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee
  4. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd” id=“WebApp_ID” version=“2.5”>
  5.   <servlet>
  6.     <description></description>
  7.     <display-name>LoginServlet</display-name>
  8.     <servlet-name>LoginServlet</servlet-name>
  9.     <servlet-class>com.Kreationnext.LoginServlet</servlet-class>
  10.   </servlet>
  11.   <servlet-mapping>
  12.     <servlet-name>LoginServlet</servlet-name>
  13.     <url-pattern>/LoginServlet</url-pattern>
  14.   </servlet-mapping>
  15.   <servlet>
  16.     <description></description>
  17.     <display-name>ProfileServlet</display-name>
  18.     <servlet-name>ProfileServlet</servlet-name>
  19.     <servlet-class>com.Kreationnext.ProfileServlet</servlet-class>
  20.   </servlet>
  21.   <servlet-mapping>
  22.     <servlet-name>ProfileServlet</servlet-name>
  23.     <url-pattern>/ProfileServlet</url-pattern>
  24.   </servlet-mapping>
  25.   <servlet>
  26.     <description></description>
  27.     <display-name>LogoutServlet</display-name>
  28.     <servlet-name>LogoutServlet</servlet-name>
  29.     <servlet-class>com.Kreationnext.LogoutServlet</servlet-class>
  30.   </servlet>
  31.   <servlet-mapping>
  32.     <servlet-name>LogoutServlet</servlet-name>
  33.     <url-pattern>/LogoutServlet</url-pattern>
  34.   </servlet-mapping>
  35. </web-app>

Output

cookieloginoutput1.jpg

cookieloginoutput2.jpg

cookieloginoutput3.jpg

cookieloginoutput4.jpg

cookieloginoutput5.jpg

If again you click on the profile link, you need to login first.

Comments

comments