Tomcat

tomcat_8 之前的版本編碼格式爲iso-8859-1 get請求中文亂碼,須要在tomcat的server.xml文件中增長UTF-8 編碼,html

 

<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="UTF-8"/>java

 

tomcat_8 編碼格式是 UTF-8 因此不須要修改web

 

 

Tomcat真正跑的目錄spring

validateJarFile(/Users/wenyunli/Documents/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/biz-oss-baseapache

 

 

 完全解決Spring MVC 中文亂碼問題tomcat

 

1:表單提交controller得到中文參數後亂碼解決方案app

注意:  jsp頁面編碼設置爲UTF-8less

form表單提交方式爲必須爲postget方式下面spring編碼過濾器不起效果eclipse

 

[html] view plain copywebapp

 

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  

[html] view plain copy

 

  1. <form action="${ctx}/user/addUser" name="userForm" method="post">    

 

 

修改web.xml,增長編碼過濾器,以下(注意,須要設置forceEncoding參數值爲true

 

[html] view plain copy

 

  1. <filter>  
  2.         <filter-name>characterEncodingFilter</filter-name>  
  3.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
  4.         <init-param>  
  5.             <param-name>encoding</param-name>  
  6.             <param-value>UTF-8</param-value>  
  7.         </init-param>  
  8.         <init-param>  
  9.             <param-name>forceEncoding</param-name>  
  10.             <param-value>true</param-value>  
  11.         </init-param>  
  12.     </filter>  
  13.     <filter-mapping>  
  14.         <filter-name>characterEncodingFilter</filter-name>  
  15.         <url-pattern>/*</url-pattern>  
  16.     </filter-mapping>  

 

截圖

 

 

 

tomcat 8 

https://tomcat.apache.org/tomcat-8.0-doc/config/http.html

 

This specifies the character encoding used to decode the URI bytes, after %xx decoding the URL. If not specified, UTF-8 will be used unless the org.apache.catalina.STRICT_SERVLET_COMPLIANCE system property is set to true in which case ISO-8859-1 will be used.

 

tomcat 7

 

tomcat的 post和get解碼方式是不一致的, 因此post沒問題,保持編碼和解碼一致就好了,

不要使用這種方式對參數進行編碼  new String(value.getBytes(「ISO-8859-1」), param); Tomcat7對URI默認編碼是ISO-8859-1,Tomcat8對URI默認編碼是UTF-8 

使用new String(value.getBytes(「ISO-8859-1」), param);這種方式對tomcat7 能夠解決get亂碼問題, 若是遷移到tomcat8就有問題了  

正確的行爲:server.xml配置上URIEncoding=「UTF-8」

 

tomcat中,保證get數據採用UTF8編碼,在server.xml中進行了以下設置:

加:URIEncoding="UTF-8"

 

<Connector port="8080" maxThreads="150"minSpareThreads="25"

maxSpareThreads="75" enableLookups="false"redirectPort="8443"

acceptCount="100" debug="99" connectionTimeout="20000"

disableUploadTimeout="true"URIEncoding="UTF-8"/>

 

指定了get時候的數據編碼。當使用IIS做爲webserver轉發servlet/jsp請求給Tomcat時候,這個設置卻失效

其實緣由很簡單:IIS是經過AJP協議,把請求轉發到Tomcat監聽的8009端口上的,因此這裏針對8080的設置天然就無效

正確的方法是進行下面的設置:

<Connector port="8009" enableLookups="false"redirectPort="8443"

debug="0" protocol="AJP/1.3"URIEncoding="UTF-8"/>

 

查看8080端口的進程:

 lsof -i:8080

殺進程

kill -9 PID

相關文章
相關標籤/搜索