"Filter" Interceptor filter

Source: Internet
Author: User

Some pages in the site often need to check whether the user has permission to release access, prohibit users to enter the URL directly to access. You can put such a page in the Web-inf folder, but this is not clear enough. General use of interceptors filter to reality. Interceptor Filter is a javaee,javaweb inside a very common thing, but the internet is always a large amount of articles let people too long not to see, more scary is to explain the Interceptor filter article is always a mixture of other technology to let people confused. is actually a more advanced servlet than the servlet. Configure the filter to jump to the appropriate Java file for each visit to the specified page to check first. As you can see, some Java EE components need to be configured with interceptor filter, in fact, in order to put the inside of this component of the file should be in any of the Web page.


I. BASIC OBJECTIVES

The website structure diagram is as follows:


requirements, before the user is not logged in, that is, in the session container difficult to find username items, in addition to login.jsp, other pages can not be accessed, once the access immediately jump back to login.jsp, such as:



Second, the production process

The process of login here is saved, the main explanation of how the filter is configured, has been done in many places, such as "Servlet" according to the concept of MVC design user login, user registration, Change password system (click to open the link), "Struts2" to create a simple, Basic STRUTS2 Project (click to open link), etc.

1, the first new project to put the corresponding servlet package into the Lib folder, Tomcat is well-equipped with the best, in the Web. config filter

<?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee/http Java.sun.com/xml/ns/javaee/web-app_2_5.xsd "version=" 2.5 "><filter><filter-name>loginfilter</ filter-name><filter-class>loginfilter</filter-class></filter><filter-mapping>< filter-name>loginfilter</filter-name><!--written/application/* every time you visit a page in the application directory, you should skip to the Java file and check <url-pattern>/*</url-pattern></filter-mapping></web-app>
The above means that all pages that visit this Web project are loginfilter.java to the root directory, and the name of this interceptor is called Loginfilter, and the simplest servlet javaweb program of the "Servlet" (Click the Open link) The servlet configuration is exactly the same!

2, in the network engineering catalogue casually new three jsp,login.jsp,newfile.jsp,welcome.jsp inside nothing to write, change the title, in the page hit a few English, clear is which page can be, the name is not important, I disorderly up.

3. Finally, the core of the entire interceptor filter is the Java file Loginfilter.java, which first introduces the following package at the beginning:

Import Java.io.*;import Javax.servlet.*;import javax.servlet.http.*;

After overriding the filter interface, you can automatically generate all the methods within the method by tapping the implements filter and using the Automatic de-error function of Eclipse for Java EE


Next, complete the construction method of the Interceptor filter init and the Destructor method destroy, and then modify the Dofilter method inside the Java EE generated parameters args0,args1, although theoretically and actually, do not change this parameter is not a problem, but, Generally change back to request, etc., let other people look at your program clear what this is. It is customary to change into request,response and chain.


Finally, the key interceptor filter execution Method Dofilter, intercept what, release what, are here.

Import java.io.*;import javax.servlet.*;import javax.servlet.http.*;p ublic class Loginfilter implements Filter {// The following things must have//here is the interface, Java requires that all of its methods must be rewritten private filterconfig config;//equivalent to the constructor, so that it can be combined with Web. Xml @overridepublic void init ( Filterconfig arg0) throws Servletexception {//TODO auto-generated method stubthis.config = config;} Equivalent to the destructor @overridepublic void Destroy () {//TODO auto-generated method stubthis.config = null;} The above thing eclipse for Java EE will be automatically generated without tube//But notice to modify the parameters of DoFilter public void DoFilter (ServletRequest request, Servletresponse Response,filterchain chain) throws IOException, Servletexception {//This is equivalent to the request object and response object inside the JSP, You can later use the method inside these objects//its source and abstract class ServletRequest and servletresponse coercion type conversion httpservletrequest HttpRequest = (httpservletrequest) Request HttpServletResponse HttpResponse = (httpservletresponse) response;//take the user name from the session container this property httpsession session = Httprequest.getsession ();//If the session container does not have a user name and is not going to access the login page login.jsp if (Session.getattribute ("username") = = null& &!httPrequest.getservletpath (). EndsWith ("/login.jsp")) {//Then, using redirection coercion that discards all parameters Login.jsphttpResponse.sendRedirect (" Login.jsp ");} else {//otherwise, release chain.dofilter (request, Response);}}}

The redirect here uses the response redirect, which is a redirect that loses all parameters before Request.getrequestdispatcher ("/form.jsp"). Forward (Request,response); Jsp "Forward Instructions" (Click the Open link), with parameters.



"Filter" Interceptor filter

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.