Java Web The most convenient configuration filter method, how exactly is the filter configured?

Source: Internet
Author: User
Tags java web

Small white Study the arduous course!

I'm only talking about the latest version of Eclipse since servelt3.0 Servelt and filter Do not have to be configured in Web. XML, personal understanding of Servelt and filter is not too deep, but since servelt3.0, you set up the Servelt is not self-configuration, automatically configured for you (of course, you need to build with eclipse in the new servelt not you build a Class and then inherit the method in Servelt! Filter also does not need to be configured in Web. XML, just configure it in the filter file you created ...

How to configure the key as follows:

@WebFilter (asyncsupported = true, Urlpatterns = {"/loginfilter", "/succ.jsp", "Here you can write the page you want to filter}")

If you are a 3.0 later version you can see these words in the beginning! Urlpatterns behind write you want to filter the page must pay attention to the format (I found no matter how many folders you create, just write the file name you want to filter and a slash on the line, you can identify, I do not understand, haha ha)

The following dofilter methods are as follows

     Public voidDoFilter (servletrequest request, servletresponse response, Filterchain chain) throws IOException, Servletexception {        //TODO auto-generated Method Stub//Place your code hereHttpServletRequest httprequest=(httpservletrequest) request; HttpServletResponse HttpResponse=(httpservletresponse) response; HttpSession Session=httprequest.getsession ();//I used a session in front to determine if the user is logged in!!            Here to get the value, if not empty can jump!! if(Session.getattribute ("Succe")!=NULL) {System. out. println ("this one only thought once."); Chain.dofilter (request, response);//release can pass}Else{System. out. println ("page redirection was used once");Httpresponse.sendredirect ("You need to redirect the page"///here to jump to the login interface you need .            }            }

Of course, this I Baidu for a while, saw a lot of big guy's blog, thank them for their own study of a bit---share the experience!

Java Web The most convenient configuration filter method, how exactly is the filter configured?

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.