Web Project listeners, filters, execution sequence tutorials for custom Servlets, listener servlets

Source: Internet
Author: User

Web Project listeners, filters, execution sequence tutorials for custom Servlets, listener servlets

The web Container will instantiate the contextInitialized (ServletContextEvent event) method of the listener as soon as it is started, and then the init () method of the filter, finally, when a user accesses a web application, the user will first execute the doFilter () method of the filter and the filter chain, and finally execute the doPost () that inherits the custom Servlet of HttpServlert () method or doGet method.

1. web Container initialization:

Initialization sequence:

A. Listener ServletContextListener interface implements class rewrite

1 public void contextInitialized(ServletContextEvent event) {2 3 }

B. Implementation of the Filter Interface

Public void init (FilterConfig filterConfig) throws ServletException {String para1 = filterConfig. getInitParameter ("para1"); System. out. println ("Wei Yongle WylFilter. init ()... ");}

If multiple filters exist, the initialization sequence of the filters is web. configuration in xml from bottom to top (Note: The initialization sequence during container startup is from bottom to top, but when the user accesses resources, the filter rewrites

Public void doFilter (ServletRequest request, ServletResponse response,

FilterChain chain) method is from top to bottom, that is, it is executed from top to bottom according to the configuration order in web. xml, but the initialization order is the opposite.

).

2. user access to web resources:

Container execution sequence:

A. The doFilter method of the filter,

@ SuppressWarnings ("all") public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {System. out. println ("Wei Yongle WylFilter. doFilter () begin... ");/*** set the collection, unified for the UTF-8, so there will be no garbled, because after the web container is started, * Each request will be intercepted by WylFilter first, so you only need to set the character set here, * You do not need to set the character set in the Custom Servlet each time */HttpServletRequest httpReq = (HttpServletRequest) request; HttpServ LetResponse resp = (HttpServletResponse) response; resp. setCharacterEncoding ("UTF-8"); resp. setHeader ("Content-type", "text/html; charset = UTF-8"); // added the checkLoginStatus (request, response); String requestURI = httpReq. getRequestURI (). substring (httpReq. getRequestURI (). indexOf ("/", 1), httpReq. getRequestURI (). length (); System. out. println ("requestURI:" + requestURI); // if the request uri does not contain login. jsp, then check whether the login is successful Recorded. If (requestURI. indexOf ("login. jsp") =-1) {// obtain the request content and filter the request content String reqContent = httpReq. getQueryString (); List
 
  
List = UtilProperties. getContentByKeytoStringArr ("keyContent. properties "," invalidWords "); // clear response first. getWriter (). write (""); // you can filter the content of reqContent if (null! = ReqContent) {for (int I = 0; I
  
   

B. Servlet's doGet (HttpServletRequest req, HttpServletResponse resp) method or protected void doPost (HttpServletRequest req, HttpServletResponse resp) method.

If multiple filters exist, the chain is executed in the first filter. doFilter (request, response) will execute the doFilter (request, response) method of the next filter after this line of code, until the doFilter (request, response) method of the last filter is executed, then, execute the doGet (HttpServletRequest req, HttpServletResponse res) method in the Servlet,

For example:

@ Override protected void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System. out. println ("Wei Yongle failed () begin..."); EnumerationEm = req. getHeaderNames (); if (em. hasMoreElements () {String headName = (String) em. nextElement (); String head = req. getHeader (headName); System. out. println ("Wei Yongle WylServlet1.doGet () method execution ..., header: "+ head);} String path = req. getContextPath ();/* resp. setCharacterEncoding ("UTF-8"); resp. setHeader ("Content-type", "text/html; charset = UTF-8"); */String content = "I am the Content of the PrintWriter object input to the page, Hahahaha, HttpServletRequest. getContextPath () "+ path; PrintWriter out = resp. getWriter (); out. write (content); System. out. println ("Wei Yongle WylServlet1.doGet () end... ");}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.