Several related garbled Servlet

1. Chinese garbled page

Reason: the content in response to the response will first input buffer and then passed to the browser input, so to encoder buffer and browser are provided. 8-UTF
. 1) is not used jsp, but arranged in the servlet Chinese response in the presence of typing

  • Solution: before () outputting content plus the following two lines response.getWriter
response.setCharacterEncoding("UTF-8");// 设置缓冲区编码
response.setContentType("text/html;charset=UTF-8");// 设置浏览器编码 

2) Use the jsp

  • Solution: In the jsp head write the following code:
    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

2.request garbage problem (with the request parameters in Chinese)

the reason:

  • Use browser transmission parameters is ISO-8859-1 encoding does not support Chinese
  • Tomcat high version of the new features: that is, in strict accordance with RFC 3986 specifications visit parsing, and RFC 3986 specification defines Url allowed only contain letters (a-zA-Z), numbers (0-9), -_ ~. four special characters as well as all reserved characters (RFC3986 specified in the following characters are reserved characters: * '();:! @ & = + $, / # []?)

solution:

  • First the parameters acquired by decoding the binary stream into ISO-8859-1, utf-8 and then encoded, as follows:
String usernameString = new String(username.getBytes("ISO-8859-1"),"UTF-8");// username为获取的参数
  • For high version of tomcat, modifying conf / catalina.properties under tomcat directory to find the last comment out the line #
#tomcat.util.http.parser.HttpParser.requestTargetAllow=| 

Remove and change #

tomcat.util.http.parser.HttpParser.requestTargetAllow=|{}

{} Represents the release

3. Set the attachment download Chinese name is not

The reason: different browsers will decode the attachment file name Solution: contention for different browsers encode code is as follows:

// 获取客户端信息
String agent = request.getHeader("User-Agent");
// 定义一个变量记录编码之后的名字
String filenameEncoder = "";// 新附件的文件名,用于传给浏览器
if (agent.contains("MSIE")) {
	// IE编码
	filenameEncoder = URLEncoder.encode(filename, "utf-8");  // filename 是原附件的文件名
	filenameEncoder = filenameEncoder.replace("+", " ");
} else if (agent.contains("Firefox")) {
	// 火狐编码
	BASE64Encoder base64Encoder = new BASE64Encoder();
	filenameEncoder = "=?utf-8?B?" + base64Encoder.encode(filename.getBytes("utf-8")) + "?=";// filename 是原附件的文件名
} else {
	// 浏览器编码
	filenameEncoder = URLEncoder.encode(filename, "utf-8");// filename 是原附件的文件名
}

Guess you like

Origin www.cnblogs.com/mujinjia/p/11205839.html