Friday, 10 October 2014

Counting the number of visits of a particular user using Cookies in Servlet



/*
 * Understanding the basic creation of cookie in Servlet
 * setting the max age of the cookie
 * Insreting the cookie into the response header
 */

/*
 * this program lets a user to login when he visits the site for the first time
 * on his subsequent visits the site displays the visit number of the user untill the user clears the cookie.
 * Note that for login this program dont use any validation or authentication,
 * For the first visit of the user it creates a cookie and retrieves the information from that cookie.
 */

package CookieLoginPkg;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class CookieLoginCls
 */
@WebServlet("/CookieLoginCls")
public class CookieLoginCls extends HttpServlet
{
      Cookie c=null;
     
      private static final long serialVersionUID = 1L;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CookieLoginCls() {
        super();
        // TODO Auto-generated constructor stub
    }

      /**
       * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
       */
      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
      {
            PrintWriter pw=response.getWriter();
            Cookie arr[]=request.getCookies();
            //pw.println(arr);
            if(arr==null && request.getParameter("username")==null)
            {
                  /*
                   * here we need to give the user the login page
                   */
                 
                  String str= "<html>"+
                                          "<body>"+
           
                                                "<h1><marquee>Login Form</marquee></h1>"+
                                                "<form action=\"CookieLoginCls\">"+
                                                "<table align=\"center\">"+
                                                      "<tr>"+
                                                            "<td>Login</td>"+
                                                            "<td><input type=\"text\" name=\"username\"></td>"+
                                                      "</tr>"+
                                                      "<tr>"+
                                                            "<td>Passoword</td>"+
                                                            "<td><input type=\"password\" name=\"password\"></td>"+
                                                      "</tr>"+
                                                      "<tr>"+
                                                            "<td ><input type=\"submit\"></td>"+
                                                      "</tr>"+
                                                "</table>"+
                                               
                                                "</form>"+
                                          "</body>"+
                                    "</html>";
                  pw.println(str);
            }
            else if(request.getParameter("username")!=null)
            {
                  c=new Cookie("login",request.getParameter("username")+"-"+request.getParameter("password"));
                  c.setMaxAge(60*60*24*7);
                  response.addCookie(c);

                  c=new Cookie("cnt","1");
                  c.setMaxAge(60*60*24*7);
                  response.addCookie(c);
                 
                  pw.println("cookie created successfully");
                 
            }
            else
            {
                  String val="";
                  for(Cookie co:arr)
                        if(co.getName().equals("cnt"))
                              val=c.getValue();
                 
                  String str= "<html>"+
                                          "<body>"+
                                                "<h1>"+
                                                      "Cookie is created and the user has logged in automatically"+
                                                      "<br>"+
                                                      "<br>"+
                                                      "Visit Number = "+val+
                                                "</h1>"+
                                          "</body>"+
                                    "</html>";
                  pw.println(str);
                 
                  c=new Cookie("cnt",""+(Integer.parseInt(val)+1));
                  c.setMaxAge(60*60*24*7);
                  response.addCookie(c);
                 
            }          
      }

      /**
       * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
       */
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
      }
}

No comments:

Post a Comment