Monday, 10 September 2012

Session tracking using cookies : shopping cart example

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));
          
           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>");
           out.println("<html>");
           out.println("<head>");
            out.println("<title>Servlet login</title>");
            out.println("</head>");
            out.println("<body>");

            out.println("<form name=\"form2\" method=\"post\" action=\"addItems\">");
            out.println("Laptop <input type=\"checkbox\" name=\"check1\" value=\"Laptop\"/><br>");
            out.println("Mobile <input type=\"checkbox\" name=\"check2\" value=\"Mobile  \"/><br>");
            out.println("Tablet <input type=\"checkbox\" name=\"check3\" value=\"Tablet   \"/><br>");
            out.println("Playstation <input type=\"checkbox\" name=\"check4\" value=\"Playstation\"/><br>");
            out.println("<input type=\"submit\" name=\"submit1\" /><br>");
            out.println("</form>");

            out.println("</body>");
            out.println("</html>");
      
            }


        } 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>

}

addItem.java


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

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;

/**
 *
 * @author Sunshine
 */
@WebServlet(name = "addItems", urlPatterns = {"/addItems"})
public class addItems extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
           String s1= request.getParameter("check1");
            String s2= request.getParameter("check2");
            String s3= request.getParameter("check3");
            String s4= request.getParameter("check4");

            Cookie c[]= request.getCookies();



            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet additems</title>");
            out.println("</head>");
            out.println("<body>");

            
            out.println("Show my Cart");

            out.println("<form name=\"form2\" method=\"post\" action=\"cart\">"
                    + "<input type=\"submit\" name=\"Show Cart\"></form>");


            out.println("</body>");
            out.println("</html>");

            response.addCookie(new Cookie("one",s1));
            response.addCookie(new Cookie("two",s2));
            response.addCookie(new Cookie("three",s3));
            response.addCookie(new Cookie("four",s4));
        } 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>
}

Cart.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;


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

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

            Cookie c[]=request.getCookies();

            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet cart</title>");
            out.println("</head>");
            out.println("<body>");
            

            out.println("<p> Current user is : "+c[1].getValue().toString()+"</p>");
            out.println("<hr align=center size=5 width=100%>");
            out.println("<h2> </h2>");
            for(int i=2,j=1; i<6; i++,j++)
            {   out.println(c[i].getValue());
                out.println("");
            }
            out.println("</body>");
            out.println("</html>");

        } 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>

}

No comments:

Post a Comment