1 借助spring web.xml中增加
如果不用spring怎么办呢?
配置文件,然后呢,转码的过滤器就要自己写咯
或
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
如果不用spring怎么办呢?
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>com.rda.commons.filter.ChineseFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
配置文件,然后呢,转码的过滤器就要自己写咯
package com.filter;
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.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* 创建人:ayanline
* 创建时间:2008-9-21
* 类作用:中文转码过滤器
*
*/
@SuppressWarnings("serial")
public class ChineseFilter extends HttpServlet implements Filter
{
private FilterConfig filterConfig;
public void init(FilterConfig filterConfig) throws ServletException
{
this.filterConfig = filterConfig;
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
{
try
{
String encoding = filterConfig.getInitParameter("encoding");
//从WEB.xml配置文件中取出参数,这样我们可以通过配置修改编码格式.
request.setCharacterEncoding(encoding);//设置请求的编码格式
filterChain.doFilter(request, response);
}
catch (ServletException sx)
{
filterConfig.getServletContext().log(sx.getMessage());
}
catch (IOException iox)
{
filterConfig.getServletContext().log(iox.getMessage());
}
}
public void destroy()
{
}
protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException
{
super.doGet(arg0, arg1);
}
protected void doPost(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException
{
super.doPost(arg0, arg1);
}
/**
* @function 验证数据是否为空,如果为空则转换
* @param param
* @return String
*/
public String checkNull(String param)
{
if (param == null || param.equals(""))
{
return "";
}
else
{
return param;
}
}
}
或
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.apache.struts2.dispatcher.FilterDispatcher;
public class NewFilterDispatcher extends FilterDispatcher {
private static String encoding = "utf-8";
public void init(FilterConfig filterConfig) throws ServletException {
super.init(filterConfig);
String encodingParam = filterConfig.getInitParameter("encoding");
if (encodingParam != null && encodingParam.trim().length() != 0) {
encoding = encodingParam;
}
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding(encoding);
super.doFilter(request, response, chain);
}
}