创建一个servlet,向客户端写入一个Cookie对象(写入信息:你的学号与姓名,有效期:一周。特别注意:这里要保存两个Cookie,当然保存一个包含学号和姓名的对象也可以的);
时间: 2024-05-29 10:09:32 浏览: 12
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.util.Calendar;
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("/CreateCookieServlet")
public class CreateCookieServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 设置响应内容类型
response.setContentType("text/html;charset=UTF-8");
// 获取当前时间
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH, 7); // 设置有效期为一周
String expireTime = calendar.getTime().toString();
// 创建Cookie对象
String studentId = "123456";
String studentName = "张三";
Cookie cookie1 = new Cookie("studentId", URLEncoder.encode(studentId, "UTF-8"));
Cookie cookie2 = new Cookie("studentName", URLEncoder.encode(studentName, "UTF-8"));
cookie1.setMaxAge(604800); // 设置Cookie的有效期为一周
cookie2.setMaxAge(604800);
cookie1.setPath("/"); // 设置Cookie的路径为根目录
cookie2.setPath("/");
cookie1.setComment("学号");
cookie2.setComment("姓名");
cookie1.setDomain("localhost"); // 设置Cookie的域名为localhost
cookie2.setDomain("localhost");
cookie1.setSecure(false); // 设置Cookie的安全性为非安全
cookie2.setSecure(false);
cookie1.setHttpOnly(false); // 设置Cookie的HttpOnly属性为false
cookie2.setHttpOnly(false);
// 添加Cookie到响应头
response.addCookie(cookie1);
response.addCookie(cookie2);
// 输出HTML文档
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Create Cookie</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>已创建Cookie对象</h1>");
out.println("<p>学号:" + studentId + "</p>");
out.println("<p>姓名:" + studentName + "</p>");
out.println("<p>有效期:" + expireTime + "</p>");
out.println("</body>");
out.println("</html>");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)