关于拦截:
- 拦截路径配置:
1. 具体资源路径: /index.jsp
只有访问index.jsp资源时,过滤器才会被执行
2. 拦截目录: /user/*
访问/user下的所有资源时,过滤器都会被执行
3. 后缀名拦截: *.jsp
访问所有后缀名为jsp资源时,过滤器都会被执行
4. 拦截所有资源:/*
访问所有资源时,过滤器都会被执行
- 过滤器的四种拦截方式:REQUEST、FORWARD、INCLUDE、ERROR
| 拦截方式 | 功能描述 |
| — | — |
| REQUEST | 直接访问目标资源时执行过滤器。包括:在地址栏中直接访问、表单提交、超链接、重定向,只要在地址栏中可以看到目标资源的路径,就是REQUEST |
| FORWARD | 转发访问执行过滤器。包括RequestDispatcher#forward()方法、<jsp:forward>
标签都是转发访问 |
| INCLUDE | 包含访问执行过滤器。包括RequestDispatcher#include()方法、<jsp:include>
标签都是包含访问 |
| ERROR | 当目标资源在web.xml中配置为中时,并且真的出现了异常,转发到目标资源时,会执行过滤器 |
注解配置:
-
@WebFilter(value="/index.jsp",dispatcherTypes = DispatcherType.REQUEST)
-
@WebFilter(value="/*",dispatcherTypes ={ DispatcherType.FORWARD,DispatcherType.REQUEST})
web.xml配置:<dispatcher>REQUEST</dispatcher>
案例:字符编码转换
@WebFilter("/*")
public class CharchaterFilter implements Filter {
protected String encoding;
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
//将父接口转换为子接口
HttpServletRequest request=(HttpServletRequest)req;
HttpServletResponse response=(HttpServletResponse)res;
//获取请求方法
String method=request.getMethod();
//解决post请求中文数据乱码问题
if(method.equalsIgnoreCase(“post”)) {
request.setCharacterEncoding(“utf-8”);
}
//处理响应乱码
response.setContentType(“text/html;charset=utf-8”);
chain.doFilter(request,response);
}
public void init(FilterConfig fConfig) throws ServletException {
// TODO Auto-generated method stub
}
}
案例:敏感词过滤
/**
- 敏感词汇过滤器
*/
@WebFilter("/*")
public class SensitiveWordsFilter implements Filter {
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
//1.创建代理对象,增强getParameter方法
ServletRequest proxy_req = (ServletRequest) Proxy.newProxyInstance(req.getClass().getClassLoader(), req.getClass().getInterfaces(), new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//增强getParameter方法
//判断是否是getParameter方法
if(method.getName().equals(“getParameter”)){
//增强返回值
//获取返回值
String value = (String) method.invoke(req,args);
if(value != null){
for (String str : list) {
if(value.contains(str)){
value = value.replaceAll(str,"***");
}
}
}
return value;
}
//判断方法是否是: getParameterMap
//判断方法是否是: getParameterValue
return method.invoke(req,args);
}
});
//2.放行
chain.doFilter(proxy_req, resp);
}
private List list = new ArrayList();//敏感词汇集合
public void init(FilterConfig config) throws ServletException {
try{
//1.获取文件真实路径
ServletContext servletContext = config.getServ
【一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义】
浏览器打开:qq.cn.hn/FTf 免费领取
letContext();
String realPath = servletContext.getRealPath("/WEB-INF/classes/敏感词汇.txt");
//2.读取文件
BufferedReader br = new BufferedReader(new FileReader(realPath));
//3.将文件的每一行
String line = null;
while((line = br.readLine())!=null){
list.add(line);
}
br.close();
System.out.println(list);
}catch (Exception e){
e.printStackTrace();
}
}
public void destroy() {
}
}
/**
- 验证方法
*/
@WebServlet("/testServlet")
public class TestServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter(“name”);
String msg = request.getParameter(“msg”);
System.out.println(name+":"+msg);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
过滤器JavaWeb三大组件之一,当我们请求服务器的资源时,过滤器会在这组资源之前执行,它可以将我们的请求拦截下来,判断是否让我们访问这个资源,并完成一些特殊的功能。过滤器一般用于完成通用的操作。如:登录验证、统一编码处理、敏感字符过滤…
监听器JavaWeb的三大组件之一,在我们执行请求之前执行,主要用于监听Web服务器中的某一个执行动作,并根据其要求做出响应的响应。
目前Servlet中包含8个Listener接口,可以将其归纳为3类:
-
ServletContextListener:监听ServletContext对象的创建和销毁
-
方法:
-
void contextDestroyed(ServletContextEvent sce) :ServletContext对象被销毁之前会调用该方法
-
void contextInitialized(ServletContextEvent sce) :ServletContext对象创建后会调用该方法
-
步骤:
-
定义一个类,实现ServletContextListener接口
-
复写方法
-
配置
-
web.xml
cn.itcast.web.listener.ContextLoaderListener
- 指定初始化参数
- 注解:
-
@WebListener
-
代码实现
@WebListener
public class ContextLoaderListener implements ServletContextListener {
/**
-
监听ServletContext对象创建的。ServletContext对象服务器启动后自动创建
-
在服务器启动后自动调用
-
@param servletContextEvent
*/
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
//加载资源文件
//1.获取ServletContext对象
ServletContext servletContext = servletContextEvent.getServletContext();
//2.加载资源文件
String contextConfigLocation = servletContext.getInitParameter(“contextConfigLocation”);
//3.获取真实路径
String realPath = servletContext.getRealPath(contextConfigLocation);
//4.加载进内存