Monday, 10 September 2012

Session tracking : counting the number of user visits to a webpage


The directory structure of the web application would be like this:

index.jsp



login.java


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;
import javax.servlet.http.HttpSession;


@WebServlet(name="login", urlPatterns={"/login"})
public class login extends HttpServlet {

    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        try {

            String name= request.getParameter("text1");
            String pass= request.getParameter("text2");
            if(name.equalsIgnoreCase(pass))
            { 

            response.addCookie(new Cookie("name",name));
            
          HttpSession session = request.getSession(true);
            
           Integer count = (Integer)session.getAttribute("count");
          if (count == null)
          count = new Integer(1);
          else
          count = new Integer(count.intValue() + 1);
          session.setAttribute("count", count);
           out.println("Total visits to this page " + count +
          ((count.intValue() == 1) ? " time." : " times."));
          
          out.println("\n\n\n\n");  
          
           out.println("<h1>Welcome "+name+"</h1><br>");
           String s="Click here to move to next page";
            out.println("<a href='http://localhost:8080/withTheHelpOfCookies/newjsp.jsp'>"+s+"</a>");
            }


        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}

newjsp.jsp







No comments:

Post a Comment