Site Filters Filter

Source: Internet
Author: User

--Filter has been used for a long time, today encountered a small problem, also want to write a filter on the blog post, record the feelings of their own use.


In fact, the filter is similar to the servlet, except that the filter's Dofilter () method has more than one filterchain parameter to control whether the user's request is released. The site has a filter is very convenient, filter can be filtered to whether the user has landed, if not logged in, then will restrict TA access, and filter settings and configuration is relatively simple. The following is a simple way to say the filter using the code of a login filter:


/** * Login Filter */public class Loginfilter implements filter {@Overridepublic void Destroy () {} @Overridepublic void DoFilter (Se Rvletrequest req, Servletresponse resp,filterchain chain) throws IOException, servletexception {httpservletrequest hsq = (HttpServletRequest) req;//gets the Loginuser object in session User User = (user) hsq.getsession (). getattribute ("Loginuser"); if ( User==null | | ". Equals (user)) {//If the user is not logged in, it will jump directly to the landing page ((httpservletresponse) resp). Getwriter (). Write (" <script> window.parent.location.href= '/index.jsp ' </script> ');//((HttpServletResponse) resp). Sendredirect ("/ Index.jsp ");} Else{chain.dofilter (req, resp);}} @Overridepublic void init (Filterconfig arg0) throws servletexception {}}
 

The above filter will filter out all the non-logged users, let Ta jump to the landing page to login. If you want this login filter to work, you also need to configure it in the Web. xml file:


<!--Configuring Filters--  <filter>    <filter-name>LoginFilter</filter-name>    < filter-class>com.common.loginfilter</filter-class>  </filter>  <filter-mapping>    <filter-name>LoginFilter</filter-name>    <url-pattern>/user/*</url-pattern>  </filter-mapping>

This means that any user accessing the/user/* path below will pass through this filter, if not logged in, the session does not have user, then will jump to the landing page. If multiple addresses need to be configured (in the SSM framework, each controller needs to be configured), the correct configuration method is (n):


<filter>    <filter-name>LoginFilter</filter-name>    <filter-class> com.common.loginfilter</filter-class>  </filter>  <filter-mapping>    <filter-name >LoginFilter</filter-name>    <url-pattern>/user/*</url-pattern>  </filter-mapping >  <filter-mapping>    <filter-name>LoginFilter</filter-name>    <url-pattern> /abc/*</url-pattern>  </filter-mapping>

do not know if you have noticed the above filter line is commented code, normally use this code to complete the jump task:

((HttpServletResponse) resp). Sendredirect ("/index.jsp");

The following line of code is:

((HttpServletResponse) resp). Getwriter (). Write ("<script>window.parent.location.href= '/index.jsp ' </ Script> ");

The bottom line of code is to solve the small problem I encountered this time. Because the need to adjust the page is frameset frame, so there are many links defined to jump to a place under the frame, so if the use of the above jump, will be a partial jump, can not achieve the desired effect, only use the following way to return a sentence JS code, and then execute, the whole jump, This will satisfy the effect.








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.