jsp page jsp and servlet url pass parameters Java web download file file name garbled

1. The content of the jsp page displays garbled characters

The reason for this garbled code is very simple. General tools or decoding programs use the default decoding method when parsing Chinese characters:

<%@ page contentType="text/html; charset=ISO-8859-1"%>

We just need to modify its encoding, as follows:

<%@ page contentType="text/html; charset=UTF-8"%>

Character set: UTF-8 > GBK > GB2312

2. Chinese garbled characters appear when jumping between jsp and servlet

2.1: method="Post"

When the action of the form form in jsp is "XxxServlet" and method="Post", after submitting the form, it is often found that the Chinese attribute value becomes garbled after being obtained in the Servlet.

At this point, you need to locate the doPost() method. First, add the following code to the first line of the method:

      request.setCharacterEncoding("UTF-8");

It means to set the encoding of the request to "UTF-8", which is generally consistent with the jsp page

Then, add code:

      response.setCharacterEncoding("UTF-8");

response.setContentType("text/html;charset=UTF-8");

It means to set the encoding of the response to "UTF-8", that is, the encoding when the Servlet returns jsp. One of the above two paragraphs can be used, and consistency is the key.

2.2:method="Get"

When the action of the form form in jsp is "XxxServlet", and the method is "Get", after submitting the form, it is often found that the Chinese attribute value becomes garbled after being obtained in the Servlet.

The tomcat installation directory %TOMCAT%/conf/server.xml file to be located at this time 

Look for the following code snippet:

1.    <Connector port="8080" protocol="HTTP/1.1"    

2.      maxThreads="150"     

3.      connectionTimeout="20000"    

4.      redirectPort="8443"  

5.         URIEncoding="UTF-8"/>  

Manually add URIEncoding="UTF-8"

3. The problem of Chinese garbled characters in javascript url passing parameters

Option One

html page:

function testOne() {
           var url = "testTwo.action?expr="+你好;

     window.location.href = encodeURI(url);

}

Background java code:

String expr = new String(

request.getParameter("expr").getBytes("ISO-8859-1"),"UTF-8");

 Option II

html page:

function testTwo() {

var url = "testTwo.action?expr="+hello;

      window.location.href= encodeURI(encodeURI(url));

}

Background java code:

String expr = java.net.URLDecoder.decode(lrequest.getParameter("expr") , "UTF-8");

If you are using a weblogic server, you can use solution two (my version of weblogic is weblogic 9.2), but solution one cannot.

If it is a tomcat server, both of these solutions can be used; it can also be used in the background without processing the passed parameters.

String expr = new String(request.getParameter("expr").getBytes("ISO-8859-1"),"UTF-8"); 

it is also fine.

4. Java web download file file name is garbled

The first: settings  

response.setHeader("Content-Disposition", "attachment; filename=" + java.net.URLEncoder.encode(fileName, "UTF-8"));

Here, the file name is encoded into UTF-8 format, and there will be no URL error. Note that under IE6, the number of Chinese characters cannot exceed 17.

 The second: setting

response.setHeader( "Content-Disposition", "attachment;filename="  + new String( fileName.getBytes("gb2312"), "ISO8859-1" ) );

The way to encode Chinese names as ISO8859-1. However, this encoding only supports Simplified Chinese.

According to the appeal method, most Chinese problems can be solved in two ways.

 fileName = URLEncoder.encode(fileNameSrc,"UTF-8");

if(fileName.length()>150)//解决IE 6.0 bug {

      fileName=new String(fileNameSrc.getBytes("GBK"),"ISO-8859-1");

response.setHeader( "Content-Disposition", "attachment;filename="  + fileName);

}


Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326714501&siteId=291194637