JavaWeb

JavaWeb-018-中文处理

2017-11-28  本文已影响20人  53b3f4658edc
<jsp-config>
    <jsp-property-group>
        <url-pattern>/jsp/*</url-pattern>
        <page-encoding>GB2312</page-encoding>
    </jsp-property-group>
</jsp-config>

post请求解决中文乱码

1.设置请求的方式为method为post
2.设置request.setCharacterEncoding为UTF-8(注意:这一步必须在getParameter之前)
3.获取request.getParameter()


通过解决

首先明白:在传输过程中默认使用的传输编码是欧码ISO-8859-1


方法一

1.获取getParameter
2.进行解码再编码(new String(解码的byte数组,编码格式))

方法二

1.打开Tomcat的server.xml文件(/usr/local/apache-tomcat-7.0.81/conf)
2.修改<Connector>节点的属性,添加一个useBodyEncodingForURI="true"(使用请求体的编码来传输)

<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" 
useBodyEncodingForURI="true" />

3.把Eclipse中的servers中的server.xml也这样改
4.重启Tomcat
5.按照post的样子处理

测试代码

a.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>
        <%--
        1.显示中文:
            四个一致解决中文乱码(编码本身推荐UTF-8):
                1.charset
                2.pageEncoding
                3.页面文件本身
                4.浏览器的解析
         --%>
         你好,我是中文!
         
         <!--  
         2.获取中文:
            2.1:post请求独有的
                2.1.1.设置请求的方式为method为post
                2.1.2.设置request.setCharacterEncoding为UTF-8(注意:这一步必须在getParameter之前)
                2.1.3.获取request.getParameter()
            2.3:通用的:
                2.3.1.在传输过程中默认使用的传输编码是欧码ISO-8859-1
                2.3.2.方法一:
                    2.3.2.1.获取getParameter
                    2.3.2.2.进行解码再编码(new String(解码的byte数组,编码格式))
                2.3.3.方法二:
                    2.3.3.1.修改Tomcat的server.xml文件(/usr/local/apache-tomcat-7.0.81/conf)
                    2.3.3.2.修改<Connector>节点的属性,添加一个useBodyEncodingForURI="true"(使用请求体的编码来传输)
                            <Connector port="8080" protocol="HTTP/1.1"
                                connectionTimeout="20000"
                                redirectPort="8443" useBodyEncodingForURI="true"
                            />
                    2.3.3.3.把Eclipse中的servers中的server.xml也这样改
                    2.3.3.4.重启Tomcat
                    2.3.3.5.按照post的样子处理中文就是了
         -->
         <%
            request.setCharacterEncoding("UTF-8");
         %>
        <%
            String aux = request.getParameter("gender");
            /*
                public String(byte[] bytes,String charsetName) throws UnsupportedEncodingException:
                通过使用指定的 charset 解码指定的 byte 数组,构造一个新的 String。新 String 的长度是字符集的函数,
                因此可能不等于 byte 数组的长度。当给定 byte 在给定字符集中无效的情况下,此构造方法的行为没有指定。
                如果需要对解码过程进行更多控制,则应该使用 CharsetDecoder 类。
                参数:
                bytes - 要解码为字符的 byte
                charsetName - 受支持的 charset 的名称
            */
            String gender = new String(aux.getBytes("utf-8"),"utf-8");      //因为写在一起的,前面已经设置成了utf-8了,这里解码的时候写utf-8才对(默认是iso-8859-1)
        %> 
         <%
            String user = request.getParameter("user");
         %>

         <p>姓名:<%= user %></p>
         <p>性别: <%= gender %></p>
    </body>
</html>

b.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="a.jsp"  method="post">
            姓名:<input type="text" name="user"/>
            性别: <input type="text" name="gender"/>
            <input type="submit" name="submit"/>
        </form>
        
    </body>
</html>

效果:


微信公众号:JavaWeb架构师

其它

关注下方公众号,回复:javaweb_course.code
完整教程PDF版本下载
上一篇 下一篇

猜你喜欢

热点阅读