Request.getParameter()乱码问题

    xiaoxiao2021-12-14  19

       URIEncoding,该配置决定了使用get请求通过浏览器地址栏访问tomcat时的编码方式,默认的编码方式使ISO8859-1,Tomcat7官方文档:https://tomcat.apache.org/tomcat-7.0-doc/config/http.html

    URIEncoding This specifies the character encoding used to decode the URI bytes, after %xx decoding the URL. If not specified, ISO-8859-1 will be used.   Post请求处理方式

           Tomcat对requset.getParameter()的默认编码方式为iso-8859-1,

            方法一: request.setCharacterEncoding(String env)

             Overrides the name of the character encoding used in the body of this request. This method must be called prior to reading request parameters or reading input using getReader().

          方法二:String username=new String(request.getParameter("iso-8859-1"),"utf-8");

      GET请求的处理方式

         Tomcat的Get与post的编码方式不同在于:Tomcat对get请求不会考虑用request.setCharacterEncoding()设置的编码方式,即Tomcat会永远使用ISO-8859-1进行编码

           步骤一:resp.setContentType("text/html;charset=utf-8");

           步骤二:username=new String(username.getBytes("ISO-8859-1"),"utf-8");

           或者

           在server.xml中,修改<connector connectionTimeOut="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" URLEncoding="utf-8">(对GET方式)

         <connector connectionTimeOut="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" useBodyEncodingForURI="true"  URLEncoding="utf-8">

       useBodyEncodingForURI参数表示是否用request.setCharacterEncoding参数对URL提交的数据和表单中GET方式提交的数据进行重新编码,在默认情况      下,该参数为false。

        URIEncoding参数指定对所有GET方式请求进行统一的重新编码(解码)的编码。

         重启Tomcat

    使用过滤器:

         EncodingFilter类

         

    package mypack; 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; import javax.servlet.http.HttpServletRequest; public class EnCodingFilter implements Filter{ private FilterConfig config; private String charset; @Override public void destroy() { // TODO Auto-generated method stub } @Override public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException { // TODO Auto-generated method stub resp.setContentType("text/html;charset="+charset); EnCodeRequest request = new EnCodeRequest((HttpServletRequest) req,charset); System.out.println("diFilter()"); chain.doFilter(request, resp); } @Override public void init(FilterConfig config) throws ServletException { // TODO Auto-generated method stub this.config=config; charset = config.getInitParameter("charset"); if(charset==null || charset.isEmpty()) charset="utf-8"; System.out.println("init() "+charset); } } EnquestRequest类

    package mypack; import java.io.UnsupportedEncodingException; import javax.servlet.ServletRequest; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; public class EnCodeRequest extends HttpServletRequestWrapper{ private String charset; public EnCodeRequest(HttpServletRequest request,String charset) { super(request); this.charset=charset; } public String getParameter(String str){ HttpServletRequest request = (HttpServletRequest) getRequest(); String method = getMethod(); if(method.equalsIgnoreCase("post")){ try { request.setCharacterEncoding(charset); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block throw new RuntimeException("Unsupport CharacterEncoding"); } } else if(method.equalsIgnoreCase("get")){ String value = request.getParameter(str); try { value= new String(value.getBytes("ISO-8859-1"),charset); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block throw new RuntimeException("Unsupport CharacterEncoding"); } return value; } return request.getParameter(str); } } Demo类

     

    package mypack; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class Demo extends HttpServlet{ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub PrintWriter writer = resp.getWriter(); String username = req.getParameter("username"); System.out.println(username); if(username!=null){ //username=new String(username.getBytes("ISO-8859-1"),"utf-8"); writer.println("<html>"); writer.println("<head><title>test</title></head>"); writer.println("<body>您好:"+username+"</body>"); writer.println("</html>"); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub doGet(req, resp); } } web.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>EnCodeAndDecode</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>filter</filter-name> <filter-class>mypack.EnCodingFilter</filter-class> <init-param> <param-name>charset</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>filter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>demo</servlet-name> <servlet-class>mypack.Demo</servlet-class> </servlet> <servlet-mapping> <servlet-name>demo</servlet-name> <url-pattern>/demo</url-pattern> </servlet-mapping> </web-app>对于Tomcat8以及以上Request.getParameter()默认编码为utf-8

    官网:https://tomcat.apache.org/tomcat-8.0-doc/config/http.html

    URIEncoding This specifies the character encoding used to decode the URI bytes, after %xx decoding the URL. If not specified, UTF-8 will be used unless the org.apache.catalina. STRICT_SERVLET_COMPLIANCE system property is set to true in which case ISO-8859-1 will be used.

         

          

    转载请注明原文地址: https://ju.6miu.com/read-969386.html

    最新回复(0)