『壹』 不用spring怎麼寫filter來解決中文亂碼的問題,謝謝!在線等
可以編寫一個Filter
public class EncodingFilter implements Filter {
/** 編碼 */
String encoding = null;
/** 銷毀編碼 */
public void destroy() {
this.encoding = null;
}
/**
* 執行過濾鏈,對請求和相應設置編碼
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
if (encoding != null) {
// 對請求進行編碼設置
request.setCharacterEncoding(encoding);
response.setCharacterEncoding(encoding);
}
// 將處理權轉交給下一個處理器
chain.doFilter(request, response);
}
/**
* 初始化編碼,從配置文件中獲取編碼的值
*/
public void init(FilterConfig filterConfig) throws ServletException {
this.encoding = filterConfig.getInitParameter("encoding");
}
}
需要再Web.xm中注冊攔截器
<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>com.sato.filter.EncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
『貳』 為什麼使用了SPRING 過濾器還會出現中文亂碼
MVC的過濾器如果優先於spring的編碼過濾器就會出現亂碼情況 需要將spring的編碼過濾器優先等級提高
『叄』 如何配置Filter過濾器處理JSP中文亂碼
注意問題:在學慣用selvert的過濾器filter處理中文亂碼時,在filter配置初始化時用了utf-8處理中文亂碼,而在提交的jsp頁面中卻用了gbk。雖然兩種都可以出來中文亂碼,但是卻造成了處理亂碼的格式不一致。所以編譯出錯。
解決方法:所有地方都用utf-8或gbk
//過濾器類
CharactorFilter.jsp
package cn.com.Filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class CharactorFilter implements Filter { //繼承Filter類
//字元編碼
String encoding=null;
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
if(encoding!=null){
//設置request字元編碼
request.setCharacterEncoding(encoding);
//設置response字元編碼
response.setContentType("text/html;charset="+encoding);
}
//傳遞給下一個過濾器
chain.doFilter(request, response);
}
public void init(FilterConfig filterConfig) throws ServletException {
//獲取初始化參數
encoding=filterConfig.getInitParameter("encoding");
}
public void destroy() {
// TODO Auto-generated method stub
encoding=null;
}
}
web.xml
<filter> <!--注意這里是filter,不要配置成servlet-->
<filter-name>CharactorFilter</filter-name> <!--過濾器名稱-->
<filter-class>cn.com.Filter.CharactorFilter</filter-class> <!--過濾器的完整類名-->
<init-param> <!--初始化參數-->
<param-name>encoding</param-name> <!--參數名稱-->
<param-value>utf-8</param-value> <!--參數值-->
</init-param>
</filter>
<filter-mapping> <!--過濾器映射-->
<filter-name>CharactorFilter</filter-name><!--過濾器名稱-->
<url-pattern>/*</url-pattern><!--URL映射,給所有頁面處理亂碼-->
</filter-mapping>
『肆』 struts2 中文亂碼問題
處理中文亂碼的filter網上找一個,然後再web.xml里配置,注意這個filter要放到struts2的filter的前面,這樣才先經過他。你直接放到最web.xml的最前面就是了。如果在後面struts2都處理,才輪到中文filter當然不行了。
<constant name="struts.i18n.encoding" value="GBK" /> 只有在struts2。1.8更新的版本才能用。你可以不用spring的過濾器,你網站找一個filter的類,放到項目里,配置一下。我建議用UTF-8的國際編碼。頁面開頭的編碼類型不管用,還有網也提交數據用表單,別只用url來提交。
『伍』 jsp Charset首字母大寫能解決亂碼
設置一下編碼:
1。全局編碼設置:編碼設置的方法:ToolBar-->Window-->Preferences-->General-->Workspace-->Text file encoding,設置合適的編碼。
2。局部編碼設置:在源碼按右鍵-->General-->Editors-->Test Editors-->Spelling-->Encoding,這里是設置單個文件的編碼。
推薦還是使用全局編碼設置吧
MyEclipse編碼設置:
Windows-->Preferences-->MyEclipse(Enterprise Workbench)--> Files and Editors-->JSP-->右Encoding->(UTF-8)-->Update
文件默認編碼設置:
Windows-->Preferences-->General-->Content Types, 然後在右邊上面的框中打開Text, 選中Java Source File (你看到下面的框中有個*.java 就對了), 然後在下面的「Default edcodng」文本框中輸入「UTF-8」, 點「Update」,就OK了。(什麼文件的編碼都可以在這里設置!)
其它的Edit-SetEnCoding 和Project-->Properties-->Resource-->Text File Encoding設置成繼承自容器就可以了。
3。Window-Preferences-General-Content Types-Text-Java Source File-UTF-8
『陸』 如何解決在Servlet向資料庫寫記錄時中文亂碼
可以用filter過濾器對所有的servlet進行過濾,在過濾器里處理字元編碼,對所有的字元都是用utf-8
public
void
doFilter(ServletRequest
request,
ServletResponse
response,FilterChain
chain)
throws
IOException,ServletException
{
HttpServletRequest
httpRequest
=
(HttpServletRequest)
request;
httpRequest.setCharacterEncoding("utf-8");
chain.doFilter(request,
response);
}
『柒』 maven filter 中文亂碼,應該用什麼思路解決
1、查看被fitler的文件是否帶utf-8的Bom頭,去掉bom頭,帶了有可能報錯
2、windows maven的war插件的版本,2.1-bata-1有問題,它的編碼識別有問題,懷疑是按照iso-5589-1解析,fitler後的文件16進制查看為3f,造成黑洞現象
3、網上很多解決方式是改成gbk,這個是一種解決方案,如果希望繼續使用utf-8編碼的,請修改war的filter,還有不要忘記resource插件的編碼設置。
『捌』 關於java過濾器解決中文亂碼的
因為你襲遺漏了重要的一步。正確的方法如下:
if(encoding!=null){
//設置request字元編碼
request.setCharacterEncoding(encoding);
//設置response字元編碼
response.setContentType("text/html;charset="+encoding);
response.setCharacterEncoding(encoding);
}
//傳遞給下一個過濾器
chain.doFilter(request,response);
『玖』 struts2中如何用過濾器處理中文亂碼
<!-- 亂碼過濾器 -->
<filter>
<filter-name>CharaterencodeFilter</filter-name>
<filter-class>com.CharaterEncodeFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CharaterencodeFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
放在web.xml里
相應的類也給你
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter;
public class CharaterEncodeFilter implements Filter {
private FilterConfig config = null;
public void destroy() {
this.config = config;
}
public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
chain.doFilter(request, response);
}
public void init(FilterConfig arg0) throws ServletException {
config = null;
}
}