两个servlet
package web;
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 AddCostServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
System.out.println("增加资费......");
}
}
package web;
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 FindCostServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
System.out.println("查询资费......");
req.setAttribute("costs", "hello");
}
}
是两个过滤器:
package web;
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;
public class LogFilter implements Filter {
//关闭服务器时被调用一次
public void destroy() {
System.out.println("销毁LogFilter");
}
/**
* 该方法是处理公共业务的方法
* Filter类似于Servlet的管家
* tomcat在调用Servlet之前会将请求提交给Filter
* Filter有权利让请求继续也有权让请求终止
* tomcat就是调用doFilter方法让Filter统一处理请求
*/
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
throws IOException, ServletException {
System.out.println("在前面记录日志");
chain.doFilter(req, res);//请求继续,向后执行
System.out.println("在后面记录日志");
}
/**
* 启动服务器时会自动实例Filter
* 然后调用其init()方法来初始化Filter(一次)
* 然后调用此方法时传入FilterConfig,该对象和Filter是一对一的关系
* 可以给Filter预置参数(web.xml)
* 该对象和ServletConfig用法完全一样
*/
public void init(FilterConfig arg0) throws ServletException {
System.out.println("初始化LogFilter");
}
}
package web;
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;
public class GuolvFilter implements Filter {
//声明成员变量接收配置文件里面的参数
private String param;
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
throws IOException, ServletException {
System.out.println("web.xml文件中的数据为:"+param);
System.out.println("在前面过滤敏感词");
chain.doFilter(req, res);
System.out.println("在后面过滤敏感词");
}
//一般在init()中接收参数,在diFilter()中使用该方法 声明成员变量来保存该数据
public void init(FilterConfig cfg) throws ServletException {
param = cfg.getInitParameter("city");
System.out.println(param);
}
}
配置文件:
<?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" version="2.5">
<display-name>jsp4</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>
<servlet>
<servlet-name>findCost</servlet-name>
<servlet-class>web.FindCostServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>findCost</servlet-name>
<url-pattern>/findCost</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>addCost</servlet-name>
<servlet-class>web.AddCostServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>addCost</servlet-name>
<url-pattern>/addCost</url-pattern>
</servlet-mapping>
<!-- 配置过滤器 -->
<filter>
<filter-name>log</filter-name>
<filter-class>web.LogFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>log</filter-name>
<!-- 此处声明Filter可以过滤哪些请求(对这些请求有效) -->
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>guolv</filter-name>
<filter-class>web.GuolvFilter</filter-class>
<init-param>
<param-name>city</param-name>
<param-value>北京</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>guolv</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
启动服务器时输出:
初始化LogFilter
北京
请求路径:http://localhost:8080/jsp4/findCost
输出:在前面记录日志
web.xml文件中的数据为:北京
在前面过滤敏感词
查询资费......
在后面过滤敏感词
在后面记录日志
请求路径:http://localhost:8080/jsp4/addCost
输出:在前面记录日志
web.xml文件中的数据为:北京
在前面过滤敏感词
增加资费......
在后面过滤敏感词
在后面记录日志
退出服务器时候输出:销毁LogFilter