package com.baizhi.filter;
import java.io.IOException;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
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.HttpServletResponse;
public class MyFilter1 implements Filter{
@Override
public void destroy() {
// TODO Auto-generated method stub
System.out.println("=====invoke destory=====");
}
@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
throws IOException, ServletException {
// TODO Auto-generated method stub
System.out.println("this is myfilter doFilter");
String name=req.getParameter("name");
HttpServletRequest request=(HttpServletRequest)req;
if(name==null) {
//重定向到一个错误页面
HttpServletResponse response=(HttpServletResponse)resp;
response.sendRedirect(request.getContextPath()+"/ERROR.html");
}
else {
//让请求继续向下执行
chain.doFilter(req, resp);
}
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub
System.out.println("===invoke init====");
}
}
package com.baizhi.servlet;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;
public class ContextA extends HttpServlet{
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取ServletContext
HttpSession session=request.getSession();
ServletContext application=session.getServletContext();
//存数据
application.setAttribute("name", "avalue");
//跳转
response.sendRedirect(request.getContextPath()+"/ContextB");
}
}
package com.baizhi.servlet;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;
public class ContextB extends HttpServlet{
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取ServletContext
HttpSession session=request.getSession();
ServletContext application=session.getServletContext();
//取数据
String aname=(String)application.getAttribute("name");
System.out.println("aname:"+aname);
}
}
package com.baizhi.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 Target1 extends HttpServlet{
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("this is target1");
}
}
package com.baizhi.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 Target2 extends HttpServlet{
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("this is target2");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>Servlet4</display-name>
<servlet>
<servlet-name>ContextA</servlet-name>
<servlet-class>com.baizhi.servlet.ContextA</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ContextA</servlet-name>
<url-pattern>/ContextA</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>ContextB</servlet-name>
<servlet-class>com.baizhi.servlet.ContextB</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ContextB</servlet-name>
<url-pattern>/ContextB</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Target1</servlet-name>
<servlet-class>com.baizhi.servlet.Target1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Target1</servlet-name>
<url-pattern>/Target1</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Target2</servlet-name>
<servlet-class>com.baizhi.servlet.Target2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Target2</servlet-name>
<url-pattern>/Target2</url-pattern>
</servlet-mapping>
<!-- 配置过滤器 -->
<filter>
<filter-name>MyFilter1</filter-name>
<filter-class>com.baizhi.filter.MyFilter1</filter-class>
</filter>
<filter-mapping>
<filter-name>MyFilter1</filter-name>
<!-- 指定过滤那些请求 -->
<url-pattern>/Target1</url-pattern>
<url-pattern>/Target2</url-pattern>
</filter-mapping>
</web-app>
<!DOCTYPE HTML>
<html>
<head>
<title>a.html</title>
<meta charset="utf-8">
</head>
<body>
this is my page<br>
</body>
</html>