Basis of the timer (47) and basis of the timer (47)

Source: Internet
Author: User

Basis of the timer (47) and basis of the timer (47)

There are only three components in a web project:

Filter

Listener.

Servlet

In the web, the browser is a javax. servlet. Filter class.

The handler is used to overwrite the user's request or the response returned from the requested resource during execution ).

Package cn. hongxin. 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; public class OneFilter implements Filter {// This method will only be executed once at tomcat startup public void init (FilterConfig filterConfig) throws ServletException {System. err. println ("initiated by the handler:" + this) ;}// this method executes the excessive task public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {System. err. println ("1: being executed:" + this); // run the next Transcoder. If there is no next Transcoder, run the target servlet chain. doFilter (request, response); // 2: System. err. println ("3: Target component, completed... ");} public void destroy () {System. err. println ("Passer dead .. "+ this );}}

Step 2: configure the timer in web. xml

<Filter> <filter-name> one </filter-name> <filter-class> cn. itcast. filter. OneFilter </filter-class> </filter> <! -- Configure which url the timer is used to overwrite --> <filter-mapping> <filter-name> one </filter-name> <url-pattern>/one </url- pattern> </filter-mapping>

Step 3: test execution

Setting of url-pattern in filter-mapping of ilter

Url-pattern settings:

/* = Request this servlet for all URLs. | All are ignored by this filter.

/= Only servlet is allowed. -All other URLs not processed by the servlet are processed by the servlet specified by this url. Default servlet.

*. Jspx = all requests sent to this Servlet or are intercepted by a Filter after jspx ends. For example, http: // local: 8080/proj/abc. jspx

/Jsps/* = requests starting with/jsps/in all paths to this servlet or are intercepted by a filter.

/Jsps/*. jspx error: SUN stipulates that * characters cannot appear simultaneously on both sides.

<Filter-mapping> <filter-name> one </filter-name> <! -- All URLs are intercepted for excessive tasks --> <url-pattern>/index. jsp </url-pattern> <url-pattern>/jsps/* </url-pattern> <! -- Intercept all url settings on the servlet --> <servlet-name> OneSerlvet </servlet-name> </filter-mapping>
Other configurations in the timer

If there is a jsps/abc. jsp page, there are two ways to display this page:

1: requests in the address bar: http: // local: 8080/project/jsps/abc. jsp -- can be intercepted because the path conforms to/jsp/abc. jsp.

2: req. getRequestDispathcer ("/jsps/abc. jsp"). forward (req, resp );-

Dipatcher attributes:

Request-by default, only new requests are blocked.

Forward-intercept forwarding.

Include-intercept include.

Error-blocks errors.

<! -- Set forwarding interception -->

<Dispatcher> REQUEST [W1] </dispatcher>

<Dispatcher> FORWARD </dispatcher>

 

7. Configure the initialization parameters of the timer.

There is a method in the filter class: init (FilterConfig config); and filtetrConfig contains reading the initialization parameter values from web. xml.

<Filter>

<Filter-name> one </filter-name>

<Filter-class> cn. itcast. filter. OneFilter </filter-class>

<Init-param>

<Param-name> name </param-name>

<Param-value> Jack Zhang San </param-value>

</Init-param>

<Init-param>

<Param-name> age </param-name>

<Param-value> 88 </param-value>

</Init-param>

</Filter>

Obtain the value in the filter class:

PrivateFilterConfig conf;

// This method will only be executed once at tomcat startup

Public VoidInit (FilterConfig conf)ThrowsServletException {

This. Conf = conf;

System.Err. Println ("the timer is initialized:" +This);

// Obtain the value of name:

String name = conf. getInitParameter ("name ");

System.Err. Println ("name is:" + name );

System.Err. Println ("----------------------------");

Enumeration <String> en = conf. getInitParameterNames ();

While(En. hasMoreElements ()){

String paramName = en. nextElement ();

String val = conf. getInitParameter (paramName );

System.Err. Println (paramName + "=" + val );

}

}

 

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.