什么是Filter过滤器
1.Filter是JavaEE的规范 也就是接口
作用是 拦截请求 、过滤响应
拦截请求常见的应用场景
1.权限检查
2.日记操作
3.事务管理
…
Filter过滤器的初体验
要求在web工程下有一个admin目录(html、jpg、jsp…)。这个admin目录下的所有资源必须是用户登录后才允许访问
//一般的jsp实现 有局限性
查看session中是否包含用户信息即可判断用户是否已经登陆
在请求到目标资源以前 先经过Fliter过滤器的过滤
package com.wangqiang.filter;
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 AdminFilter implements Filter{
@Override
public void destroy() {
// TODO Auto-generated method stub
}
/**
* doFilter专门用于拦截请求 过滤
*/
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
throws IOException, ServletException {
HttpServletRequest httpServletRequest=(HttpServletRequest)servletRequest;
HttpSession session=httpServletRequest.getSession();
Object user=session.getAttribute("user");
if(user==null){
servletRequest.getRequestDispatcher("/login.jsp").forward(servletRequest, servletResponse);
return ;
}else{
//放行 让用户继续向下访问用户的目标资源
filterChain.doFilter(servletRequest, servletResponse);
}
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
package com.wangqiang.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
String username=req.getParameter("username");
String password=req.getParameter("password");
if("123".equals(username)&&"456".equals(password)) {
req.getSession().setAttribute("user", username);
resp.getWriter().write("登陆成功");
}else {
req.getRequestDispatcher("/login.jsp").forward(req, resp);
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<!-- Filter标签用于配置一个Filter过滤器 -->
<filter>
<filter-name>AdminFilter</filter-name>
<filter-class>com.wangqiang.filter.AdminFilter</filter-class>
</filter>
<!-- 配置拦截路径 具体内容与Servlet类似-->
<filter-mapping>
<filter-name>AdminFilter</filter-name>
<!-- /表示请求地址到工程路径 映射到WebContent目录 -->
<url-pattern>/admin/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.wangqiang.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/loginServlet</url-pattern>
</servlet-mapping>
</web-app>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
这是登录页面login.jsp
<form action="http://localhost:8080/Filter_my/loginServlet" method="get">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password">
<input type="submit" value="登陆">
</form>
</body>
</html>