当浏览器向服务器发送请求的时候,过滤器可以将请求拦截下来,完成一些特殊的功能,比如:编码设置、权限校验、日志记录等。
package com.by.servlet;
import javax.servlet.*;
import java.io.IOException;
public class FilterDemo implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
//真正执行过滤业务的方法
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");//设置请求的编码格式
response.setContentType("text/html;charset=utf-8"); //设置相应的编码格式
System.out.println("请求进来,经过过滤器...");
//一个web路径,可以配置多个过滤器,这多个过滤器就被称为过滤器链
filterChain.doFilter(request,response);
System.out.println("相应返回,经过过滤器...");
}
@Override
public void destroy() {
}
}
1.过滤器必须实现Filter接口。
2.过滤器拦截的请求执行完毕之后,必须要放行,否则我们的请求就不会被执行。
filterChain.doFilter(request,response); //过滤器放行
3.我们可以使用@WebFilter来配置过滤器要拦截的资源,当然我们也可以通过xml的方式配置过滤器。
<filter>
<filter-name>filterfilter-name>
<filter-class>com.by.servlet.FilterDemofilter-class>
filter>
<filter-mapping>
<filter-name>filterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
1.拦截具体的资源路径:/index.jsp,只有访问index.jsp的时候才会被拦截
2.目录拦截:/user/*,访问/user下的所有资源,都会被拦截
3.后缀名拦截:*.jsp 访问后缀名为jsp的资源,都会被拦截
4.拦截所有:/* 访问所有的资源,都会被拦截
1.创建servlet
package com.by.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class FilterTestServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
//设置字符编码
//request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println("username: " + username+"===password"+password);
//设置字符编码
//response.setContentType("text/html;charset=utf-8");
response.getWriter().write("username: " + username+"===password"+password);
}
}
2.配置servlet
<servlet>
<servlet-name>filterTestservlet-name>
<servlet-class>com.by.servlet.FilterTestServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>filterTestservlet-name>
<url-pattern>/filterTesturl-pattern>
servlet-mapping>
3.创建filter_test.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
监听器可以监听就是在application,session,request三个对象创建、销毁或者往其中添加修改删除属性时自动执行代码的功能组件。
package com.by.servlet;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class MyListener implements ServletContextListener {
//监听Servlet上下文对象创建的方法
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
System.out.println("============tomcat启动(create servletContext)========");
}
//监听Servlet上下文对象销毁的方法
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
System.out.println("============tomcat关闭(destroyed servletContext)==========");
}
}
<listener>
<listener-class>com.by.listen.MyListenerlistener-class>
listener>