Servlets - SPLessons

Servlet Credential Cookies

Home > Lesson > Chapter 20
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Servlet Credential Cookies

Servlet Credential Cookies

shape Description

Servlet Credential Cookies, Cookies are allocated memory at the client side as a textual information by remembering client information throughout the session. The servlet programs of web application create cookies on the server side, but these cookies come to the client along with the response and allocate memory at the client side. The first request cookies are created and next request, cookies go back to the web application along with request given by the client.When user sign out from the browser cookies will be deleted automatically. The following are the some imporatnat methods of cookies.
Methods Description
getComment() To get back the comment describing the purpose of this cookie.
setComment() It is used to set cookie description at server side.
getName() To give back the name of the cookie.
getDomain() To get the domain of the cookie.
getValue() To return the value of the cookie as string.
To know more details of cookies Click Here

shape Example

Servlet Credential Cookies - Following are the required files, where created three links profile, login, logout. The client can not see the profile until log in, in case if the client is logged out cookies will be deleted then the client has to re-enter his credentials. LoginServlet.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 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("12345")){ out.print("You are successfully logged in!"); out.print("<br>Welcome, "+name); Cookie ck=new Cookie("name",name); response.addCookie(ck); }else{ out.print("sorry, username or password error!"); request.getRequestDispatcher("login.html").include(request, response); } out.close(); } } [/java] A RequestDispatcher is an extremely important JavaSW class that allows for 'including' content in a request/response or 'forwarding' a request/response to a resource. The getWriter() Returns a PrintWriter object that can send character text to the client. PrintWriter prints text data to a character stream. LogoutServlet.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 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); Cookie ck=new Cookie("name",""); ck.setMaxAge(0); response.addCookie(ck); out.print("you are successfully logged out!"); } } [/java] This is the file to logout page, when click on logout page link.html page will be invoked. ProfileServlet.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 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); Cookie ck[]=request.getCookies(); if(ck!=null){ String name=ck[0].getValue(); if(!name.equals("")||name!=null){ out.print("<b>Welcome to Profile</b>"); out.print("<br>Welcome, "+name); } }else{ out.print("Please login first"); request.getRequestDispatcher("login.html").include(request, response); } out.close(); } } [/java] This is the page for profile, The getWriter() Returns a PrintWriter object that can send character text to the client. link.html file will have the profile information. web.xml [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>[/xml] Make sure that servlet name should be same on various classes where the developer used three class files. login.html [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> [/html] Here created two text boxes one is Name and another one is Password and also created login button. link.html [html] <a href="login.html">Login</a> | <a href="LogoutServlet">Logout</a> | <a href="ProfileServlet">Profile</a> <hr> [/html] index.html [html]<!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Servlet Login Example</title> </head> <body> <h1><center><b>This is the Log In Example to describe cookies concept</b></center></h1> <a href="login.html">Login</a>| <a href="LogoutServlet">Logout</a>| <a href="ProfileServlet">Profile</a> </body> </html>[/html] Output When the code complied following page will be displayed on the server. By clicking on login following page will be displayed. When clicked on login following message will be displayed. When clicked on logout following message will be displayed. When clicked on profile following message will be displayed.

Summary

shape Key Points

  • Cookie can be defined as small amount of data.
  • When user logged out cookies will be deleted.
  • Servlet Credential Cookies - If cookies are disabled in browser then it will not work.