Custom cookie example:

Custom cookie example:
In the example in one servlet we set the username as the cookie in response. And in all the other servlets we get the cookie from the request and view the user name. The life time of cookies is 20 minutes.

Required Files:
Demo1.java – This http servlet is to set the cookies
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 demo1 extends HttpServlet{
public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.println("<center><h1><font color='red'>Hello Client..,</font></h1></center>");
String name=request.getParameter("username");
Cookie cookie=new Cookie("username",name);
cookie.setMaxAge(20*60);
response.addCookie(cookie);
out.println("<h1>cookie is set<h1>");}}

demo2.java – This http servlet is to get the cookie value
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 demo2 extends HttpServlet{
public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
response.setContentType("text/html");
PrintWriter out=response.getWriter();
Cookie[] cookies=request.getCookies();
if(cookies!=null){
for(int i=0;i<cookies.length;i++){
Cookie cookie=cookies[i];
if(cookie.getName().equals("username")){
String username=cookie.getValue();
out.println("<h1>hello "+username +"</h1>");
break;
}}}}}
Web.xml- The DD file to configure the servlets
<web-app>
<servlet>
<servlet-name>demo1</servlet-name>
<servlet-class>demo1</servlet-class>
</servlet>
<servlet>
<servlet-name>demo2</servlet-name>
<servlet-class>demo2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>demo1</servlet-name>
<url-pattern>/demo1.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>demo2</servlet-name>
<url-pattern>/demo2.do</url-pattern>
</servlet-mapping>
<web-app>
Index.html- The home page wich has two buttons the first requests to the demo1 servlet and second requests to the demo2 servlet.
<html>
<body>
<form action="./demo1.do" method="GET">
Enter Username:<input type="text" name="username"/>
<input type="submit" value="click me"/>
</form><br>
<form action="./demo2.do" method="get">
<input type="submit" value="click">
</form>
</body>
</html>


Here if we click the “clickme” button the request goes to demo1 servlet and it sets the cookie in the response. When again request to demo2 servlet and it uses the previous cookie and get the user name.
You close your browser and open again and click the second button you will get the name. This session is valid for 20 minutes. After 20minutes again you open your browser click the second button and request to the second servlet but tiis time you will not get the name because the session does not exist any more.

No comments:

Post a Comment