如果浏览器提交数据给Servlet的时候含中文参数,那么在Servlet中就要处理中文乱码。如果有多个Servlet都要同时接收中文参数,那么在Servlet中处理中文乱码就比较麻烦。
解决办法:使用过滤器处理中文乱码。
处理post请求的乱码
index.jsp代码
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="${pageContext.request.contextPath }/AddUserServlet" method="post"> 用户名:<input type="text" name="userName" /><br> <input type="submit" value="提交" /> </form> <hr> <form action="${pageContext.request.contextPath }/AddProductServlet" method="post"> 产品名:<input type="text" name="productName" /><br> <input type="submit" value="提交" /> </form> </body> </html>
AddUserServlet.java类代码(Servlet 业务处理)
import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class AddUserServlet */ @WebServlet("/AddUserServlet") public class AddUserServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取文本框输入参数 String userName=request.getParameter("userName"); System.out.println("userName="+userName); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
AddProductServlet.java类代码(Servlet 业务处理)
import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class AddProductServlet */ @WebServlet("/AddProductServlet") public class AddProductServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取文本框输入参数 String productName=request.getParameter("productName"); System.out.println("productName="+productName); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
创建一个CharacterEncodingFilter类来实现Filter接口,并实现Filter所有方法
import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; public class CharacterEncodingFilter implements Filter { @Override public void destroy() { // TODO Auto-generated method stub } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { request.setCharacterEncoding("utf-8"); chain.doFilter(request, response); } @Override public void init(FilterConfig arg0) throws ServletException { // TODO Auto-generated method stub } }
web.xml配置:
结果:
如果要处理Get请求的中文乱码,这时候需要对request对象进行增强处理。