This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Servlets and the fly likes Problems with servlet filter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Problems with servlet filter" Watch "Problems with servlet filter" New topic
Author

Problems with servlet filter

Chad Cook
Greenhorn

Joined: Dec 02, 2004
Posts: 29
I'm using servlet filters for the first time on my current development project and I'm running into a couple of problems.

The first problem I ran into was that certain urls will not hit the filter, while others will. I read online and in these forums and found that it could be because I'm using a dispatch.forward(), whereas the filter looks for requests by default. So I added in the two dispatcher tags to web.xml to specify both the forward and the request. But now when I try to access my web-app I get the following exception on my filter chain:



Does anyone know what I'm doing wrong, or am I wrong in my assumption that I need to adjust the dispatcher in web.xml to ensure the filter snags the missing forwards?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61086
    
  66

No code? No config? How can we even guess?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Chad Cook
Greenhorn

Joined: Dec 02, 2004
Posts: 29
Erm, telepathy?

Here's my filter setup in web-xml:
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
Your mapping has already indicated that he filter should be applied to forwards, so not sure why it's not working. Do these resources that are forwarded to also match the url pattern .jsp?


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Chad Cook
Greenhorn

Joined: Dec 02, 2004
Posts: 29
All of the pages/resources that I am forwarding to are .jsp's.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

StackOverflow exceptions are often caused by endless recursion.
What are you doing in your filter?
Are you forwarding to something?
If so, is it possible that you've set up an endless loop by catching the forward with a filter that tries to forward which gets caught again by the filter which tries to forward which gets caught by the filter which tries to forward to something that gets caught by the filter......?

If it's not too long, post the code to your filter.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Chad Cook
Greenhorn

Joined: Dec 02, 2004
Posts: 29


I don't think there's an endless forward here, but this is my first time working with servlet filters so I could be missing something.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

You've got this filter configured to intercept calls to *.jsp; both for requests and forwards.

In this filter, you have a case in which it will forward to index.jsp.
Why wouldn't this loop endlessly?
Chad Cook
Greenhorn

Joined: Dec 02, 2004
Posts: 29
Good catch. Is there a way to configure the forward so that it will not catch index.jsp, but everything else? Is this the proper way to attack this?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Chad Cook wrote:Good catch. Is there a way to configure the forward so that it will not catch index.jsp, but everything else? Is this the proper way to attack this?



Not unless you have your index.jsp in a separate directory.
If it were me, I'd take a good look at why I need this filter to catch forwards.
Is there anything else you could map to in your URLs other than *.jsp?

Situations like this are a good example of why Front Controllers make sense.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61086
    
  66

Ben Souther wrote:Situations like this are a good example of why Front Controllers make sense.

Quoted for truth.
Eddy Pelaic
Greenhorn

Joined: Apr 09, 2009
Posts: 11
Hello,

It's a recursive loop because you filter all the *.jsp included index.jsp.

try <url-pattern>/NeoAdmin/*</url-pattern>

Hope your index.jsp is not inside /NeoAdmin/ directory.

@+
Edo...


SCJP 5.0 87%
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Eddy Pelaic wrote:...

try <url-pattern>/NeoAdmin/*</url-pattern>
...


I don't think there is enough information in this thread for us to make suggestions such as this (which is why I didn't make any concrete suggestions).
If there is an image in the NeoAdmin directory that gets used in the index.jsp page the filter would catch it and try to redirect it to the index.jsp with the URL pattern you've suggested.

The original poster needs to take a long look at how he's got his app structured and figure out whether there even is a url-pattern or group of them that would work. If not, he needs to consider restructuring the application so that this can be done.
Eddy Pelaic
Greenhorn

Joined: Apr 09, 2009
Posts: 11
Hello,

It's right, there is not enough informations, but if the index.jsp is in public "area", it should not use images or resources in secure directories. I consider that's all resources must be in a "resources" folder in public access, and specials critical resources in a subdirectory of secured directory or provided by secure code (filter/servlet/Jsp).
It's better to have a clean separation of public / secure.
The url pattern "*.jsp" works only for JSP's, but not for others extension or servlet's url "/myservlet.1234". So it's more difficult to ensure secure access for all resources with that url-pattern.

In the filter, he can check if the browser want to GET an picture and return a 404 code than forwarding to index page. But it's more complex to do and time consuming of CPU.



I did that for Flex UI HTTP/XML and Ajax request to not have the login page content in the response, because the forward send a 200 status code and it's simpler to handle status code than check the content of page.

@+
Edo...
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Eddy Pelaic wrote:... I consider that's all resources must be in a "resources" folder in public access, and specials critical resources in a subdirectory of secured directory or provided by secure code (filter/servlet/Jsp).


Another assumption that I wouldn't be prepared to make.
Again, if this were my project, I would take a good look at how things are organized and then determine whether there is a url-pattern (or group of them) that will work OR whether I need to restructure the app (possibly adding a front controller) to make it possible to do this cleanly.


Eddy Pelaic
Greenhorn

Joined: Apr 09, 2009
Posts: 11
Hello,

this problem could be a good question for SCWCD exam, Chad now knows why he get a StackOverflowException => infinite recursion and and how to fix it => web app structure / url-pattern.

I assumed with "*.jsp" url-pattern, that Chad's application is a "direct access to JSP's" based web app. For the Front controller pattern it's another topic, why only use Servlet's and Jsp's ?

@+
Edo.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Problems with servlet filter