在做网站的时候为了对应用户权限显示不一样的内容,其中的远离就用到了过滤器!在Servlet中,实现过滤器只需要实现javax.servlet.Filter接口即可,该接口中有三个操作方法。
1:public void init(FilterConfig arg0) throws ServletException {
}
@Override
2: public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
}
@Override
3: public void destroy() {
}
第一个方法是在启动服务器的时候过滤器进行初始化第二个方法有三个参数,前两个参数都是ServletRequest类型,说明它可以处理任何协议类型的过滤请求,第三个参数是将用户的请求传递给其他的用户或则servlet,第三个方法是进行过滤器的销毁操作。
现在编写代码测试:
package com.practice;
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;
public class SimpleFilter implements Filter{
@Override
public void init(FilterConfig arg0) throws ServletException {
String initParm = arg0.getInitParameter("robin");
System.out.println("过滤器初始化.....初始化参数为:"+initParm);
}
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
arg0.setCharacterEncoding("utf-8");//设置统一编码
System.out.println("执行doFilter之前....");
arg2.doFilter(arg0, arg1);//继续向下传递
System.out.println("执行doFilter之后.....");
}
@Override
public void destroy() {
System.out.println("销毁过滤器.....");
}
}
现在配置web.xml文件。在xml文件中配置如下:
<filter>
<filter-name>RoBin</filter-name>
<filter-class>com.practice.SimpleFilter</filter-class>//表示需要进行过滤的类
<init-param>
<param-name>robin</param-name>
<param-value>RoBin Filter</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>RoBin</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
过滤器配置和servlet配置基本差不多,就不多说。
现在做一个简单的登陆过滤器,相信大家在做网站开发的时候不希望某一个用户还没有进行登陆就访问到某些不应该看见的资源,代码如下:
首先编写一个过滤器类让他实现Filter接口,如下:
1:Login
package com.practice;
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.HttpServletRequest;
import javax.servlet.http.HttpSession;
public class Login implements Filter{
@Override
public void init(FilterConfig arg0) throws ServletException {
System.out.println("过滤器初始化.....初始化信息为:"+arg0.getInitParameter("robin"));
}
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest)arg0;
HttpSession session = request.getSession();
if(session.getAttribute("userid")!=null)
arg2.doFilter(arg0, arg1);
else{
request.getRequestDispatcher("login.jsp").forward(arg0, arg1);
}
}
@Override
public void destroy() {
System.out.println("过滤器销毁.....");
}
}
2:jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<base href="<%=basePath%>">
<title>登录页面</title>
</head>
<body>
<form action="handle.jsp" method="post">
用户ID:<input type="text" name="userid"/><br/>
<input type="submit" value="登录"/>
</form>
</body>
</html>
3:handle.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<head>
<base href="<%=basePath%>">
<title>handle</title>
</head>
<body>
<h2>处理页面</h2>
<%
String userid = request.getParameter("userid");
session.setAttribute("userid",userid);
%>
</body>
</html>
3:配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>login</filter-name>
<filter-class>com.practice.Login</filter-class>
<init-param>
<param-name>robin</param-name>
<param-value>RoBin Filter</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>login</filter-name>
<url-pattern>/login.jsp</url-pattern>
</filter-mapping>
</web-app>