java web编程中解决字符的另一招(RequestProcessor)

以前我都是用Filter (servlet)过滤器去 解决中文乱码(字符集)的问题 ,需要在web.xml配置2处。
但是今天 发现 RequestProcessor也可以解决字符集问题。 只需在web.xml种 配置一处 即可。
在此做个备忘!
java 代码
  1. MyRequestProcessor.java
  2. package StudyNews;
  3. import javax.servlet.http.HttpServlet;
  4. import javax.servlet.http.HttpServletRequest;
  5. import javax.servlet.http.HttpServletResponse;
  6. import org.apache.struts.action.RequestProcessor;
  7. import org.apache.struts.config.ForwardConfig;
  8. import org.apache.commons.logging.Log;
  9. public class MyRequestProcessor extends RequestProcessor {
  10. public MyRequestProcessor() {}
  11. protected boolean processPreprocess( HttpServletRequest request,
  12. HttpServletResponse response ){
  13. try{
  14. request.setCharacterEncoding("latin1");//此处为自己用的数据库的字符集 <4>
  15. }
  16. catch(Exception ex){
  17. System.out.println("字符集设置失败");
  18. }
  19. return true;
  20. }
  21. }
xml
xml 代码
  1. <controller processorClass="StudyNews.MyRequestProcessor" />

---------------------------------
java 代码
  1. import java.io.IOException;
  2. import javax.servlet.Filter;
  3. import javax.servlet.FilterChain;
  4. import javax.servlet.FilterConfig;
  5. import javax.servlet.ServletException;
  6. import javax.servlet.ServletRequest;
  7. import javax.servlet.ServletResponse;
  8. public class SetCharacterEncodingFilter implements Filter {
  9. /*
  10. * (非 Javadoc)
  11. *
  12. * @see javax.servlet.Filter#destroy()
  13. */
  14. public void destroy() {
  15. this.encoding = null;
  16. this.filterConfig = null;
  17. }
  18. public void doFilter(ServletRequest request, ServletResponse response,
  19. FilterChain chain) throws IOException, ServletException {
  20. // 选择使用的字符编码
  21. if (ignore ¦ ¦ (request.getCharacterEncoding() == null)) {
  22. String encoding = selectEncoding(request);
  23. if (encoding != null) {
  24. request.setCharacterEncoding(encoding);
  25. }
  26. // 将控制权交给下一个Filter
  27. chain.doFilter(request, response);
  28. }
  29. }
// 将这个filter放置在服务中
java 代码
  1. /*
  2. * (非 Javadoc)
  3. *
  4. * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
  5. */
  6. public void init(FilterConfig filterConfig) throws ServletException {
  7. this.encoding = filterConfig.getInitParameter( "encoding ");
  8. String value = filterConfig.getInitParameter( "ignore ");
  9. if (value == null)
  10. this.ignore = true;
  11. else if (value.equalsIgnoreCase( "true "))
  12. this.ignore = true;
  13. else if (value.equalsIgnoreCase( "yes "))
  14. this.ignore = true;
  15. else
  16. this.ignore = false;
  17. }
  18. protected String selectEncoding(ServletRequest request) {
  19. return (this.encoding);
  20. }
  21. protected String encoding = null;
  22. protected FilterConfig filterConfig = null;
  23. protected boolean ignore = true;
  24. }
在web.xml文件中加入以下代码
xml 代码
xml 代码
 
  1. <filter>  <filter-name> SetCharacterEncodingFilter </filter-name>  <filter-class> SetCharacterEncodingFilter </filter-class>  <init-param>  <param_name> encoding </param_name>  <param-value> GBK </param-value>  </init-param>  </filter>  <filter-mapping>  <filter-name> SetCharacterEncodingFilter </filter-name>  <url_pattern> /* </url_pattern>  </filter-mapping>  

你可能感兴趣的:(java,编程,Web,xml,servlet)