Use of filters in Servlet/jsp

Source: Internet
Author: User

A filter is an object that can transmit requests or modify responses. It can pre-process the request before it reaches the servlet/JSP, and can post-process it after the response leaves the servlet/JSP. So if you have several servlets/JSPs that need to perform the same data conversion or page processing, you can write a filter class and then deploy the description file (web. XML.

You can use a filter to act on one or more servlets. Zero or multiple filters can filter one or more servlets. A filter implements the java. servlet. Filter interface and defines three methods:

1. Void Init (filterconfig config) throws servletexception: called before the filter executes the service to set the configuration object of the filter.

2. Void destroy (); called after the filter executes the service.

3. Void dofilter (servletrequest req, servletresponse res, filterchain chain) throws ioexception, servletexception; perform the actual filtering.

The server calls Init (filterconfig) once to prepare a filter for the service, and then calls dofilter () whenever the request requires a filter (). The filterconfig interface retrieves the Filter Name, initialization parameters, and active servlet context. The server calls destory () to indicate that the filter service has ended.

In the dofilter () method, each filter accepts the current request and response, while the filter contained in the filterchain must still be processed. In the dofilter () method, the filter can do everything it wants for the request and response. (As I will discuss later, collect data by calling their methods, or add new behaviors to objects .)

Chain. dofilter () transfers control to the next filter. After this call is returned, the filter can do other work for the response at the end of its dofilter () method; for example, it can record the response information. If the filter wants to terminate the processing of the request or has full control over the response, it does not call the next filter.

The following is an example of a filter used to obtain the real path of the current web application. Because this path is frequently required in the program, it can be obtained by the browser and put into a session, then the program can be used elsewhere.

Package wasingmon. Common;

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. http. httpservletrequest;
Import javax. servlet. http. httpsession;

Public class appcontextfilter implements filter {
Private filterconfig Config = NULL;
Public void Init (filterconfig config) throws servletexception {
This. Config = config;
}
Public void dofilter (servletrequest requestx, servletresponse response, filterchain chain) throws ioexception, servletexception {
Httpservletrequest request = (httpservletrequest) requestx;
Httpsession session = request. getsession ();
Appcontext = (appcontext) Session. getattribute ("wasingmon. Common. appcontext ");
If (appcontext = NULL ){
Appcontext context = new appcontext ();
String webrealpath = request. getsession (). getservletcontext (). getrealpath ("/");
If (webrealpath. endswith ("//"))
Context. setwebrealpath (webrealpath );
Else
Context. setwebrealpath (webrealpath + "//");
Session. setattribute ("wasingmon. Common. appcontext", context );
}
Chain. dofilter (request, response );

}
Public void destroy (){
This. Config = NULL;
}

}

Appcontext stores the upstream and downstream information;

Package wasingmon. Common;

Public class appcontext {
 
Private string webrealpath = NULL;

Public String getwebrealpath (){
Return webrealpath;
}

Public void setwebrealpath (string webrealpath ){
This. webrealpath = webrealpath;
}

}
In web. XML, deploy it with the <filter> tag:

<Filter>
<Filter-Name> appcontext </filter-Name>
<Display-Name> appcontext </display-Name>
<Filter-class> wasingmon. Common. appcontextfilter </filter-class>
</Filter>
<Filter-mapping>
<Filter-Name> appcontext </filter-Name>
<URL-pattern>/* </url-pattern>
</Filter-mapping>
In this way, the filter settings are completed. The filter will be executed when you access JSP/servlet later.

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.