為什么我的過濾器案例得不到老師講的“死循環(huán)”結(jié)果?
FirstFilter.java代碼:
package com.imooc.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;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class FirstFilter implements Filter {
public void destroy() {
System.out.println("destroy---FirstFilter");
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
System.out.println("start.......doFilter()---FirstFilter");
//chain.doFilter(request,response);
HttpServletRequest req=(HttpServletRequest) request;
HttpServletResponse response2=(HttpServletResponse) response;
//重定向
response2.sendRedirect(req.getContextPath()+"/main.jsp");
System.out.println("end.......doFilter()---FirstFilter");
}
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("init---FirstFilter");
}
}
web.xml配置代碼:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
? <display-name>ImoocFilte</display-name>
? <welcome-file-list>
? ? <welcome-file>index.html</welcome-file>
? ? <welcome-file>index.htm</welcome-file>
? ? <welcome-file>index.jsp</welcome-file>
? ? <welcome-file>default.html</welcome-file>
? ? <welcome-file>default.htm</welcome-file>
? ? <welcome-file>default.jsp</welcome-file>
? </welcome-file-list>
? ? <filter>
? ? ? ? <filter-name>FirstFilter</filter-name>
? ? ? ? <filter-class>com.imooc.filter.FirstFilter</filter-class>
? ? ? ? <init-param>
<param-name>name</param-name>
<param-value>zhangsan</param-value>
</init-param>
? ? </filter>
? ? <filter-mapping>
? ? ? ? <filter-name>FirstFilter</filter-name>
? ? ? ? <url-pattern>/index.jsp</url-pattern>
? ? ? ? <dispatcher>REQUEST</dispatcher>
? ? </filter-mapping>
? ? ?<filter-mapping>
? ? ? ? <filter-name>FirstFilter</filter-name>
? ? ? ? <url-pattern>/main.jsp</url-pattern>
? ? </filter-mapping>
? ?<!--?
? ?<filter>
? ? ? ? <filter-name>SecondFilter</filter-name>
? ? ? ? <filter-class>com.imooc.filter.SecondFilter</filter-class>
? ? </filter>
? ? <filter-mapping>
? ? ? ? <filter-name>SecondFilter</filter-name>
? ? ? ? <url-pattern>/index.jsp</url-pattern>
? ? </filter-mapping>
? ?-->?
</web-app>
index.jsp代碼:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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>
? ? This is my JSP page. <br>
? ? <%
? ? System.out.println("處理過程完成!");
? ? ?%>
? </body>
</html>
main.jsp代碼:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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 'main.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>
? ? This is my Main page. <br>
? </body>
</html>
2017-10-16
你服務(wù)器啟動后是直接訪問main.jsp頁面嗎?