filter以及filter的web.xml的配置
2013-03-27 20:29
281 查看
package filter;
import java.io.IOException;
import java.io.PrintWriter;
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.HttpServletRequest;
public class filter implements Filter
{
private FilterConfig config = null;
private String blackList = null;
public void init(FilterConfig config) throws ServletException
{
this.config = config;
blackList = config.getInitParameter("blacklist");
}
public void destroy()
{
config = null;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException
{
String username = ((HttpServletRequest) request)
.getParameter("username");
if (username != null)
username = new String(username.getBytes("utf-8"), "GB2312");
if (username != null && username.indexOf(blackList) != -1)
{
response.setContentType("text/html;charset=GB2312");
PrintWriter out = response.getWriter();
out.println("<html><head></head><body>");
out.println("<h1>对不起," + username + ",你没有权限留言 </h1>");
out.println("</body></html>");
out.flush();
return;
}
/**
* filter 的设置
*/
long before = System.currentTimeMillis();
config.getServletContext().log(
"NoteFilter:before call chain.doFilter()");
chain.doFilter(request, response);
config.getServletContext()
.log("NoteFilter:after call chain.doFilter()");
long after = System.currentTimeMillis();
String name = "";
if (request instanceof HttpServletRequest)
{
name = ((HttpServletRequest) request).getRequestURI();
}
config.getServletContext().log(
"NoteFilter:" + name + ": " + (after - before) + "ms");
}
}
//web.xml的配置
<filter>
<filter-name>filter</filter-name>
<filter-class>filter.filter</filter-class>
<init-param>
<param-name>blacklist</param-name>
<param-value>hello</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>filter</filter-name>
<url-pattern>/NoteServlet</url-pattern>
</filter-mapping>
相关文章推荐
- 在web.xml中配置了struts2的filter后访问jsp出现404
- web.xml中的所有配置,Listener和Filter的加载顺序
- web.xml的某些知识点以及在springmvc下的某些配置
- ApplicationContext的三种实现方式以及在web.xml配置的两种方式
- Servlet的生命周期及其常用覆盖方法的说明 以及在web.xml中配置servlet的属性说明
- web.xml 配置Filter
- web.xml配置详解之filter
- ApplicationContext的三种实现方式以及在web.xml配置的两种方式
- Servlet开发技术,创建,以及Servlet的配置,web.xml的配置
- web.xml以及结合Spring配置文件详解
- web.xml文件中引用log4J的配置以及log4J文件配置
- struts2 ,web.xml中配置为/*.action,运行报错Invalid <url-pattern> /*.action in filter mapp
- [转]Struts配置文件详解,web.xml以及struts-cofig.xml,以及struts-cofig.xml中各个标签的作用。
- Filter接口的实例调用和配置,在Web.xml配置文件中有顺序的
- web.xml 配置Filter
- web.xml配置以及servlet生命周期
- web.xml中关于Servlet、Filter、Listener的配置
- Spring整合Hibernate的XML文件配置,以及web.xml文件配置
- web.xml 配置Filter
- web工程中web.xml元素加载顺序以及配置实例