使用Filter对Request进行统一编码
2007-03-17 11:53
357 查看
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package test.encoding;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.IOException;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.servlet.Filter;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.servlet.FilterChain;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.servlet.FilterConfig;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.servlet.ServletException;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.servlet.ServletRequest;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.servlet.ServletResponse;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class SetCharacterEncoding implements Filter ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected String encoding=null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected FilterConfig filterconfig=null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected boolean ignore=true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void destroy() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// TODO Auto-generated method stub
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void doFilter(ServletRequest request, ServletResponse response,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
FilterChain arg2) throws IOException, ServletException ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(ignore||(request.getCharacterEncoding()==null))...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String encoding=this.selectEncoding(request);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(encoding!=null)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
request.setCharacterEncoding(encoding);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
arg2.doFilter(request, response);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void init(FilterConfig filterconfig) throws ServletException ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.filterconfig=filterconfig;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String encoding=filterconfig.getInitParameter("encoding");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String value=filterconfig.getInitParameter("ignore");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(value==null)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ignore=true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
else if(value.equalsIgnoreCase("true"))...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ignore=true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
else if(value.equalsIgnoreCase("yes"))...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ignore=true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
else...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ignore=false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String selectEncoding(ServletRequest request)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return (this.encoding);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<filter>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<filter-name>setCharacterEncoding</filter-name>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<filter-class>test.encoding.SetCharacterEncoding</filter-class>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<init-param>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<param-name>encoding</param-name>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<param-value>gb2312</param-value>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</init-param>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</filter>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<filter-mapping>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<filter-name>setCharacterEncoding</filter-name>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<url-pattern>/*</url-pattern>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</filter-mapping>
相关文章推荐
- 使用Filter对Request进行统一编码
- 在后端是PHP程序的情况下,保持前端Javascript和PHP之间传值的统一编码可以使用以下函数进行处理:
- Filter的原理与使用(对用户进行统一验证)
- 通过Filter进行统一编码的实现
- 在非英文字符集的页面上,如果使用Ajax方式进行数据交互的话,就必须要注意保证前后端数据的统一编码,否则,很容易就出现乱码!
- 使用filter来改变request的编码
- Laravel中使用FormRequest进行表单验证方法及问题汇总
- jsp中使用select,并实现通过request中的参数进行默认选中
- PHP中使用Filter进行数据安全过滤
- Spring Boot实战之Filter实现使用JWT进行接口认证
- 使用 libjpeg进行编码之一:建立libjpeg开发环境(X86平台)
- 第十七天dbutils的使用------Commons DbUtils(Apache)第三方的:只是对JDBC编码进行了简单的封装
- java web使用filter进行全站压缩的原理及实现
- Android使用系统API进行音视频编码
- 为什么采用filter实现了字符集的统一编码
- Java Servlet的request使用的编码引发的思考 以及解决方法
- 使用ffmpeg进行h.264编码
- JBoss 系列十九:使用JGroups构建块RspFilter对群组通信返回消息进行过滤
- 防止出现乱码,J2EE项目统一使用UTF-8编码设置方法(转)
- ios 如何使用 ASIHttpRequest进行http通信