Aspect service-Application of filter in DRP Project

Source: Internet
Author: User

Filter is an important specification in servlet. It can intercept requests and response and perform preprocessing. That is to say, the request is preprocessed before the request arrives at the servlet, and the response is preprocessed before the reponse leaves the servlet. With filter, we can manage the system in a unified manner.

Take Character Set settings as an example. If we do not use filter, we need to set the character set in the servlet of each page:

The preceding figure shows the sequence of adding a user. When adding a user, we need to set a character set. When modifying a user, we still need to set a character set. If we have new requirements. No character set is required for all pages. Therefore, we need to modify each page, which is not conducive to the same control.

In addition, in the above-added user logic, the set character set is obviously an independent logical unit, so it is necessary to separate it.

The configuration in the web. xml configuration file is written in the configuration file to facilitate future modification of the character set, not in the program:

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"><filter><filter-name>CharsetEncodingFilter</filter-name><filter-class>com.xxjstgb.drp.util.filter.CharsetEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>GBK</param-value></init-param></filter>        <filter-mapping>                <filter-name>CharsetEncodingFilter</filter-name>                <url-pattern>*.jsp</url-pattern>        </filter-mapping>        <filter-mapping>               <filter-name>CharsetEncodingFilter</filter-name>               <url-pattern>/servlet/*</url-pattern>        </filter-mapping></web-app>

In filter-mapping of charsetencodingfilter, its url-pattern is *. jsp, which indicates that it works for all JSPs. /Servlet/* indicates that all URLs of/servlet/will be filtered.

Character encoding filter code:

Package COM. xxjstgb. DRP. util. 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 charsetencodingfilter implements filter {private string encoding; Public void destroy () {} public void dofilter (servletrequest request, servletresponse response, filterchain chain) throws ioexception, servletexception {// set the character set [This is not written to death, but read the configuration file] request. setcharacterencoding (encoding); // continue to run the chain. dofilter (request, response);} public void Init (filterconfig) throws servletexception {// read the parameters set in the XML file, which contains this. encoding = filterconfig. getinitparameter ("encoding ");}}

In this way, you can complete the unified character set settings.

In addition, the filter can still jump to the login page: first, the user needs to log on to the system through the login page; then, due to permission issues, after a user logs on to the system, enter the corresponding URL in the address bar and do not jump to the unauthorized page. Also, after the session expires, You need to jump to the login page and log on again. Users should not be allowed to continue accessing.

These problems can still be managed in a unified manner using filters. No. If you are interested, you can do it by yourself.

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.