3 Replies - 1522 Views - Last Post: 28 November 2012 - 05:26 PM

#1 MitulP91  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 18-July 12

Creating a Servlet to Store Cookies

Posted 28 November 2012 - 01:58 PM

I've just been given an assignment regarding servlets, a topic I am having quite a bit of difficulty understanding. I have been asked to create a servlet that stores cookies named color, radius, and count with values red, 5.5, and 2 respectively in a browser and to set the max age for 2 days. Then, create a separate servlet that displays the cookies to the client when the URL of the servlet is accessed.

I'm not looking for the answers, but if anyone could give me some tips to get started and/or link me to a good tutorial I would appreciate it.

Is This A Good Question/Topic? 0
  • +

Replies To: Creating a Servlet to Store Cookies

#2 MitulP91  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 18-July 12

Re: Creating a Servlet to Store Cookies

Posted 28 November 2012 - 03:39 PM

I have been given an assignment to create a servlet that stores 3 cookies: color, radius, and count. The following is the code I've come up with so far based on an example in my text book.

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;

public class StoreCookies extends HttpServlet {
    public static final String CONTENT_TYPE = "text/html";
    private PreparedStatement pstmt;

    public void init() throws ServletException {
        initializeJdbc();
    }

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

        String color = request.getParameter("color");
        String radius = request.getParameter("radius");
        String count = request.getParameter("count");

        Cookie cookieColor = new Cookie("color", color);
        cookieColor.setMaxAge(172800);
        response.addCookie(cookieColor);
        Cookie cookieRadius = new Cookie("radius", radius);
        cookieRadius.setMaxAge(172800);
        response.addCookie(cookieRadius);
        Cookie cookieCount = new Cookie("count", count);
        cookieCount.setMaxAge(172800);
        response.addCookie(cookieCount);

        out.println("You entered the following data");
        out.println("<p>Color: " + color);
        out.println("<br>Radius: " + radius);
        out.println("<br>Count: " + count);

        out.println("<p><form method =\"post\" action=" +
                "Cookies>");
        out.println("<p><input type = \"submit\" value = \"Confirm\" >");
        out.println("</form>");

        out.close();
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType(CONTENT_TYPE);
        PrintWriter out = response.getWriter();

        String color = "";
        String radius = "";
        String count = "";

        Cookie[] cookies = request.getCookies();

        for(int i = 0; i < cookies.length; i++) {
            if (cookies[i].getName().equals("color")) {
                color = cookies[i].getValue();
            }

            else if(cookies[i].getName().equals("radius")) {
                radius = cookies[i].getValue();
            }

            else if(cookies[i].getName().equals("count")) {
                count = cookies[i].getValue();
            }
        }

        try {
            storeValues(color, radius, count);

            out.close();
        }

        catch(Exception ex) {
            out.println("Error: " + ex.getMessage());
        }
    }

    private void initializeJdbc() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("Driver loaded");

            Connection conn = DriverManager.getConnection
                    ("jdbc:mysql://localhost/javabook", "Mitul", "Patel");
            System.out.println("Database connected");

            pstmt = conn.prepareStatement("insert Values " +
                    "(color, radius, count) values (?, ?, ?)");
        }
        catch (Exception ex) {
            System.out.println(ex);
        }
    }

    private void storeValues(String color, String radius, String count)
            throws SQLException {
        pstmt.setString(1, color);
        pstmt.setString(2, radius);
        pstmt.setString(3, count);
        pstmt.executeUpdate();
    }
}



I am not sure this is right, but that is not my current problem.
When I try to run this it tells me it is not a servlet specified in web.xml

I found web.xml and I see an add servlet option, but I am unsure of how to fill out the fields in order to get this servlet to run. Can anyone help?
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10396
  • View blog
  • Posts: 38,463
  • Joined: 27-December 08

Re: Creating a Servlet to Store Cookies

Posted 28 November 2012 - 03:50 PM

Duplicate threads merged. Please avoid duplicate posting.
Was This Post Helpful? 0
  • +
  • -

#4 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1150
  • View blog
  • Posts: 2,528
  • Joined: 05-May 05

Re: Creating a Servlet to Store Cookies

Posted 28 November 2012 - 05:26 PM

Quote

I found web.xml and I see an add servlet option, but I am unsure of how to fill out the fields in order to get this servlet to run. Can anyone help?


In Servlet 3.0 you can use annotation-based Servlet-URL mapping, instead of defining at entry in web.xml. Either way works, annotations are easier. It's something like this:

@WebServlet("/<url-pattern>");
public class MyServlet{}


See the Java EE 6 tutorials in the J2EE resource thread.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1