怎样在Struts 项目中编写编码过滤器! 还有配置web.xml

y2j3 2008-10-24 04:01:29
比如 我的 actionform 中有 两个属性: String username, String password,
如何写一个CharsetFilter implements Filter类过滤下 使其解决在表单输入中文 在显示页面却是乱玛的问题,
只要有 写好 CharsetFilter 可以直接把源代码发上来 谢谢!!
...全文
247 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
编程小强 2012-01-01
  • 打赏
  • 举报
回复
其实只需要简单几行就够了
public void doFilter(ServletRequest req,
ServletResponse res, FilterChain chain)
throws IOException, ServletException {
// TODO Auto-generated method stub
//HttpServletResponse response = (HttpServletResponse)res;
//response.setCharacterEncoding("utf-8");
req.setCharacterEncoding("utf-8");
chain.doFilter(req, res);


}
web.xml下:
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>com.demo.servlet.filter.EncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
就可以了!!!
y2j3 2008-10-24
  • 打赏
  • 举报
回复
谢谢大家了!!!
hy0231 2008-10-24
  • 打赏
  • 举报
回复
写一个类继承filter,然后在web.xml中配置.
request.setCharacterEncoding("GBK");
耶律火柴 2008-10-24
  • 打赏
  • 举报
回复
public class CharsetFilter
implements Filter
{

protected String encoding;
protected FilterConfig filterConfig;
protected boolean ignore;

public CharsetFilter()
{
encoding = null;
filterConfig = null;
ignore = true;
}

public void destroy()
{
encoding = null;
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;
encoding = filterConfig.getInitParameter("encoding");

String value = filterConfig.getInitParameter("ignore");
if(value == null)
ignore = true;
else
if(value.equalsIgnoreCase("true"))
ignore = true;
else
if(value.equalsIgnoreCase("yes"))
ignore = true;
else
ignore = false;
}

protected String selectEncoding(ServletRequest request)
{
return encoding;
}
}

在 web.xml 配置
<filter>
<filter-name>CharsetFilter</filter-name>
<filter-class>com.customFilter.CharsetFilter</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>
<filter-mapping>
<filter-name>CharsetFilter</filter-name>
<servlet-name>action</servlet-name>
</filter-mapping>

忙碌的布谷鸟 2008-10-24
  • 打赏
  • 举报
回复
package filters;

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;

/**
* Set All HttpRequest Encoding
*
*/
public class SetEncodingFilter implements Filter {
/**
* The default character encoding to set for requests that pass through this
* filter.
*/
protected String encoding = null;

/**
* The filter configuration object we are associated with. If this value is
* null, this filter instance is not currently configured.
*/
protected FilterConfig filterConfig = null;

/**
* Should a character encoding specified by the client be ignored?
*/
protected boolean ignore = true;

/**
* Take this filter out of service.
*/
public void destroy() {
this.encoding = null;
this.filterConfig = null;
}

/**
* Select and set (if specified) the character encoding to be used to
* interpret request parameters for this request.
*
* @param request
* The servlet request we are processing
* @param result
* The servlet response we are creating
* @param chain
* The filter chain we are processing
*
* @exception IOException
* if an input/output error occurs
* @exception ServletException
* if a servlet error occurs
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
if (ignore || (request.getCharacterEncoding() == null)) {
request.setCharacterEncoding(selectEncoding(request));
}
chain.doFilter(request, response);
}

/**
* Place this filter into service.
*
* @param filterConfig
* The filter configuration object
*/
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")
|| value.equalsIgnoreCase("yes"))
this.ignore = true;
else
this.ignore = false;
}

/**
* Select an appropriate character encoding to be used, based on the
* characteristics of the current request and/or filter initialization
* parameters. If no character encoding should be set, return
* <code>null</code>. <p> The default implementation unconditionally returns
* the value configured by the <strong>encoding</strong> initialization
* parameter for this filter.
*
* @param request
* The servlet request we are processing
*/
protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}

/**
* Returns the filterConfig.
*
* @return FilterConfig
*/
public FilterConfig getFilterConfig() {
return filterConfig;
}

/**
* Sets the filterConfig.
*
* @param filterConfig
* The filterConfig to set
*/
public void setFilterConfig(FilterConfig filterConfig) {
this.filterConfig = filterConfig;
}

}

web.xml中添加

<filter>
<filter-name>SetCharsetEncodingFilter</filter-name>
<filter-class>
filters.SetEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>gbk</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SetCharsetEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧