Jeet Patel
Jeet Patel

Reputation: 1241

Cookies are showing some random values

I am trying to get integer value of "id" but second parameter of cookie requires string value only. It is not showing me any error but it is printing random values like "javax.servlet.http.Cookie@821ece4" while I tried to print it.Any solution to resolve this.

@WebServlet("/Delete")
public class Delete extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String s = request.getParameter("id");
        Cookie ck1 = new Cookie("name", s);
        response.addCookie(ck1);

        PrintWriter pw = response.getWriter();

        pw.print("<html>");
        pw.print("<body>");

        pw.println("<form action = 'index.html' > <input type = 'submit' value = 'Home'></form>");

        pw.print("</html>");
        pw.print("</body>");


        int id = Integer.parseInt(request.getParameter("id"));

        try{
            Class.forName("com.mysql.jdbc.Driver");
        }catch(Exception e){
            e.printStackTrace();
        }

        try{

            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jeet","root","jeet");
            PreparedStatement pst = con.prepareStatement("Delete from jeet where id = ?");
            pst.setInt(1, id);
            pst.executeUpdate();
            pw.println("<center>");
            pw.println("<br>");
            pw.println("Your Record is been Deleted");
            pw.println("<br>");

            Cookie ck2[] = request.getCookies();

            pw.println("Your last Record Deleted is " +ck2[1]);
            pw.println("</center>");

        }catch(Exception e){
            e.printStackTrace();
        }
    }


}

Upvotes: 1

Views: 407

Answers (2)

mattjegan
mattjegan

Reputation: 2884

When you have a javax.servlet.http.Cookie and try to print it, you are only going to be getting the object reference, hence the @821ece4. What you need to do is actually make use of the Cookie methods as described here. Something like:

ck[2].getName()

to get the name of the cookie or

ck[2].getValue()

to get the actual stored value.

Upvotes: 1

S&#225;ndor Juhos
S&#225;ndor Juhos

Reputation: 1615

You should print the pw.println("Your last Record Deleted is " +ck2[1].getValue());

Upvotes: 0

Related Questions