Javaweb完整代码 2.使用cookie技术,实现发送cooike和获取cooike 要求如下 1.创建一个servlet使用response对象向客户端发送一个cooike,cooike的名为myCooike,cooike的值为hait 2.创建另一个servlet,获取所有的cooike的信息,遍历cooike,把刚刚发送的名为myCooike的cooike的值赎回处到网页上
时间: 2023-12-08 13:04:12 浏览: 58
好的,以下是JavaWeb的完整代码,实现发送和获取cookie:
发送cookie的servlet(SendCookieServlet.java):
```java
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SendCookieServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//创建cookie对象
Cookie cookie = new Cookie("myCookie", "hait");
//设置cookie的最大存活时间为1小时
cookie.setMaxAge(60 * 60);
//向客户端发送cookie
response.addCookie(cookie);
}
}
```
获取cookie的servlet(GetCookieServlet.java):
```java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class GetCookieServlet 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");
PrintWriter out = response.getWriter();
out.println("<html><head><title>获取cookie</title></head><body>");
//获取所有的cookie
Cookie[] cookies = request.getCookies();
if(cookies != null) {
for(Cookie cookie : cookies) {
//查找名为myCookie的cookie
if(cookie.getName().equals("myCookie")) {
//将cookie的值显示在网页上
out.println("myCookie的值为:" + cookie.getValue());
}
}
}
out.println("</body></html>");
out.close();
}
}
```
web.xml配置文件:
```xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>CookieDemo</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>SendCookieServlet</servlet-name>
<servlet-class>SendCookieServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SendCookieServlet</servlet-name>
<url-pattern>/sendCookie</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>GetCookieServlet</servlet-name>
<servlet-class>GetCookieServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>GetCookieServlet</servlet-name>
<url-pattern>/getCookie</url-pattern>
</servlet-mapping>
</web-app>
```
index.jsp文件:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>CookieDemo</title>
</head>
<body>
<h1>发送和获取cookie</h1>
<p><a href="sendCookie">发送cookie</a></p>
<p><a href="getCookie">获取cookie</a></p>
</body>
</html>
```
运行程序后,在浏览器中访问http://localhost:8080/CookieDemo/,点击“发送cookie”链接,然后点击“获取cookie”链接,就可以看到刚刚发送的cookie的值显示在网页上了。
阅读全文