java web编程中解决字符的另一招(RequestProcessor) 博客分类: 技术 编程WebJavaServletXML
程序员文章站
2024-02-16 15:15:04
...
以前我都是用Filter (servlet)过滤器去 解决中文乱码(字符集)的问题 ,需要在web.xml配置2处。
但是今天 发现 RequestProcessor也可以解决字符集问题。 只需在web.xml种 配置一处 即可。
在此做个备忘!
xml
---------------------------------
// 将这个filter放置在服务中
在web.xml文件中加入以下代码
但是今天 发现 RequestProcessor也可以解决字符集问题。 只需在web.xml种 配置一处 即可。
在此做个备忘!
java 代码
- MyRequestProcessor.java
- package StudyNews;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.struts.action.RequestProcessor;
- import org.apache.struts.config.ForwardConfig;
- import org.apache.commons.logging.Log;
- public class MyRequestProcessor extends RequestProcessor {
- public MyRequestProcessor() {}
- protected boolean processPreprocess( HttpServletRequest request,
- HttpServletResponse response ){
- try{
- request.setCharacterEncoding("latin1");//此处为自己用的数据库的字符集 <4>
- }
- catch(Exception ex){
- System.out.println("字符集设置失败");
- }
- return true;
- }
- }
xml 代码
- <controller processorClass="StudyNews.MyRequestProcessor" />
---------------------------------
java 代码
- 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 SetCharacterEncodingFilter implements Filter {
- /*
- * (非 Javadoc)
- *
- * @see javax.servlet.Filter#destroy()
- */
- public void destroy() {
- this.encoding = null;
- this.filterConfig = null;
- }
- public void doFilter(ServletRequest request, ServletResponse response,
- FilterChain chain) throws IOException, ServletException {
- // 选择使用的字符编码
- if (ignore ¦ ¦ (request.getCharacterEncoding() == null)) {
- String encoding = selectEncoding(request);
- if (encoding != null) {
- request.setCharacterEncoding(encoding);
- }
- // 将控制权交给下一个Filter
- chain.doFilter(request, response);
- }
- }
java 代码
- /*
- * (非 Javadoc)
- *
- * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
- */
- public void init(FilterConfig filterConfig) throws ServletException {
- 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);
- }
- protected String encoding = null;
- protected FilterConfig filterConfig = null;
- protected boolean ignore = true;
- }
xml 代码
xml 代码
- <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>