Cookie显示一些随机值

时间:2017-08-02 08:04:43

标签: java servlets cookies

我想获得" id"的整数值但cookie的第二个参数只需要字符串值。它没有向我显示任何错误,但它正在打印随机值 喜欢" javax.servlet.http.Cookie@821ece4"当我试图打印它时。解决此问题的任何解决方案。

@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();
        }
    }


}

2 个答案:

答案 0 :(得分:1)

当你有一个javax.servlet.http.Cookie并尝试打印它时,你只会获得对象引用,因此@821ece4。您需要做的是实际使用here所述的Cookie方法。类似的东西:

ck[2].getName()

获取cookie的名称或

ck[2].getValue()

获取实际存储的值。

答案 1 :(得分:0)

您应该打印pw.println("Your last Record Deleted is " +ck2[1].getValue());