Common Java _ Chang rupeng Filter applications

Source: Internet
Author: User

1. Common Filter applications (2) ------ page not cached
1. Disable the browser from caching all dynamic page filters:
(1) Three HTTP response header fields can disable the browser from caching the current page. Their instance code in the Servlet is as follows: response. setDateHeader ("Expires",-1 );
Response. setHeader ("Cache-Control", "no-cache ");
Response. setHeader ("Pragma", "no-cache ");
(2) Not all browsers fully support the above three response headers, so it is best to use the above three response headers at the same time.
(3) Expires data header: The value is the GMT time value.-1 indicates that the browser does not cache pages.
2. the Cache-Control response header has two common values:
(1) no-cache indicates that the browser does not cache the current page
(2) max-age: xxx indicates that the browser caches the page for xxx seconds.
Instance:
[Java]
Package com. hbsi. 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;
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;
Public class NoCatchFilter implements Filter {
Public void destroy (){
// TODO Auto-generated method stub
}
Public void doFilter (ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse resp = (HttpServletResponse) response;
Resp. setDateHeader ("expires",-1 );
Resp. setHeader ("Cache-Control", "no-cache ");
Resp. setHeader ("Pragma", "no-cache ");
Chain. doFilter (req, resp );
}
Public void init (FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub
}
}

2. Common Filter applications (3) ------ control the Filter of static resources on the browser cache page
1. Scenario: some images or css files are referenced in some dynamic pages to modify the page effect. These images and css files are often unchanged, so to reduce the pressure on the server, you can use filter to control the cache of these files in the browser to improve the server performance.
Instance:
[Java]
Package com. hbsi. 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;
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;
Public class ExpiresFilter implements Filter {
Private FilterConfig config;
Public void destroy (){
// TODO Auto-generated method stub
}
Public void doFilter (ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse resp = (HttpServletResponse) response;
String uri = req. getRequestURI ();
If (uri. endsWith (". css ")){
Long time = Integer. parseInt (config. getInitParameter ("css") * 1000;
Resp. setDateHeader ("expries", System. currentTimeMillis () + time );
} Else if (uri. endsWith (". jpg ")){
Long time = Integer. parseInt (config. Fig ("jpg") * 1000;
Resp. setDateHeader ("expries", System. currentTimeMillis () + time );
} Else if (uri. endsWith (". js ")){
Long time = Integer. parseInt (config. getInitParameter ("js") * 1000;
Resp. setDateHeader ("expries", System. currentTimeMillis () + time );
}
Chain. doFilter (req, resp );
}
Public void init (FilterConfig filterConfig) throws ServletException {
This. config = filterConfig;
}
}

Filter configuration in Web. xml:
[Html]
<Filter>
<Filter-name> ExpiresFilter </filter-name>
<Filter-class> com. hbsi. filter. ExpiresFilter </filter-class>
<Init-param>
<Param-name> css </param-name>
<Param-value> 120 </param-value>
</Init-param>
<Init-param>
<Param-name> jpg </param-name>
<Param-value> 120 </param-value>
</Init-param>
<Init-param>
<Param-name> js </param-name>
<Param-value> 120 </param-value>
</Init-param>
</Filter>
<Filter-mapping>
<Filter-name> ExpiresFilter </filter-name>
<Url-pattern> *. css </url-pattern>
</Filter-mapping>
<Filter-mapping>
<Filter-name> ExpiresFilter </filter-name>
<Url-pattern> *. jpg </url-pattern>
</Filter-mapping>
<Filter-mapping>
<Filter-name> ExpiresFilter </filter-name>
<Url-pattern> *. js </url-pattern>
</Filter-mapping>
3. Common Filter applications (4) ------ use Filter to implement URL-level permission Authentication
Scenario: in actual development, we often map some servlets that execute sensitive operations to some special directories and use filters to protect these special directories, restrict users with access permissions to access resources in these directories. This allows us to implement a URL-level permission function in our system.
Requirements: to make the Filter generic, the resources protected by the Filter and the corresponding access permissions are configured in the form of filter parameters.
Iv. Common Filter applications (5) ------ achieve automatic login
After a user logs in successfully, a cookie named user is sent to the client. The cookie value is the user name and the md5 encrypted password.
Compile an AutoLoginFilter. This filter checks whether the user has a cookie named user. If so, the user name and password that calls dao to query the cookie match the database, the user object (that is, the user login tag) is saved to the session for automatic login.

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.