web.xml
<filter>
<filter-name>filterDemo05</filter-name>
<filter-class>com.itheima.filter.FilterDemo05</filter-class>
<!--配置开启异步支持,当dipatcher配置ASYNC时,需要配置此行-->
<async-supported>true</async-supported>
</filter>
<filter-mapping>
<filter-name>filterDemo05</filter-name>
<!-- <url-pattern>/error.jsp</url-pattern>-->
<url-pattern>/index.jsp</url-pattern>
<!--过滤请求:默认值-->
<dispatcher>REQUEST</dispatcher>
<!--过滤全局错误页面,当由服务器调用全局页面时,过滤器工作-->
<dispatcher>ERROR</dispatcher>
<!--过滤请求转发:当请求转发时,过滤器工作-->
<dispatcher>FORWARD</dispatcher>
<!--过滤请求包含:当请求包含时,过滤器工作,它只能过滤动态包含,jsp的include指令是静态包含,过滤器不会-->
<dispatcher>INCLUDE</dispatcher>
<!--过滤异步类型:它要求我们在filter标签中配置开启异步支持-->
<dispatcher>ASYNC</dispatcher>
</filter-mapping>
<!--配置全局错误页面-->
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/error.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/error.jsp</location>
</error-page>
<%--
Created by IntelliJ IDEA.
User: hqg
Date: 2023/3/30
Time: 6:34
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>自定义错误页面</title>
</head>
<body>
不好意思出错了。。。。
</body>
</html>
<%--
Created by IntelliJ IDEA.
User: hqg
Date: 2023/3/30
Time: 4:28
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
$END$
</body>
</html>
package com.itheima.filter;
import javax.servlet.*;
import java.io.IOException;
/*过滤器拦截行为*/
//@WebFilter("/*")
public class FilterDemo05 implements Filter {
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("filterDemo05执行了。。。。。");
servletResponse.setContentType("text/html;charset=UTF-8");
//放行
filterChain.doFilter(servletRequest,servletResponse);
}
}
package com.itheima.serlvet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/servletDemo03")
public class servletDemo03 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("servletDemo03 执行了");
/*int i=1/0;*/
//请求转发
/* req.getRequestDispatcher("index.jsp").forward(req,resp);*/
//请求包含
req.getRequestDispatcher("index.jsp").include(req,resp);
//resp.setContentType("text/html;charset=UTF-8");
resp.getWriter().write("servletDemo03 执行了");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}