首页 > 编程 > JSP > 正文

jsp乱码问题解决方案

2019-11-17 04:05:55
字体:
来源:转载
供稿:网友
view plaincopy to clipboardPRint?
1,页面上编码设置为"UTF-8"    
2,写一个字符编码过滤器, 在web.xml中配置加载过滤器   
3,设置tomcat的属性 在配置端口的节点上加入如下代码:URIEncoding="UTF-8"  
  
1,代码如下:    
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>    
2,实现代码:    
package com.mww.comm;    
  
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 javax.servlet.http.HttpServletRequest;    
import javax.servlet.http.HttpServletResponse;    
  
public class checkEcodingFelter implements Filter {    
  
public void destroy() {    
// TODO Auto-generated method stub    
  
}    
  
public void doFilter(ServletRequest servletrequest,    
ServletResponse servletresponse, FilterChain filterChain)    
throws IOException, ServletException {    
HttpServletRequest request = (HttpServletRequest) servletrequest;    
HttpServletResponse response = (HttpServletResponse) servletresponse;    
request.setCharacterEncoding("UTF-8");    
response.setCharacterEncoding("UTF-8");    
response.setContentType("text/html;charset=UTF-8");    
filterChain.doFilter(servletrequest, servletresponse);    
}    
  
public void init(FilterConfig arg0) throws ServletException {    
// TODO Auto-generated method stub    
  
}    
  
}    
在web.xml中配置过滤器:    
  <!-- 字符编码过滤 -->    
<filter>    
<filter-name>checkEcodingFelter </filter-name>   
<filter-class>com.mww.comm.checkEcodingFelter </filter-class> <!-- 配置过滤器类 包名+类名 -->   
</filter>    
<filter-mapping>    
<filter-name>checkEcodingFelter </filter-name>    
<url-pattern>/* </url-pattern> <!-- 过滤所有页面-->   
</filter-mapping>    
3,在tomcat配置端口的节点上配置该属性URIEncoding="UTF-8" :    
<Connector port="8888" protocol="HTTP/1.1"    
              connectionTimeout="20000"    
              redirectPort="8443"    
  URIEncoding="UTF-8"/>    
  
按这一套下来乱码问题解决.  
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表