Servlet 工具类 - ServletUtil
# Servlet 工具类 - ServletUtil
简介
ServletUtil
是 Hutool 提供的一个用于简化 Servlet 相关操作的工具类。它封装了许多常用的操作,如获取请求参数、处理 Cookie、获取客户端 IP 等,极大地简化了在 Web 开发中处理 Servlet 相关逻辑的代码量。
# 依赖引入
在 Maven 项目中使用 ServletUtil
工具类,需要添加以下依赖:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet-api.version}</version>
<!-- 此包一般在 Servlet 容器中都已经提供 -->
<scope>provided</scope>
</dependency>
2
3
4
5
6
7
提示:
javax.servlet-api
的版本号请根据你使用的 Servlet 容器进行选择。
# 1. getParamMap
方法
getParamMap
方法用于获取所有请求参数并将其封装为一个 Map<String, String[]>
,便于在后续逻辑中处理请求数据。
# 示例:获取请求参数并封装为 Map
import cn.hutool.extra.servlet.ServletUtil;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
public class ServletUtilExample {
public static void main(String[] args) {
// 假设 request 是从 Servlet 中获取的 HttpServletRequest 对象
HttpServletRequest request = ...;
// 获取所有请求参数并封装为 Map
Map<String, String[]> paramMap = ServletUtil.getParamMap(request);
// 遍历参数并打印
paramMap.forEach((key, value) -> System.out.println(key + " : " + String.join(",", value)));
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
getParamMap(HttpServletRequest request)
:获取所有请求参数并封装为Map
。- 参数:
request
:HttpServletRequest
对象,表示当前的请求对象。
- 返回值:包含所有请求参数的
Map<String, String[]>
。
作用: 用于方便地获取和处理请求中的所有参数,特别适合表单提交、多参数传递的场景。
实际开发场景: 在处理复杂的表单提交时,可以使用该方法将所有参数统一封装到
Map
中,便于后续处理。- 参数:
# 2. getClientIP
方法
getClientIP
方法用于获取客户端的真实 IP 地址,支持从代理头信息中获取,避免获取到代理服务器的 IP。
# 示例:获取客户端真实 IP 地址
import cn.hutool.extra.servlet.ServletUtil;
import javax.servlet.http.HttpServletRequest;
public class ServletUtilExample {
public static void main(String[] args) {
// 假设 request 是从 Servlet 中获取的 HttpServletRequest 对象
HttpServletRequest request = ...;
// 获取客户端真实 IP
String clientIP = ServletUtil.getClientIP(request);
System.out.println("客户端 IP: " + clientIP);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
getClientIP(HttpServletRequest request)
:获取客户端的真实 IP 地址。- 参数:
request
:HttpServletRequest
对象,表示当前的请求对象。
- 返回值:客户端的真实 IP 地址。
作用: 在使用代理服务器时,可以正确获取到用户的真实 IP 地址。
实际开发场景: 在日志记录、用户访问分析、IP 访问限制等场景中,需要准确获取用户的真实 IP 地址。
- 参数:
# 3. getHeader
和 getHeaderIgnoreCase
方法
getHeader
和 getHeaderIgnoreCase
方法用于获取请求头信息,前者区分大小写,后者忽略大小写。
# 示例:获取请求头信息
import cn.hutool.extra.servlet.ServletUtil;
import javax.servlet.http.HttpServletRequest;
public class ServletUtilExample {
public static void main(String[] args) {
// 假设 request 是从 Servlet 中获取的 HttpServletRequest 对象
HttpServletRequest request = ...;
// 获取指定的请求头信息
String contentType = ServletUtil.getHeader(request, "Content-Type");
System.out.println("Content-Type: " + contentType);
// 忽略大小写获取请求头信息
String userAgent = ServletUtil.getHeaderIgnoreCase(request, "user-agent");
System.out.println("User-Agent: " + userAgent);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
getHeader(HttpServletRequest request, String name)
:获取指定名称的请求头信息,区分大小写。- 参数:
request
:HttpServletRequest
对象,表示当前的请求对象。name
:请求头的名称。
- 返回值:指定请求头的值。
- 参数:
getHeaderIgnoreCase(HttpServletRequest request, String name)
:获取指定名称的请求头信息,忽略大小写。- 参数:
request
:HttpServletRequest
对象,表示当前的请求对象。name
:请求头的名称。
- 返回值:指定请求头的值。
作用: 用于获取客户端发送的请求头信息,适用于判断请求的来源、内容类型等。
实际开发场景: 在处理跨域请求、设备识别、内容协商等场景中,需要根据请求头信息来调整服务端的响应。
- 参数:
# 4. addCookie
方法
addCookie
方法用于向客户端添加 Cookie
,支持自定义过期时间、路径等参数。
# 示例:向客户端添加 Cookie
import cn.hutool.extra.servlet.ServletUtil;
import javax.servlet.http.HttpServletResponse;
public class ServletUtilExample {
public static void main(String[] args) {
// 假设 response 是从 Servlet 中获取的 HttpServletResponse 对象
HttpServletResponse response = ...;
// 向客户端添加一个 Cookie
ServletUtil.addCookie(response, "username", "hutool", 3600);
System.out.println("Cookie 已添加");
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
addCookie(HttpServletResponse response, String name, String value, int maxAge)
:向客户端添加Cookie
。- 参数:
response
:HttpServletResponse
对象,表示当前的响应对象。name
:Cookie
的名称。value
:Cookie
的值。maxAge
:Cookie
的最大生存时间,单位为秒。
- 返回值:无。
作用: 用于在用户访问时记录信息,如登录状态、用户偏好设置等。
实际开发场景: 在用户登录、保存用户偏好设置等场景中,可以使用
Cookie
来保持会话状态或保存用户数据。- 参数:
# 5. isMultipart
方法
isMultipart
方法用于判断请求是否为文件上传表单。此方法用于处理 Multipart
类型的表单数据。
# 示例:判断请求是否为文件上传表单
import cn.hutool.extra.servlet.ServletUtil;
import javax.servlet.http.HttpServletRequest;
public class ServletUtilExample {
public static void main(String[] args) {
// 假设 request 是从 Servlet 中获取的 HttpServletRequest 对象
HttpServletRequest request = ...;
// 判断请求是否为文件上传
boolean isMultipart = ServletUtil.isMultipart(request);
System.out.println("是否为文件上传表单: " + isMultipart);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
isMultipart(HttpServletRequest request)
:判断请求是否为文件上传表单。- 参数:
request
:HttpServletRequest
对象,表示当前的请求对象。
- 返回值:
true
如果请求为Multipart
类型。
作用: 用于在文件上传处理时判断请求的类型。
实际开发场景: 在处理用户上传文件时,需要先判断请求是否为文件上传表单,以确保安全和正确处理。
- 参数:
# 6. write
方法
write
方法用于向客户端返回响应内容,支持返回字符串、字节数组等格式的数据。
# 示例:返回字符串响应
import cn.hutool.extra.servlet.ServletUtil;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class ServletUtilExample {
public static void main(String[] args) {
// 假设 response 是从 Servlet 中获取的 HttpServletResponse 对象
HttpServletResponse response = ...;
// 返回文本内容
try {
ServletUtil.write(response, "Hello, Hutool!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
write(HttpServletResponse response, String text)
:向客户端返回字符串响应。- 参数:
response
:HttpServletResponse
对象,表示当前的响应对象。text
:要返回的字符串内容。
- 返回值:无。
作用: 用于在处理请求后将结果返回给客户端。
实际开发场景: 在 Web 应用中,处理完请求后需要返回数据给客户端,这种场景非常常见。
- 参数:
ServletUtil
工具类极大地简化了 Servlet 相关的操作,尤其是在 Web 开发中频繁使用的请求处理、响应操作、Cookie 操作等方面,提供了丰富且易用的 API。通过合理使用这些工具,可以提高开发效率,并且使代码更加简洁。