什么时候在Servlet中创建cookie?

时间:2018-06-16 13:59:59

标签: java servlets cookies session-cookies

我正在学习Servlet和cookies,我有一个误解。可以在不使用" new Cookie"?

的情况下创建cookie

我知道使用此声明创建了一个cookie:

Cookie ck = new Cookie("username",name);

以下语句用于在客户端浏览器中发送cookie:

response.addCookie(ck);

但是我找到了一个应用程序并且它没有上面的声明,它在我的浏览器中创建了一个cookie。如果我不使用" new Cookie"我在创建新会话时会创建Cookie吗?在我的代码?

这是代码:

的index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form method="post" action="Validate">
  User: <input type="text" name="user" /><br/>
  Password: <input type="password" name="pass" ><br/>
  <input type="submit" value="submit">
</form>
</body>
</html>

Validate.java

package servlet.httpses;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class Validate
 */
@WebServlet("/Validate")
public class Validate extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Validate() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.getWriter().append("Served at: ").append(request.getContextPath());
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/html;charset=UTF-8");

        String name = request.getParameter("user");
        String pass = request.getParameter("pass");

        if(pass.equals("1234")) {

            //creating a session
            HttpSession session = request.getSession();

            session.setAttribute("user", name);

            response.sendRedirect("Welcome");
        }
    }
}

Welcome.java

package servlet.httpses;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class Welcome
 */
@WebServlet("/Welcome")
public class Welcome extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Welcome() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/html;charset=UTF-8");

        PrintWriter out = response.getWriter();

        HttpSession session = request.getSession();

        String user = (String) session.getAttribute("user");

        out.println("Hello " + user);

        out.println("<br>Session ID in Request is from a Cookie: " 
        + request.isRequestedSessionIdFromCookie());

        out.println("<br> Session ID: " + session.getId());
        out.println("<br> Creation Time: " + new Date(session.getCreationTime()));
        out.println("<br>Last Accessed Time: " + new Date(session.getLastAccessedTime()));
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }
}

在这种情况下什么时候创建cookie?可以在不使用&#34;新cookie&#34;?

的情况下创建cookie

提前致谢!

1 个答案:

答案 0 :(得分:-1)

您可以通过两种方式创建Cookie。

  1. 在服务器上创建一个cookie并将其发送到客户端

  2. 在客户端创建Cookie

  3. 当你使用新的Cookie时,java会创建一个cookie对象并发送到客户端,客户端会保存cookie。

    另一方面,您可以使用一些数据在JavaScript的帮助下创建cookie。