Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6824503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:54:33+00:00 2026-05-26T21:54:33+00:00

I would like some help in understanding a particular behaviour of java Filters: I

  • 0

I would like some help in understanding a particular behaviour of java Filters: I wrote a simple Filter which gets all user requests and, if a non-logged user requires a restricted resource, the filter forwards user to the home page. Here is my code:

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    // TODO Auto-generated method stub
    // place your code here

    HttpServletRequest req = (HttpServletRequest) request;
    String uri = req.getRequestURI();

    System.out.println("\n\nFILTERING...\n\n");

    //Se la risorsa appartiene all'area ristretta e l'utente non è
    //loggato lo sbatto fuori
    if(uri.contains("restricted") && (req.getSession(false) == null || req.getSession(false).getAttribute("user") == null)) {

        System.out.println("\n\nCannot access\n\n");
        //((HttpServletResponse) response).sendRedirect("/Hotel/index.jsp");
        req.getRequestDispatcher("/index.jsp").forward(request, response);
    }
    else {
        // pass the request along the filter chain
        System.out.println("\n\nNext step\n\n");
        chain.doFilter(request, response);
    }
}

And the mapping in the web.xml:

<filter>
<filter-name>MyFilter</filter-name>
<filter-class>mycontroller.MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>MyFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> 

What it is strange to me is that, if I ask a restricted resource via URL, the doFilter method is called several times before moving to the home (the server logs ‘FILTERING… Cannot access’ 4,5 times).
I’m using Tomcat 7.
Can someone help me to understand? Thanks a lot

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:54:33+00:00Added an answer on May 26, 2026 at 9:54 pm

    You’ve mapped the filter on /*. It will thus intercept on every single HTTP request. Not only HTML/JSP pages, but also static resources like CSS, JS and image files. Apparently you’ve requested a HTML/JSP page which in turn references several CSS, JS and/or image files.

    Your check in the filter is also pretty poor. You should rather map the filter on /restricted/*.

    <url-pattern>/restricted/*</url-pattern>
    

    Then remove that URI check from the filter’s code. If you put those static resources outside that map, e.g. in /static or /resources, etc, then the filter won’t be invoked for them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like some help understanding how to deal with isstringstream objects. I am
Good morning! I would like to ask some help from you guys on how
I am developing a site and i would like some simple markup. I would
I know this is a really rookie question but would like some understanding off
I would like some advice on the best approach to use in the following
I would like some of my preferences to have icons, like the Settings app.
I am learning C++ exceptions and I would like some clarification of the scenario:
I am designing RESTful Api's and would like some advice on designing an API
I'm writing a small application in VB.NET and I would like some of the
We are designing a new database and I would like some input in where

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.