Web.xml files in the filter configuration detailed __web

Source: Internet
Author: User

Starting with j2ee1.3, support for filters is added to the Servlet2.3 specification, and filters are able to intercept requests for target resources and accordingly. The filter works in 4 different ways: Request filter, include filter, forward filter, error filter.

The work of the filter is divided into the same as I understand. Let's talk about Java handling filter requests first

public class Authorityfilter implements filter {//filterconfig can be used to access the configuration information of the filter private filterconfig config;//implementation initialization method PU 
Blic void init (filterconfig config) {this.config = config; 
}//Implementation destroy method public void Destroy () {this.config = null; }//Execute core filtering method public void Dofilter (ServletRequest request, servletresponse response, Filterchain chain) throws Ioexceptio
n,servletexception {//Gets the configuration parameter of the filter String encoding = Config.getinitparameter ("encoding");
String LoginPage = Config.getinitparameter ("LoginPage");
String Prologin = Config.getinitparameter ("Prologin");                    Sets the character set request.setcharacterencoding (encoding) for request encoding;
①httpservletrequest requ = (httpservletrequest) request;
HttpSession session = Requ.getsession (true);
Gets the client-requested page String Requestpath = Requ.getservletpath (); If the session scope user is NULL, it indicates that there is no login//and the user requested neither the login page nor the page to process the login if (Session.getattribute ("user") = = NULL &&! Requestpath.endswith (LoginPage) &&!requestpath.endswith (Prologin)) {//forwaRd to login page request.setattribute ("Tip", "You are not logged in");
Request.getrequestdispatcher (LoginPage). Forward (request, response);
}//"release" Request else {Chain.dofilter (request, response);} }

The first three lines of code in the Dofilter method of filter above are used to obtain configuration parameters for the filter. The ① code sets the character set used by the request encoding according to the configuration parameters. Next to determine whether there is a user attribute in the session scope, no such attribute is considered not logged in, if neither login, and the request address is not a login page and processing login page, then the system directly jump to the login page.

The configuration fragment of the filter in Web.xml is as follows:

<!--defines the name of the filter--> <filter> <!--filter,--> Filter-name> <!--filter implementation class--> <filter-class>lee. Authorityfilter</filter-class> <!--The following 3 Init-param elements are configured with 3 parameters--> <init-param> <param-name> encoding</param-name> <param-value>GBK</param-value> </init-param> <init-param> <
Param-name>loginpage</param-name> <param-value>/login.jsp</param-value> </init-param> <init-param> <param-name>proLogin</param-name> <param-value>/prologin.jsp</ Param-value> </init-param> </filter> <!--define filter blocking URL address--> <filter-mapping> <!-- Filter's name--> <filter-name>authority</filter-name> <!--filter is responsible for intercepting the URL--> <url-pattern>/ *</url-pattern> </filter-mapping> 
The configuration fragment above specifies 3 configuration parameters for filter, and specifies that LoginPage is/login.jsp,prologin as/prologin.jsp. This indicates that if the application is not logged in, ordinary users can only access the login.jsp and prologin.jsp pages, and they will be free to access other pages only after they have logged in.

Configure filter in Web.xml to configure parameters for this filter using only init-param elements, and Init-param can receive two child elements as follows:

Param-name: Specifying parameter names

Param-value: Specifying parameter values

In addition, the filter class specified in Filter-class needs to inherit Javax.servlet.Filter, and there are three ways to do this:

Init (filterconfig filterconfig): Initialize, generally read the Init-param parameter values in the configuration file, such as Filterconfig.getinitparameter ("encoding")

Dofilter (...) : Used to process request,response, and through Chain.dofilter (...) Give it to the next controller.

Destroy (): Resource Destruction



Related Article

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.