Filter过滤器
Fileter可以实现:
1)客户端的请求访问servlet之前拦截这些请求,对用户请求进行预处理
2)对HttpServletResponse进行后处理;
注意
多个Filter的执行顺序在web.xml配置文件中的配置顺序一致,一般把feilter配置在servlet之前
代码示例
public class MyFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
System.out.println("过滤器拦截成功");
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("GBK");
//放行请求
chain.doFilter(request, response);
}
}
web.xml配置Filter
<!-- 配置filter过滤器 -->
<filter>
<!-- 别名 -->
<filter-name>my</filter-name>
<!-- 地址 -->
<filter-class>code.filter.MyFilter</filter-class>
</filter>
<!-- 配置filter映射关系 -->
<filter-mapping>
<filter-name>my</filter-name>
<!-- 设置拦截路径 ,路径为/* 为拦截所有路径-->
<url-pattern>/m</url-pattern>
</filter-mapping>
执行顺序
在web.xml设置初始化参数
getInitParameter()方法可以获取在web.xml配置文件中配置的init-param参数
//在MyFilter内的init方法验证
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("filter-init");
String site = filterConfig.getInitParameter("Site");
System.out.println(site);
}
listener监听器
定义
用于监听web项目的常见对象
HttpServletRequest/HttpSession/ServltContext/HttpServletResponse
作用
1)监听web对象的创建与销毁
2)监听web对象属性的变化
3)监听session绑定javabean的操作(Java类)
代码
package code.listener;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
public class ReqListener implements ServletRequestListener{
@Override
public void requestDestroyed(ServletRequestEvent sre) {
System.out.println("listener-req-destroy");
}
@Override
public void requestInitialized(ServletRequestEvent sre) {
System.out.println("listener-req-init");
}
}
web.xml配置Listener
运行顺序
filter过滤器和listener监听器的区别
![image]
(/i/l/?n=22&i=blog/2636359/202211/2636359-20221116205054468-2011387421.png)
WEB-INF目录
利用反射创建BaseServlet实现调用自定义业务方法
BaseServlet不需要再配置web.xml,因为只需要其提供doGet()doPost()方法
public class BaseServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
//获取请求中的method参数,该参数的值,表示业务方法的名字
String mName = req.getParameter("method");//在路径后加?method=xxx
//this当前类对象,创建反射的Class类对象
Class c = this.getClass();
try {
//创建反射method对象
Method method = c.getDeclaredMethod(mName, HttpServletRequest.class,HttpServletResponse.class);
//执行方法
method.invoke(this, req, res);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
doGet(req, res);
}
public void test(HttpServletRequest req, HttpServletResponse res) throws IOException{
PrintWriter out = res.getWriter();
out.write("baseservlet-function-test()");
out.flush();
if(out!=null) out.close();
}
public void test2(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
req.getRequestDispatcher("page/demo.jsp").forward(req, res);
}
}
可以将BaseServlet设置doGet()方法,然后其他的servlet类都可以继承这个类实现调用自定义业务方法
public class BaseServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
//获取请求中的method参数,该参数的值,表示业务方法的名字
String mName = req.getParameter("method");
//this当前类对象,创建反射的Class类对象
Class c = this.getClass();
//创建反射method对象
try {
Method method = c.getDeclaredMethod(mName, HttpServletRequest.class,HttpServletResponse.class);
//执行方法
method.invoke(this, req, res);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
doGet(req, res);//如果post方法接收的话,也跳转到doGet方法
}
}
//
//因为 MyServlet 继承 BaseServlet,所以继承了baseservlet的doget方法,所以在运行时BaseServlet内的this关键字相当于指代MyServlet类对象
public class MyServlet extends BaseServlet{
public void run(HttpServletRequest req, HttpServletResponse res){
PrintWriter out = res.getWriter();
out.write("MyServlet-function test()");
out.flush();
if(out != null) out.close();
}
}
标签:自定义,res,void,req,method,listener,监听器,public,out
From: https://www.cnblogs.com/xiaoto9426/p/16897568.html