过滤器
javaweb 三大组件
servlet 和filter一样是都是单例的
listener
filter
都需要在web.xml中进行配置
它会在一组资源(jsp,servlet,css,html,Servlet等等)的前面执行,它可以让请求得到目标资源,也可以不让请求达到!!
过滤器有拦截请求的能力!!
是否允许它访问AServlet,BServlet,CServlet,就是在访问的前面加上一层保安,只有符合要求了才允许登录,如果不符合要求就不能登录,过滤器的好处是可以处理很多的请求不是只处理一个。
过滤器如何编写
第一步先创建一个servlet
<servlet>
<servlet-name>AServlet</servlet-name>
<servlet-class>cn.itcast.web.servlet.AServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AServlet</servlet-name>
<url-pattern>/AServlet</url-pattern>
</servlet-mapping>
说明访问成功了,这个时候准备加一个过滤器
Filter 接口存在tomcat 服务器的jar 包中,所以要把tomcat 加进来才能不报错
Filter 是单例的
web.xml 配置
servlet 全部改为
<filter>
<filter-name>AFilter</filter-name>
<filter-class>cn.itcast.web.filter.AFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>AFilter</filter-name>
<url-pattern>/AServlet</url-pattern> 表示的是AFilter 的过滤范围,只能是过滤AServlet
</filter-mapping>
<url-pattern>/AServlet</url-pattern> 表示的是只拦截AServlet,如果写成了<url-pattern>/web/*</url-pattern>表示对web 路径的都拦截,但是一般情况下,<url-pattern>/*</url-pattern>
只要是启动服务器,过滤器就出生了
七月 16, 2018 8:44:44 下午 org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 3499 ms
七月 16, 2018 8:44:44 下午 org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
七月 16, 2018 8:44:44 下午 org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.82
A过滤器出生了
七月 16, 2018 8:44:45 下午 org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
七月 16, 2018 8:44:45 下午 org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
七月 16, 2018 8:44:45 下午 org.apache.catalina.startup.Catalina start
INFO: Server startup in 1631 ms
/**
* 每次过滤时都会执行
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
System.out.println("AFilter#start");
chain.doFilter(request, response);//放行!! filterChain 对象的dofilter 方法
System.out.println("AFilter#end");
}
这个步骤虽然是访问的是AServlet,
和serlvetConfig
2 拦截index.jsp
<filter>
<filter-name>AFilter</filter-name>
<filter-class>cn.itcast.web.filter.AFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>AFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
这样写的话,就是过滤器是servlet 容器创建的,所以只要是请求,都会拦截
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">
<display-name></display-name>
<servlet>
<servlet-name>AServlet</servlet-name>
<servlet-class>cn.itcast.web.servlet.AServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AServlet</servlet-name>
<url-pattern>/AServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>BServlet</servlet-name>
<servlet-class>cn.itcast.web.servlet.BServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>BServlet</servlet-name>
<url-pattern>/BServlet</url-pattern>
</servlet-mapping>
<filter>
<filter-name>AFilter</filter-name>
<filter-class>cn.itcast.web.filter.AFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>AFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>BFilter</filter-name>
<filter-class>cn.itcast.web.filter.BFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>BFilter</filter-name>
<url-pattern>/*</url-pattern>
<!-- <dispatcher>FORWARD</dispatcher> -->
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
服务器启动
七月 16, 2018 10:08:53 下午 org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 586 ms
七月 16, 2018 10:08:53 下午 org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
七月 16, 2018 10:08:53 下午 org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.82
A过滤器出生了
B过滤器出生了
七月 16, 2018 10:08:53 下午 org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
七月 16, 2018 10:08:53 下午 org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
七月 16, 2018 10:08:53 下午 org.apache.catalina.startup.Catalina start
INFO: Server startup in 547 ms
web.xml 是servlet 容器中的内容,所以更改了web.xml 中的内容,所以要重新启动服务器
FilterChain#doFilter()方法:
执行目标资源,或是执行下一个过滤器!如果没有下一个过滤器那么执行的是目标资源,如果有,那么就执行下一个过滤器!
过滤器的四种拦截方式
1 拦截请求,只是拦截请求,如果是从AServlet 转发到BServlet 中是不拦截的
<dispatcher>REQUEST</dispatcher> 什么都不写默认拦截请求
如果是这种就没有默认了,只有转发了。
2 拦截转发
<dispatcher>FORWARD</dispatcher>
3 拦截包含
<dispatcher>INCLUDE</dispatcher>
4 拦截错误
<error-page>
<error-code>500</error-code>
<location>/500.jsp</location>
</error-page>
<dispatcher>ERROR</dispatcher>
在<filter-mapping>中进行配置!
控制多个过滤器的执行顺序
package cn.itcast.web.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;
/**
* Servlet Filter implementation class CFilter
*/
public class CFilter implements Filter {
/**
* Default constructor.
*/
public CFilter() {
// TODO Auto-generated constructor stub
}
/**
* @see Filter#destroy()
*/
public void destroy() {
// TODO Auto-generated method stub
}
/**
* @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
// place your code here
// pass the request along the filter chain
chain.doFilter(request, response);
}
/**
* @see Filter#init(FilterConfig)
*/
public void init(FilterConfig fConfig) throws ServletException {
// TODO Auto-generated method stub
}
}
我们在实现接口定义的方法、Eclipse往往会自动加上一句:TODO Auto-generated method stub
每次手动删除很麻烦,我们可以设置一下,让强大的Eclipse在完成自动代码时,不生成这句代码。
在 菜单栏中 Window --> Preferences -->Java -->Code Style -->Code Templates--> Code--> Method Body, 双击这一项edit 去掉TODO语句的话就行了
多个filter 的执行顺序
过滤器的应用场景:
<%@ taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core” %>报错,少两个包
加上就好了
代码javaweb day21_3
<filter>
<display-name>AFilter</display-name>
<filter-name>AFilter</filter-name>
<filter-class>cn.itcast.web.filter.AFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>AFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>cn.itcast.web.listener.AListener</listener-class>
</listener>
package cn.itcast.web.filter;
import java.io.IOException;
import java.util.Map;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
/**
* 从application中获取Map
* 从request中得到当前客户端的IP
* 进行统计工作,结果保存到Map中
* @author cxf
*
*/
public class AFilter implements Filter {
//为了过去servletContext 还有另外一种方法reuqest.getSession()
private FilterConfig config;
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
/*
* 1. 得到application中的map
* 2. 从request中获取当前客户端的ip地址
* 3. 查看map中是否存在这个ip对应访问次数,如果存在,把次数+1再保存回去
* 4. 如果不存在这个ip,那么说明是第一次访问本站,设置访问次数为1
*/
/*
* 1. 得到appliction
*/
ServletContext app = request.getServletContext();
// ServletContext app = config.getServletContext();
Map<String,Integer> map = (Map<String, Integer>) app.getAttribute("map");
/*
* 2. 获取客户端的ip地址
*/
String ip = request.getRemoteAddr();
/*
* 3. 进行判断
*/
if(map.containsKey(ip)) {//这个ip在map中存在,说明不是第一次访问
int cnt = map.get(ip);
map.put(ip, cnt+1);
} else {//这个ip在map中不存在,说明是第一次访问
map.put(ip, 1);
}
System.out.println(map);
app.setAttribute("map", map);//把map再放回到app中
chain.doFilter(request, response);//肯定放行
}
/**
* 在服务器启动时就会执行本方法,而且本方法只执行一次!
*/
public void init(FilterConfig fConfig) throws ServletException {
this.config = fConfig;
}
}
package cn.itcast.web.listener;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class AListener implements ServletContextListener {
/**
* 在服务器启动时创建Map,保存到ServletContext
*/
public void contextInitialized(ServletContextEvent sce) {
// 创建Map
Map<String,Integer> map = new LinkedHashMap<String,Integer>();
// 得到ServletContext
ServletContext application = sce.getServletContext();
// 把map保存到application中
application.setAttribute("map", map);
}
public void contextDestroyed(ServletContextEvent sce) {
}
}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'show.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h1 align="center">显示结果</h1>
<table align="center" width="60%" border="1">
<tr>
<th>IP</th>
<th>次数</th>
</tr>
<c:forEach items="${applicationScope.map }" var="entry">
<tr>
<td>${entry.key }</td>
<td>${entry.value }</td>
</tr>
</c:forEach>
</table>
</body>
</html>
RBAC --- 基于角色的权限控制
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<!-- index.jsp 游客可以访问 -->
<h1>你就是个游客而已</h1>
<a href="<c:url value='/index.jsp'/>">游客入口</a><br/>
<a href="<c:url value='/users/u.jsp'/>">会员入口</a><br/>
<a href="<c:url value='/admin/a.jsp'/>">管理员入口</a><br/>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'login.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h1>登录</h1>
${msg }
<form action="<c:url value='/LoginServlet'/>" method="post">
用户名:<input type="text" name="username"/>
<input type="submit" value="登录"/>
</form>
</body>
</html>
代码day21_4
servlet:
POST:request.setCharacterEncoding(“utf-8”);
GET:
String username = request.getParameter(“username”); (默认使用的是ISO-8859-1)
username = new String(username.getBytes(“ISO-8859-1”), “utf-8”);//解决方案就是解了重编