What is a Java Servlet filter?

Source: Internet
Author: User

Article transferred from: http://www.cnblogs.com/hongten/archive/2011/07/29/2120528.html

 

What is a filter?

Filters are used to send requests and

The content that the server responds to the browser, which can be filtered. Browsing can be intercepted during this filtering process.

The request sent by the server and the content that the server responds to the browser. After interception, you can view and

You can extract or modify the intercepted content.

Servlet filters intercept requests and responses for viewing, extracting, or operating between clients and servers

Exchange data.

Servlet filter usage:

User authentication and authorization management: when we develop a web application, we certainly have users with different permissions, including administrators and common users.

User. Administrators may be divided into level-1 Administrators, level-2 administrators, and level-3 administrators. Different level-1 Administrators may have different management permissions.

Operation to access different resources. In the past, we may all control permissions on JSP pages and servlets. Check whether session is used
This permission is granted. If yes, it is required to operate on a resource. These are all commonalities. Now we can extract it. Let him pass

Filters are implemented. Users can access a resource. We use filters to filter the request and determine whether the program has the permission to access the resource;

Yes, let him access it. If not, let him go to another page. In this way, authorization management is implemented through the filter;

Measure the web application traffic, access hit rate, and report;

Implement the log processing function of Web applications;

Implement data compression;

Encrypt the transmitted data;

Implement XSLT conversion of XML files;

Implement servlet Filter  

Write a class that implements the javax. servlet. fillter interface;

      package javax.servlet;      import java.io.IOException;      public interface Filter{        public void init(FilterConfig filterConfig)throws ServletException;        public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain)throws IOException, ServletException;        public void destroy();      }

 

Configure the servlet filter;

The filter needs to be configured through web. xml

<Filter> defines the name of the filter and declares the implementation class.

<Filter-mapping> associate the filter with servlet or URL Mode

        <filter>          <filter-name>EncodingFilter</filter-name>          <filter-class>webbook.hongten.CharacterEncodingFilter</filter-class>        </filter>        <filter-mapping>          <filter-name>EncodingFilter</filter-name>          <url-pattern>/*</url-pattern>        </filter-mapping>

-----------------------------------------------------------------------------

Example: Use a filter to set the encoding of Transmission Parameters

Create a WEB Project

Characterenccodingfilter. Java

Code:

package com.b510.hongten.filter;import java.io.IOException;import javax.servlet.FilterChain;import javax.servlet.FilterConfig;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;/*** * @author XHW* * @date 2011-7-29* */public class CharacterEnccodingFilter implements javax.servlet.Filter {private FilterConfig config;private String encoding = "ISO8859_1";public void destroy() {config = null;}public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)throws IOException, ServletException {request.setCharacterEncoding(encoding);chain.doFilter(request, response);}public void init(FilterConfig config) throws ServletException {this.config = config;encoding = config.getInitParameter("encoding");}}

-----------------------------------------------------------------------------

Web. xml

Code:

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><filter><filter-name>Encoding</filter-name><filter-class>com.b510.hongten.filter.CharacterEnccodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>UTF-8</param-value></init-param></filter><filter-mapping><filter-name>Encoding</filter-name><url-pattern>/*</url-pattern></filter-mapping><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list></web-app>

-----------------------------------------------------------------------------

Index. jsp

Code:

<Language = "Java" pageencoding = "UTF-8" %> <uri = "http://java.sun.com/jsp/jstl/core" prefix = "C" %> <HTML> 

 

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.