国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
java 解決字符集的亂碼問題

struts1中文亂碼解決辦法

1.首先jsp頁面要使用UTF-8編碼,個(gè)人建議將pageEncodingcontentType中的編碼全部設(shè)置為UTF-8

2.修改tomcat配置文件server.xml,添加一個(gè)屬性[紅色部分],如下

       <Connector
               port="8080"               maxHttpHeaderSize="8192"
               maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
               enableLookups="false" redirectPort="8443" acceptCount="100"
               connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="UTF-8" />

3.編寫過濾器類

Java代碼

1.      public class SetCharacterEncodingFilter implements Filter {   

2.        

3.          protected String encoding = null;   

4.        

5.          protected FilterConfig filterConfig = null;   

6.        

7.          protected boolean ignore = true;   

8.        

9.          /*  

10.        * (non-Javadoc)  

11.        *   

12.        * @see javax.servlet.Filter#destroy()  

13.        */  

14.       public void destroy() {   

15.     

16.           this.encoding = null;   

17.           this.filterConfig = null;   

18.     

19.       }   

20.     

21.       /*  

22.        * (non-Javadoc)  

23.        *   

24.        * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,  

25.        *      javax.servlet.ServletResponse, javax.servlet.FilterChain)  

26.        */  

27.       public void doFilter(ServletRequest request, ServletResponse response,   

28.               FilterChain chain) throws IOException, ServletException {   

29.     

30.           // Conditionally select and set the character encoding to be used   

31.           if (ignore || (request.getCharacterEncoding() == null)) {   

32.               String encoding = selectEncoding(request);   

33.               if (encoding != null)   

34.                   request.setCharacterEncoding(encoding);   

35.           }   

36.     

37.           // Pass control on to the next filter   

38.           chain.doFilter(request, response);   

39.       }   

40.     

41.       /*  

42.        * (non-Javadoc)  

43.        *   

44.        * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)  

45.        */  

46.       public void init(FilterConfig filterConfig) throws ServletException {   

47.     

48.           this.filterConfig = filterConfig;   

49.           this.encoding = filterConfig.getInitParameter("encoding");   

50.           String value = filterConfig.getInitParameter("ignore");   

51.           if (value == null)   

52.               this.ignore = true;   

53.           else if (value.equalsIgnoreCase("true"))   

54.               this.ignore = true;   

55.           else if (value.equalsIgnoreCase("yes"))   

56.               this.ignore = true;   

57.           else  

58.               this.ignore = false;   

59.     

60.       }   

61.     

62.       /*  

63.        *   

64.        */  

65.       protected String selectEncoding(ServletRequest request) {   

66.     

67.           return (this.encoding);   

68.     

69.       }   

70.     

71.   }  

public class SetCharacterEncodingFilter implements Filter {

 

protected String encoding = null;

 

protected FilterConfig filterConfig = null;

 

protected boolean ignore = true;

 

/*

 * (non-Javadoc)

 *

 * @see javax.servlet.Filter#destroy()

 */

public void destroy() {

 

  this.encoding = null;

  this.filterConfig = null;

 

}

 

/*

 * (non-Javadoc)

 *

 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,

 *      javax.servlet.ServletResponse, javax.servlet.FilterChain)

 */

public void doFilter(ServletRequest request, ServletResponse response,

   FilterChain chain) throws IOException, ServletException {

 

  // Conditionally select and set the character encoding to be used

  if (ignore || (request.getCharacterEncoding() == null)) {

   String encoding = selectEncoding(request);

   if (encoding != null)

    request.setCharacterEncoding(encoding);

  }

 

  // Pass control on to the next filter

  chain.doFilter(request, response);

}

 

/*

 * (non-Javadoc)

 *

 * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)

 */

public void init(FilterConfig filterConfig) throws ServletException {

 

  this.filterConfig = filterConfig;

  this.encoding = filterConfig.getInitParameter("encoding");

  String value = filterConfig.getInitParameter("ignore");

  if (value == null)

   this.ignore = true;

  else if (value.equalsIgnoreCase("true"))

   this.ignore = true;

  else if (value.equalsIgnoreCase("yes"))

   this.ignore = true;

  else

   this.ignore = false;

 

}

 

/*

 *

 */

protected String selectEncoding(ServletRequest request) {

 

  return (this.encoding);

 

}

 

}

 4.web.xml中添加下列代碼

 

Xml代碼

1.      <filter>  

2.              <filter-name>Encoding</filter-name>  

3.              <filter-class>  

4.                  struts.util.SetCharacterEncodingFilter   

5.              </filter-class>  

6.              <init-param>  

7.                  <param-name>encoding</param-name>  

8.                  <param-value>UTF-8</param-value>  

9.              </init-param>  

10.       </filter>  

11.       <filter-mapping>  

12.           <filter-name>Encoding</filter-name>  

13.           <url-pattern>/*</url-pattern>  

14.       </filter-mapping>  

看了一下樓主鏈接中的filter代碼,應(yīng)該沒問題的。

如果不加filter,頁面采用utf-8,的確會(huì)在java代碼中出現(xiàn)中文亂碼的情況。

然后我就寫了一個(gè)filter,大致跟鏈接中的內(nèi)容一致,然后就解決了亂碼問題。

我貼一下我的代碼吧

web.xml代碼

<filter>

   <filter-name>charactarFileter</filter-name>

   <filter-class>com.yourcompany.struts.CharactarEncodingFilter</filter-class>

  </filter>

  <filter-mapping>

   <filter-name>charactarFileter</filter-name>

   <url-pattern>/*</url-pattern>

  </filter-mapping>

filter實(shí)現(xiàn)代碼

package com.yourcompany.struts;

 

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 CharactarEncodingFilter implements Filter{

 

 public void destroy() {

 }

 

 public void doFilter(ServletRequest request, ServletResponse response,

   FilterChain chain) throws IOException, ServletException {

  request.setCharacterEncoding("utf-8");

  chain.doFilter(request, response);

 }

 

 public void init(FilterConfig arg0) throws ServletException {

 }

 

}

就這樣,寫好后重新發(fā)布項(xiàng)目,運(yùn)行,解決問題。

 

 

 

 

 

1.修改Tomcatconf/server.xml文件中 <Connector
port="8080" ...
處添加URIEncoding="UTF-8"。

2.
編寫一個(gè)過濾器類CharacterEncodingFilter

package com.lyb.example;

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 CharacterEncodingFilter implements Filter {

public void destroy() {
// TODO Auto-generated method stub

}

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding("UTF-8");
chain.doFilter(request,response);
// TODO Auto-generated method stub

}

public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub

}

}


3.
web.xml中添加過濾器映射
<filter>
  <filter-name>characterEncoding</filter-name>
  <filter-class>com.lyb.example.CharacterEncodingFilter</filter-class>
  </filter>
  <filter-mapping>
  <filter-name>characterEncoding</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>

OK
。重新啟動(dòng)Tomcat即可

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊舉報(bào)
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Filter技術(shù)的應(yīng)用(轉(zhuǎn))
Mysql與JSP網(wǎng)頁中文亂碼問題的解決方案
Servlet中文亂碼解決
幾個(gè)有用的Servlet過濾器
Servlet過濾器和監(jiān)聽器知識總結(jié)
一個(gè)servlet登陸過濾器
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服