Home > Web Front-end > JS Tutorial > Solving the problem of garbled characters when passing js and URL containing Chinese parameters

Solving the problem of garbled characters when passing js and URL containing Chinese parameters

巴扎黑
Release: 2016-12-20 11:46:51
Original
1378 people have browsed it

1. Use code to complete the character set modification

Method (1):
html page:

function testOne() {

var url = "testOne_test.do?expr="+Hello;

location = encodeURI( url);

}

Backend java code:

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

method (2):

html page:

function testOne() {
var url = "testOne_test.do?expr="+Hello;
location = encodeURI(encodeURI(url));
}

Backend java code :
String expr = java.net.URLDecoder.decode(lrequest.getParameter("expr") , "UTF-8");
2. Modify the configuration parameters in tomcat

Find server.xml under tomcat
< Connector port="8080" maxThreads="150" minSpareThreads="25" maxSpareThreads="75" URIEncoding="GBK">
Modify to UTF-8 and other character sets as needed.工3, add Spring.jar to the web engineering, use the Spring's CharacterencodingFilter

View Plaincopy to Clipboardprint? ding & lt;/file-name & gt;
& lt; file-class & gt; ORG.SpringFramework.Web.filter.CharacterenCodingFilter & LT;/Filter-Class & GT;
& LT; Init-Param & GT;
& LT; Param-Name & GT; Encoding & lt;/param-name & gt;
& lt; param-value & gt; utf-8 & lt;/param- value>
                                                ;/*
The transcoding part in



org.springframework.web.filter.CharacterEncodingFilter:

view plaincopy to clipboardprint?

protected void doFilterInternal(
ttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {

if (this.encoding != null && (this.forceEncoding || request.getCharacterEncoding() == null)) {
request.setCharacterEncoding(this.encoding);
if ( this.forceEncoding && responseSetCharacterEncodingAvailable) {
  response.setCharacterEncoding(this.encoding);
  }
  }
  filterChain.doFilter(request, response);
  }

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template