比如说一个中文转化的问题,总是要在servlet(jsp)里面处理request里面的encoding,这样你就可以放在一个filter里package mytest;import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;public class GBKEncoding
implements Filter {
private FilterConfig filterConfig;
//Handle the passed-in FilterConfig
private static boolean debug = true;
public void init(FilterConfig filterConfig) {
this.filterConfig = filterConfig;
}
public void setFilterConfig( FilterConfig filterConfig )
{
this.filterConfig = filterConfig;
}
public FilterConfig getFilterConfig()
{
return this.filterConfig;
}
public void doBeforeProcessing(ServletRequest request,
ServletResponse response) throws IOException,
ServletException {
if (debug)
System.out.println("GBKEncoding:DoBeforeProcessing");
request.setCharacterEncoding("GBK");
} private void doAfterProcessing(ServletRequest request,
ServletResponse response) throws IOException,
ServletException {
if (debug)
System.out.println("GBKEncoding:DoAfterProcessing");
} //Process the request/response pair
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) throws IOException,
ServletException {
if (debug)
System.out.println("GBKEncoding:doFilter()");
doBeforeProcessing(request, response);
Throwable problem = null;
try {
filterChain.doFilter(request, response);
}
catch (Throwable t) {
problem = t;
t.printStackTrace();
}
doAfterProcessing(request, response); if (problem != null) {
if (problem instanceof ServletException) {
throw (ServletException) problem;
}
else if (problem instanceof IOException) {
throw (IOException) problem;
}
else {
throw new ServletException(problem);
}
} } //Clean up resources
public void destroy() {
}
}
import javax.servlet.http.*;
import java.io.*;
import java.util.*;public class GBKEncoding
implements Filter {
private FilterConfig filterConfig;
//Handle the passed-in FilterConfig
private static boolean debug = true;
public void init(FilterConfig filterConfig) {
this.filterConfig = filterConfig;
}
public void setFilterConfig( FilterConfig filterConfig )
{
this.filterConfig = filterConfig;
}
public FilterConfig getFilterConfig()
{
return this.filterConfig;
}
public void doBeforeProcessing(ServletRequest request,
ServletResponse response) throws IOException,
ServletException {
if (debug)
System.out.println("GBKEncoding:DoBeforeProcessing");
request.setCharacterEncoding("GBK");
} private void doAfterProcessing(ServletRequest request,
ServletResponse response) throws IOException,
ServletException {
if (debug)
System.out.println("GBKEncoding:DoAfterProcessing");
} //Process the request/response pair
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) throws IOException,
ServletException {
if (debug)
System.out.println("GBKEncoding:doFilter()");
doBeforeProcessing(request, response);
Throwable problem = null;
try {
filterChain.doFilter(request, response);
}
catch (Throwable t) {
problem = t;
t.printStackTrace();
}
doAfterProcessing(request, response); if (problem != null) {
if (problem instanceof ServletException) {
throw (ServletException) problem;
}
else if (problem instanceof IOException) {
throw (IOException) problem;
}
else {
throw new ServletException(problem);
}
} } //Clean up resources
public void destroy() {
}
}
解决方案 »
- hibernate怎么在类里用注解添加检查约束
- 图像缩放怎么没有效果啊
- 小小问题,给我带来大麻烦,给高手指点~~~
- 问一些有关短信开发的概念:什么是长号码, 短号码, 接入号?
- jfreechart 如何取消掉图标中每一项的标签,求救-----------------
- 做银行后台开发,需要对银行业务的了解程度?
- struts不同的formbean能否用一个action处理
- jdk1.5+myEclipse+jboss(自带的j2ee1.4api)写了一个简单的j2EE project,部署完成,运行时错误!
- 懂XML的朋友进来一下,帮个忙吧!
- hibernate并发的问题
- 版主回答一下,什么叫运行在同一个JVM中的应用程序
- J2EE的源代码能不能下载到的?至少我希望能有Servlet,JSP的源代码。。。
过滤器的主要功能:
(1)查询请求并执行相应的操作。
(2)阻塞请求——相应关系,使请求不再向后传递
(3)和外部资源交互
等等