赞
赏
使用 JavaEE Filter 解决 POST 方法提交汉字乱码的问题。
编写过滤器,解决所有 Servlet 中使用 POST 方法提交汉字乱码的问题。
百度云
链接:https://pan.baidu.com/s/1CrCm15py7wEsIXcvCbwIIw 提取码:sr6c
register.jsp 代码:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用户注册</title>
</head>
<body>
<h2>用户注册(www.haicoder.net)</h2>
<form action="register" method="post">
注册名:<input type="text" name="name">
<input type="submit" value="注册">
</form>
</body>
</html>
login.jsp 代码:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登录</title>
</head>
<body>
<h2>用户登录(www.haicoder.net)</h2>
<form action="login" method="post">
登录名:<input type="text" name="user">
<input type="submit" value="登录">
</form>
</body>
</html>
RegisterServlet 代码:
package net.haicoder.servlet;
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 java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter pw = response.getWriter();
pw.print("嗨客网(www.haicoder.net)<br/>");
String name = request.getParameter("name");
pw.print("注册成功,您的注册名字是:" + name);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
LoginServlet 代码:
package net.haicoder.servlet;
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 java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter pw = response.getWriter();
pw.print("嗨客网(www.haicoder.net)<br/>");
//获取提交的用户名,显示在页面上
String user = request.getParameter("user");
pw.print("登录成功,欢迎您:" + user);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
CharacterEncodingFilter 代码:
package net.haicoder.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
@WebFilter(filterName = "CharacterEncodingFilter", urlPatterns = "/*")
public class CharacterEncodingFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
// 向下转型转成子接口
HttpServletRequest request = (HttpServletRequest) req;
// 获取请求的方式
String method = request.getMethod();
// 判断请求的方式是否是POST方法,注:POST是大写
if ("POST".equals(method)) {
// 设置请求的编码是utf-8
req.setCharacterEncoding("utf-8");
}
// 放行
chain.doFilter(req, resp);
}
public void destroy() {
}
}
浏览器访问登录界面,如下图:
不使用过滤器时,点击登录按钮,会出现乱码,如下图:
浏览器访问注册界面,如下图:
不使用过滤器时,点击注册按钮,会出现乱码,如下图:
使用过滤器后,再次登录,如下图:
使用过滤器后,再次注册,如下图:
以上案例使用过滤器,解决所有 Servlet 中使用 POST 方法提交汉字乱码的问题。