Searching...
Monday, 1 October 2012

Cookies Session Tracking Example

22:17

Cookies

Cookie is a one session tracking solution it is a small bit of text sent to the client that can be read again later.
these are text files stored on the client computer and they are kept for various information tracking purpose. Java Servlets transparently supports HTTP cookies

How to use Cookies

Create a JSP page (see cookies for username)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<%
   String cookieName ="username";
   Cookie cookies[]= request.getCookies();
   Cookie myCookie =null;
   if(cookies !=null){
      for(int i =0; i < cookies.length; i++){
         if(cookies[i].getName().equals(cookieName)){
            myCookie = cookies[i];
            out.println(" Hi "+ myCookie.getValue()+"You are logged in");
            break;
         }
      }
   }
%>
</head>
<body>
<formname="input"action="/MyWebProject/MyCookiesServlet"
method="get">
User name: <inputtype="text"name="user"> Password: <input
type="text"name="pass"> <inputtype="submit"value="Submit">
</form>
</body>
</html>
Create a servlet to create Cookies for requests
package com.vinod.test;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

import javax.servlet.http.Cookie;

/**
 * Servlet implementation class CookiesServlet
 */
@WebServlet("/CookiesServlet")
public class CookiesServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
    *@see HttpServlet#HttpServlet()
    */
public CookiesServlet() {
super();
// TODO Auto-generated constructor stub
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<HTML><TITLE>HttpSession Example</TITLE><BODY>");
Cookie c = null;
String username = null;
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (int i = 0; i < cookies.length; i++) {
if (cookies[i].getName().equals("user")) {
username = cookies[i].getValue();
break;
}
}
}

if (username == null) {
c = new Cookie("username", request.getParameter("user"));
response.addCookie(c);
}

out.println("Your request Session created " + c.getValue());
out.close();
}

}

Run the jsp page and submit with user name and passoword
Run again the JSP (cookie.jsp) We can see the welcome message as it is already logged in.


0 comments: