Servlet Filter (Filter) (8), servletfilter

Source: Internet
Author: User

Servlet Filter (Filter) (8), servletfilter

1. servlet filter background

In the project, we will encounter such a requirement to filter the ip addresses of users accessing the server. Only the ip addresses in the list can access the service. To meet the requirement, whenever a client request is made, we will write the code for verifying the ip address. We need to do this for the servlet that can be accessed by the client. It is obvious that code redundancy is inconvenient to maintain. If the verification rules change, it is also very troublesome to modify.

To solve the above problems, the Filter technology came into being.

2. What is a servlet filter?

Servlet filters are defined in java servlet 2.3. It can check and modify the ServletRequest object and ServletResponse object that the servlet container passes to the servlet component.

3. Case studies

  All servlet filters must implement the javax. servlet. Filter interface.

 NoteFilter

Package com. learn; import javax. servlet. *; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import java. io. IOException; import java. io. printWriter;/*** Created by Administrator on login /09/29. */public class NoteFilter implements Filter {private FilterConfig config = null; private String ipTable = null; // ip address list @ Override public void init (FilterConfig filterCon Fig) throws ServletException {System. out. println ("note filter initial"); this. config = filterConfig; this. ipTable = config. getInitParameter ("ipTable") ;}@ Override public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {System. out. println ("do filter starting"); // check the IP address if (! VeryfyIP (request, response) return; long befor = System. currentTimeMillis (); config. getServletContext (). log ("before call note Filter"); chain. doFilter (request, response); config. getServletContext (). log ("after call note Filter"); long after = System. currentTimeMillis (); String name = ""; if (request instanceof HttpServletRequest) {name = (HttpServletRequest) request ). getRequestURI ();} config. getServletContext (). log ("Note Filter: name:" + name + "time:" + (after-befor) + "ms") ;}@ Override public void destroy () {} private boolean veryfyIP (ServletRequest request, ServletResponse response) {String ip = request. getRemoteAddr (); System. out. println ("request ip:" + ip); System. out. println ("ipTable blacklist:" + ipTable); if (ip. indexOf (ipTable) =-1) {System. out. println ("Verification Failed"); response. setContentType ("text/html"); PrintWriter out = null; try {out = response. getWriter (); out. print ("

NoteServlet

Package com. learn; import javax. servlet. servletException; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import java. io. IOException;/*** Created by Administrator on login /09/29. */public class NoteServlet extends HttpServlet {@ Override protected void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System. out. println ("filter passed ");}}

Web. xml configuration

<filter>        <filter-name>ip</filter-name>        <filter-class>com.learn.NoteFilter</filter-class>        <init-param>            <param-name>ipTable</param-name>            <param-value>127.0.0.1</param-value>        </init-param>    </filter>    <filter-mapping>        <filter-name>ip</filter-name>        <url-pattern>/note</url-pattern>    </filter-mapping>
<servlet>        <servlet-name>note</servlet-name>        <servlet-class>com.learn.NoteServlet</servlet-class>    </servlet>    <servlet-mapping>        <servlet-name>note</servlet-name>        <url-pattern>/note</url-pattern>    </servlet-mapping>

The result is as follows:

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.