过滤器
过滤器: 定义在服务器端的一段程序, 可以截获客户端发来的请求, 并根据一定规则进行过滤和拦截
过滤器的生命周期:
服务器启动
先通过web.xml加载过滤器进行实例化(这个过程是调用过滤器中init()方法, 同样也是只执行一次)
客户端发送请求:
执行过滤器中的doFilter()方法, 这个方法会执行N次, 每次有请求发送过来, 都会执行这个方法进行过滤
销毁过程
同servlet, 关闭或者停止服务器的时候会执行destory()方法
过滤器链:
不同的请求互不影响
过滤器链主要是针对多个过滤器过滤同一个url请求
过滤器的先后顺序是按照在web.xml的配置顺序来的
运行顺序: 请求--->过滤器1过滤--->过滤器2过滤--->后台处理层--->过滤器2过滤--->过滤器1过滤--->客户端接收
3.0新特性的过滤器:
使用注解的方式:
@WebFilter(filterName="", value={"",""}...等其他配置)
login.jsp
<from type="LoginServler" method="post">
用户名:<input type="text" name="username" /><br>
密码: <input type="text" name="password" /><br>
<input type="submit" value="登录" />
</from>
register.jsp
<from type="RegisterServler" method="post">
用户名: <input type="text" name="username" /><br>
密码: <input type="text" name="password" /><br>
姓名: <input type="text" name="realname" /><br>
<input type="submit" value="提交" />
</from>
LoginServler.java
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println(username);
System.out.println(password);
if("admin".equals(username) && "123456".equals(password)){
request.getSession().setAttribute("currentUser","admin");
response.sendRedirect("index.jsp");
}else{
response.sendRedirect("fail.jsp");
}
}
RegisterServler.java
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println(username);
System.out.println(password);
if("admin".equals(username) && "123456".equals(password)){
request.getSession().setAttribute("currentUser","admin");
response.sendRedirect("index.jsp");
}else{
response.sendRedirect("fail.jsp");
}
}
SessionFilterTest.java
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.HttpServletResponse;
public class SessionFilterTest implements Filter {
private String pages;
private HttpServletRequest request;
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest)req;
HttpServletResponse response = (HttpServletResponse)resp;
//获取当前的请求
String uri = request.getRequestURI();
System.out.println("当前发送的请求"+uri);
//获取不需要判断session的请求
String[] _pages = pages.split(";");
//判断当前请求是否在不需要判断的请求里
if(checkString(_pages,uri)){
//放行
chain.doFilter(req,resp);
}else{
//先判断session在放行
Object obj = request.getSession().getAttribute("currentUser");
if(obj==null){
response.sendRedirect("login.jsp");
}else{
chain.doFilter(req,resp);
}
}
}
public static boolean checkString(String[] pages,String uri){
for(String s:pages){
if(uri.contains(s)){
return true;
}
}
return false;
}
@Override
public void init(FilterConfig config) throws ServletException {
String str = config.getInitParameter("page");
this.setPages(str);
System.out.println(str);
}
public String getPages(){
return pages;
}
public void setPages(String pages){
this.pages = pages;
}
}
TestFilter.java
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 TestFilter implements Filter {
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain arg2)
throws IOException, ServletException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=utf-8");
arg2.doFilter(request, response);
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}