1、Jsp 输出中文的乱码问题
Jsp 页面头部加上:<%@ page contentType="text/html;charset=UTF-8" %>
head 部分加上:<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
2、获取表单提交的数据时的中文乱码问题
由于 Tomact 的 J2EE 实现对表单提交,即以 post 方式提交的参数采用默认的 ISO-8859-1 来
处理,所以中文就会出现乱码。
解决方法:
//过滤器设置编码过滤(SetCharacterEncodingFilter.java)
package com.sorc;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SetCharacterEncodingFilter extends HttpServlet implements Filter{
private FilterCon#g #lterCon#g;
private String encoding=null;
//Handle the passed-in FilterCon#g
public void init(FilterCon#g #lterCon#g){
this.#lterCon#g=#lterCon#g;
encoding=#lterCon#g.getInitParameter("encoding");
}
//Process the request/response pair
public void doFilter(ServletRequest request,ServletResponse response,FilterChain
#lterChain){
try{
request.setCharacterEncoding(encoding);
#lterChain.doFilter(request,response);
} catch(ServletException sx){
#lterCon#g.getServletContext().log(sx.getMessage());
} catch(IOException iox){
#lterCon#g.getServletContext().log(iox.getMessage());
}
}
//Clean up resources
public void destroy(){
}
}
//web.xml 配置过滤器方法(web.xmd)
<#lter>
<#lter-name>setcharacterencoding#lter</#lter-name>
<#lter-class>com.sorc.SetCharacterEncodingFilter</#lter-class>
<init-param>
评论5
最新资源