Thursday, 9 October 2014

Using Cookies in Servlets to count the hits of a particular website



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

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 {
      private static final long serialVersionUID = 1L;
    int visitNumber=0;
    /**
     * @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
                   */
                  visitNumber=0;
                 
                  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)
            {
                  Cookie c=new Cookie("login",request.getParameter("username")+"-"+request.getParameter("password"));
                  c.setMaxAge(60*60*24*7);
                  response.addCookie(c);
                  pw.println("cookie created successfully");
            }
            else
            {
                  visitNumber++;
                  String str= "<html>"+
                                          "<body>"+
                                                "<h1>"+
                                                      "Cookie is created and the user has logged in automatically"+
                                                      "<br>"+
                                                      "<br>"+
                                                      "Visit Number = "+visitNumber+
                                                "</h1>"+
                                          "</body>"+
                                    "</html>";
                  pw.println(str);
            }          
      }

      /**
       * @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