public class CookieUtils { public CookieUtils() { } public static void addCookie(String search, HttpServletRequest request, HttpServletResponse response) { String info = getCookieInfo(request); if ("".equals(info)) { info = search; } else if (!info.contains(search)) { boolean isContain = info.contains("#"); if (isContain) { String[] strs = info.split("#"); if (strs.length == 3) { info = strs[1] + "#" + strs[2]; } } info = info + "#" + search; } Cookie cookie = new Cookie("search", info); response.addCookie(cookie); } public static String getCookieInfo(HttpServletRequest request) { String info = ""; Cookie[] cookies = request.getCookies(); if (cookies != null && cookies.length != 0) { Cookie[] var6 = cookies; int var5 = cookies.length; for(int var4 = 0; var4 < var5; ++var4) { Cookie cookie = var6[var4]; if ("search".equals(cookie.getName())) { info = cookie.getValue(); } } } return info; } }
时间: 2024-02-14 14:36:02 浏览: 55
这段代码是一个Java工具类,提供了两个静态方法,用于操作Cookie。具体来说,addCookie方法接收一个搜索关键字参数,一个HttpServletRequest对象和一个HttpServletResponse对象作为输入,用于向客户端添加一个名为"search"的Cookie。如果当前请求中已经存在名为"search"的Cookie,则将新的搜索关键字添加到Cookie中;如果Cookie中已经包含了3个搜索关键字,则删除最早的那个搜索关键字。getCookieInfo方法接收一个HttpServletRequest对象作为输入,用于获取当前请求中名为"search"的Cookie的值,并返回该值。如果当前请求中不存在名为"search"的Cookie,则返回空字符串。该类的使用可以方便地在Web应用程序中操作Cookie,实现在不同的请求之间共享数据的功能。
相关问题
Javaweb完整代码 2.使用cookie技术,实现发送cooike和获取cooike 要求如下 1.创建一个servlet使用response对象向客户端发送一个cooike,cooike的名为myCooike,cooike的值为hait 2.创建另一个servlet,获取所有的cooike的信息,遍历cooike,把刚刚发送的名为myCooike的cooike的值赎回处到网页上
好的,以下是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的值显示在网页上了。
springboot使用cookie
如何在Spring Boot中使用Cookie?
在Spring Boot中,可以使用HttpServletResponse和HttpServletRequest对象来操作Cookie。通过HttpServletResponse对象的addCookie方法可以将Cookie添加到响应中,而HttpServletRequest对象的getCookies方法可以获取请求中的所有Cookie。
示例代码如下:
//设置Cookie
@GetMapping("/setCookie")
public String setCookie(HttpServletResponse response) {
Cookie cookie = new Cookie("username", "admin");
cookie.setMaxAge(3600); //设置Cookie的过期时间为1小时
response.addCookie(cookie);
return "success";
}
//获取Cookie
@GetMapping("/getCookie")
public String getCookie(HttpServletRequest request) {
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
if ("username".equals(cookie.getName())) {
return cookie.getValue(); //获取Cookie的值
}
}
}
return "not found";
}
阅读全文