Java Web Development Filter

Source: Internet
Author: User

We usually encounter a problem when surfing the internet, see a video and so on, want to open the watch, click after the page

Remind you: You are not logged in, do you want to sign in? Then Balabala ran to lose the account password.

So this is the function of a filter, when you want to access a resource (usually understood as a link), the server will

Analysis, whether the current visitor meets certain criteria (usually to detect if a member is already logged in), meets

Conditions, the server will jump to the specified link, not satisfied will also be friendly reminder: you are not a member, you have not logged in and so on.

How to implement it, or user login as an example, see below ~ ~ ~

1. Three pages

Login page, login Success page, login failure page

LOGIN.JSP---loginsuccess.jsp---loginwrong.jsp

2. Classes that implement the filter interface

This Java file implements the filter interface, sets the rules, and also has a configuration file

1  Public voidDoFilter (ServletRequest arg0, Servletresponse arg1,2Filterchain arg2)throwsIOException, servletexception {3HttpServletRequest request =(HttpServletRequest) arg0;4HttpServletResponse response =(HttpServletResponse) arg1;5         request.setcharacterencoding ("Utf-8");//Set Request Encoding6         response.setcharacterencoding ("Utf-8");//Setting the response encoding7HttpSession session =request.getsession (); //session object for storing form data 8String username = request.getparameter ("username");9String Password = request.getparameter ("Password");TenSession.setattribute ("username", username); OneSession.setattribute ("Password", password); ASystem.out.println ("Password:" +password); -SYSTEM.OUT.PRINTLN ("Account number:" + session.getattribute ("username"))); -         if(Username! =NULL&& Password! =NULL) { the             if(Username.equals ("admin") && password.equals ("admin"))) { -Arg2.dofilter (arg0, arg1);//meet the conditional release, jump to the page you want to get to the form loginsuccess.jsp -}Else { -Request.getrequestdispatcher ("loginwrong.jsp"). Forward (Request, +ARG1);//do not meet the conditions, go to the Loginwrong page -             } +         } A}

The configuration file stipulates that this rule applies to which page the client is accessing, calling the rule

1     <Filter>2         <Filter-name>Logfilter</Filter-name>3         <filter-class>Filter.Servletfilter</filter-class>4     </Filter>5     <filter-mapping>6         <Filter-name>Logfilter</Filter-name>7         <url-pattern>/loginsuccess.jsp</url-pattern>8     </filter-mapping>

Filter-name Custom, but the rule and the applicable page to correspond, filter rule class file to package name + file name form writing,

This file means that when the user wants to access the loginsuccess.jsp page, the filter servletfilter to intercept, and then

According to the rules, if the user's username and password are admin, the server jumps directly to the loginsuccess.jsp page,

If the account and password are incorrect, the server jumps to the loginwrong.jsp page.

Note: After the user submits the form, the request is intercepted by the filter, and the character encoding of the request and response is set to prevent

The output is garbled when the data is used later. The setting method in the filter source code has been marked with red characters.

Java Web Development Filter

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.