When the get / post solution to the garbage problem in Chinese

1, article 1

Recently I encountered a problem: with get delivery method Chinese have problems with post no problem.
The problem is this simple description:
<a href="userGroup. JSP ?userGroupName=<%=userGroupName%> "> AA </a>
here userGroupName is Chinese

in userGroup. JSP pages get userGroupName is garbled.
Each page also has a <% @ page language = "java " pageEncoding = "GBK"%> instructions.

Later, the Internet looking for a bit to know: pageEncoding the only post work. get when submitting method, we can see from the address bar parameters submitted, because get method transfer is submitted as a header, but pageEncoding packet header is not useful, it is still in accordance with iso8859-1 encoding, it came with a garbled earlier. The post submission form is the content of the form, pageEncoding specify its encoding, so he passed the specified encoding.

Issue clear, here to solve it:

Because the tomcat servlet implementation ServletRequest.
GET data submitted by the method can not correctly decode solutions to modify the configuration file server.xml its server to the HTTP protocol
Connector configuration, plus URIEncoding = "GBK" attribute, after the configuration is complete, "possible" content
< Connector Port = "8080"
the maxThreads = "150" minSpareThreads = "25" maxSpareThreads = "75"
enableLookups = "to false" the redirectPort = "8443" acceptCount to = "100"
Debug = "0" connectionTimeout = "20000" 
disableUploadTimeout = "to true "  URIEncoding =" GBK "/> 
and you're done!

2, article 2

Form submission, Method, = " GET " is the default value, submitted in the past by url, form: HTTP: // localhost:. 8081 / the Test JSP ? Username = bebe0453 & password = 082 628 & the Submit =% C8% B7% EF% BF% BD% EF% BF% BD

= The Method, " POST ", was an implicit commit, the parameter does not appear in the browser address bar.

POST is more confidential and submit a larger capacity in the past, generally used POST submission. 

 

3, article 3

In the project, we often encounter the need jsp transfer Chinese characters in page switching. This is mainly in two ways.

  1. URL mode, for example: HTTP: // Website / test1. JSP ACT = the Add & of the type? = Apple & param =% 20D% 20B 
  2. FORM way, such as: 

                     <form name=test  mehtod="post">

                              <input type=hidden name=text2 value="中文">

                             <input type=text name=text1>

                             <input type=submit value=submit>

                    </form>

    We will focus on these two cases, respectively, provide solutions to Chinese proper delivery.

Case 1: URL mode

For example: HTTP: // Website / test1. JSP ACT = the Add & of the type? = Apple & param =% 20D% 20B

  • Generally speaking we rarely directly inside the URL parameters written in Chinese, as in the example of the "type = apple" this transfer. If this happens, the page in our reception parameters we just need to make a simple conversion on it. 

            Code test1. JSP major part :()

           <%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>

       <%

           String type = request.getParameter("type");

           String result = new String(type.getBytes("iso-8859-1"), "gb2312");      

           out.println(result);   

     %>

  • 更普遍的做法,就是对url中的中文字符进行编码,变成类似type=%20D%20B这样的字符。 

代码MyJsp1.jsp

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ page import="java.net.*" %>

<a href='./MyJsp2.jsp?act=<%=URLEncoder.encode("中国人 非常好 =-")%>'>test</a>

代码MyJsp2.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ page import="java.net.*" %>

String tempVal = URLDecoder.decode(request.getParameter("act"));
out.println(new String(tempVal.getBytes("ISO-8859-1"), "gb2312"));

     情况2:FORM方式

     请注意,我们只是讨论在<form enctype="application/x-www- form-urlencoded" >这种形式的中文情况,因为在enctype="multipart/form-data"的时候通过解析出来 中文也可以运用这种方法进行字符转化,所以不再重复讨论。

  • <form method=post>这种情况最简单。 

 代码MyJsp1.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>

 <form action="./MyJsp2.jsp" method="post" enctype="application/x-www-form-urlencoded" >
 <input type=hidden name=act value=动作 />
 <input type=submit value=ok>
 </form>

代码MyJsp2.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>

request.setCharacterEncoding("gb2312");

out.println(request.getParameter("act"));

或者

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>

String tempVal = request.getParameter("act");

out.println(new String(tempVal.getBytes("ISO-8859-1"), "gb2312"));

  • <form method=get>情况。 

代码MyJsp1.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>

 <form action="./MyJsp2.jsp" method="get" enctype="application/x-www-form-urlencoded" >
 <input type=hidden name=act value=动作 />
 <input type=submit value=ok>
 </form>

代码MyJsp2.jsp:

           

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>

String tempVal = request.getParameter("act");

out.println(new String(tempVal.getBytes("ISO-8859-1"), "gb2312"));

转载于:https://www.cnblogs.com/Codenewbie/p/3336475.html

1、文章1

最近遇到一个问题:用get方法传递中文有问题,用post没有问题。
问题简单的描述是这样的:
<a href="userGroup.jsp?userGroupName=<%=userGroupName%>">aa</a>
这里userGroupName是中文

在userGroup.jsp页面得到的userGroupName却是乱码。
每个页面也都有<%@ page language="java" pageEncoding="GBK" %>说明。

后来上网找了一下才知道:pageEncoding这个只对post起作用。get方法提交时,大家可以从地址栏里看到提交的参数,这是因为get方法传递是作为报文头提交的,而pageEncoding对报文头是没有作用的,所以仍然按照 iso8859-1编码,才出现了刚才的乱码问题。而post提交的是form表单的内容,pageEncoding指定了它的编码,所以他会按照指定编码传递。

问题清楚了,下面就来解决它:

由于tomcat的servlet实现中ServletRequest.setCharacterEncoding方法未对HTP报文头的内容进行解码,因此
使用HTTP的GET方法提交的数据将不能正确的解码.解决方案为修改其服务器的配置server.xml文件中对HTTP协议的
Connector配置,加上URIEncoding="GBK"属性,配置完成以后"可能"的内容为
< Connector port="8080"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
debug="0" connectionTimeout="20000" 
disableUploadTimeout="true" URIEncoding="GBK" /> 
大功告成!

2、文章2

表单提交中,method="GET" 为默认值,通过url提交过去,形式为:http://localhost:8081/test.jsp?username=bebe0453&password=082628&submit=%C8%B7%EF%BF%BD%EF%BF%BD

而 method="POST" ,则为隐式提交,在浏览器地址栏不会出现参数。

POST更加保密,且提交过去的容量更大,一般都采用POST提交。 

 

3、文章3

在项目中,我们经常遇到需要在jsp页面切换中传递中文字符。这主要有两种方式。

  1. URL方式,例如:http://website/test1.jsp?act=add&type=苹果&param=%20D%20B 
  2. FORM方式,例如: 

                     <form name=test  mehtod="post">

                              <input type=hidden name=text2 value="中文">

                             <input type=text name=text1>

                             <input type=submit value=submit>

                    </form>

    我们将针对这两种情况,分别提供中文正确传递的解决方法。

情况1:URL方式

例如:http://website/test1.jsp?act=add&type=苹果&param=%20D%20B

  • 一般来说我们很少直接在URL里面把参数写成中文,如例子中的"type=苹果"这样传递。如果出现这种情况,在我们的接收参数的页面我们只需要做个简单的转换就可以了。 

            代码test1.jsp:(主要部分)

           <%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>

       <%

           String type = request.getParameter("type");

           String result = new String(type.getBytes("iso-8859-1"), "gb2312");      

           out.println(result);   

     %>

  • 更普遍的做法,就是对url中的中文字符进行编码,变成类似type=%20D%20B这样的字符。 

代码MyJsp1.jsp

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ page import="java.net.*" %>

<a href='./MyJsp2.jsp?act=<%=URLEncoder.encode("中国人 非常好 =-")%>'>test</a>

代码MyJsp2.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ page import="java.net.*" %>

String tempVal = URLDecoder.decode(request.getParameter("act"));
out.println(new String(tempVal.getBytes("ISO-8859-1"), "gb2312"));

     情况2:FORM方式

     请注意,我们只是讨论在<form enctype="application/x-www- form-urlencoded" >这种形式的中文情况,因为在enctype="multipart/form-data"的时候通过解析出来 中文也可以运用这种方法进行字符转化,所以不再重复讨论。

  • <form method=post>这种情况最简单。 

 代码MyJsp1.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>

 <form action="./MyJsp2.jsp" method="post" enctype="application/x-www-form-urlencoded" >
 <input type=hidden name=act value=动作 />
 <input type=submit value=ok>
 </form>

代码MyJsp2.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>

request.setCharacterEncoding("gb2312");

out.println(request.getParameter("act"));

或者

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>

String tempVal = request.getParameter("act");

out.println(new String(tempVal.getBytes("ISO-8859-1"), "gb2312"));

  • <form method=get>情况。 

代码MyJsp1.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>

 <form action="./MyJsp2.jsp" method="get" enctype="application/x-www-form-urlencoded" >
 <input type=hidden name=act value=动作 />
 <input type=submit value=ok>
 </form>

代码MyJsp2.jsp:

           

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>

String tempVal = request.getParameter("act");

out.println(new String(tempVal.getBytes("ISO-8859-1"), "gb2312"));

Guess you like

Origin blog.csdn.net/weixin_33978044/article/details/93448118