package com.ogi.filter;import javax.servlet.*;
import java.io.*;
/**
* @author guest1
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*
* setup on web.xml
*<filter>
*<filter-name>Set Character Encoding</filter-name>
*<filter-class>com.learndiary.website.util.SetCharacterEncodingFilter</filter-class>
*<init-param>
*<param-name>encoding</param-name>
*<param-value>GB2312</param-value>
*</init-param>
*<init-param>
*<param-name>ignore</param-name>
*<param-value>true</param-value>
*</init-param>
*</filter>
*/
public abstract class SetCharacterEncodingFilter implements Filter{
protected String encoding = null;
protected FilterConfig filterConfig = null;
protected boolean ignore = true;
public void destory(){
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);
}
chain.doFilter(request,response);
}
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;
}
}
public String selectEncoding(ServletRequest request){
return this.encoding;
}
}注释中是你的web.xml的配置方法。
import java.io.*;
/**
* @author guest1
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*
* setup on web.xml
*<filter>
*<filter-name>Set Character Encoding</filter-name>
*<filter-class>com.learndiary.website.util.SetCharacterEncodingFilter</filter-class>
*<init-param>
*<param-name>encoding</param-name>
*<param-value>GB2312</param-value>
*</init-param>
*<init-param>
*<param-name>ignore</param-name>
*<param-value>true</param-value>
*</init-param>
*</filter>
*/
public abstract class SetCharacterEncodingFilter implements Filter{
protected String encoding = null;
protected FilterConfig filterConfig = null;
protected boolean ignore = true;
public void destory(){
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);
}
chain.doFilter(request,response);
}
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;
}
}
public String selectEncoding(ServletRequest request){
return this.encoding;
}
}注释中是你的web.xml的配置方法。
我用了你的方法,为什么提示Exception starting filter Set Character Encoding: java.lang.InstantiationException
request.setCharacterEncoding("gb2312");
request.getParameter("name");
%>
*<filter-class>com.ogi.filter.SetCharacterEncodingFilter</filter-class>
而且我的数据直接传给form,这样的话即使在action中request.setCharacterEncoding("gb2312");也没有效果了吧
我改了类名的,改成我自己的包,就是不行
在web.xml中指定action类为自己的类,如下: <servlet-name>action</servlet-name>
<servlet-class>com.beltino.util.EncodingActionServlet</servlet-class>/*
* 2004-8-18
* [email protected]
*/
package com.beltino.util;import org.apache.struts.action.*;import java.io.IOException;import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.util.RequestUtils;
/**
* @author jbas
*/
public class EncodingActionServlet extends ActionServlet { protected void process(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
request.setCharacterEncoding("gbk");
response.setContentType("text/html;charset=gbk"); RequestUtils.selectModule(request, getServletContext());
getRequestProcessor(getModuleConfig(request)).process
(request, response); }}在每个页面的头上加入:
<%@ page contentType="text/html; charset=gbk" %>
<% request.setCharacterEncoding("gbk"); %>如果用的是tomcat,为了解决url乱码的问题,还需要在tomcat的server.xml的
<Connector URIEncoding="gbk" acceptCount="100" connectionTimeout="20000" disableUploadTimeout="true" port="8080" redirectPort="8443" maxSpareThreads="75" maxThreads="150" minSpareThreads="25">
</Connector>
中加进去URIEncoding="gbk"it's ok!
<Connector URIEncoding="gbk" acceptCount="100" connectionTimeout="20000" disableUploadTimeout="true" port="8080" redirectPort="8443" maxSpareThreads="75" maxThreads="150" minSpareThreads="25">
</Connector>
中加进去URIEncoding="gbk"不知你看到了没有?