欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

servlet-filter-listener

程序员文章站 2022-06-03 09:46:33
...

javaWeb的三大组件

servlet

1、web.xml

<servlet>
    <servlet-name>HelloWorld</servlet-name>
    <servlet-class>com.htp.servlet.ServletDemo</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>HelloWorld</servlet-name>
    <url-pattern>/HelloWorld</url-pattern>
  </servlet-mapping>

2、实现类 此处继承httpServlet接口,实现doGet()

package com.htp.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ServletDemo
 */
@WebServlet("/ServletDemo")
public class ServletDemo extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServletDemo() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
         // 设置响应内容类型
          response.setContentType("text/html");

          // 实际的逻辑是在这里
          PrintWriter out = response.getWriter();
          out.println("<h1>" + "HELLO WORLD" + "</h1>");

          System.out.println("servletDemo end...");
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

}

filter

1、web.xml

<filter>
    <filter-name>FilterDemo</filter-name>
    <filter-class>com.htp.filter.FilterDemo</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>FilterDemo</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

2、实现类 (实现filter接口)

package com.htp.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.annotation.WebFilter;

/**
 * Servlet Filter implementation class FliterDemo
 */
@WebFilter(description = "测试过滤器", urlPatterns = { "/FilterDemo" })
public class FilterDemo implements Filter {

    /**
     * Default constructor.
     */
    public FilterDemo() {
        // TODO Auto-generated constructor stub
    }

    /**
     * @see Filter#destroy()
     */
    public void destroy() {
        // TODO Auto-generated method stub
        System.out.println("test fliter end....");
    }

    /**
     * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
     */
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        // TODO Auto-generated method stub
        // place your code here
        // 对request和response进行一些预处理
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        System.out.println("before FilterDemo!!!");

        // pass the request along the filter chain 让资源放行
        chain.doFilter(request, response);
        System.out.println("after FilterDemo!!!");
    }

    /**
     * @see Filter#init(FilterConfig)
     */
    public void init(FilterConfig fConfig) throws ServletException {
        // TODO Auto-generated method stub
        System.out.println("test fliter init....");
    }

}

listener

1、web.xml

 <listener>
    <listener-class>com.htp.listener.ListenerDemo2</listener-class>
  </listener>

2、实现类

package com.htp.listener;

import javax.servlet.ServletRequest;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.annotation.WebListener;

/**
 * Application Lifecycle Listener implementation class ListenerDemo2
 *
 */
@WebListener
public class ListenerDemo2 implements ServletRequestListener {

    /**
     * Default constructor. 
     */
    public ListenerDemo2() {
        // TODO Auto-generated constructor stub
    }

    /**
     * @see ServletRequestListener#requestDestroyed(ServletRequestEvent)
     */
    public void requestDestroyed(ServletRequestEvent arg0)  { 
         // TODO Auto-generated method stub
        ServletRequest servletRequest = arg0.getServletRequest();
        System.out.println("ServletRequest destroyed. Remote IP="+servletRequest.getRemoteAddr());
    }

    /**
     * @see ServletRequestListener#requestInitialized(ServletRequestEvent)
     */
    public void requestInitialized(ServletRequestEvent arg0)  { 
         // TODO Auto-generated method stub
        ServletRequest servletRequest = arg0.getServletRequest();
        System.out.println("ServletRequest initialized. Remote IP="+servletRequest.getRemoteAddr());

    }

}

servlet-filter-listener

servlet-filter-listener

总结:
执行顺序为listener–filter—servlet