File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes 404 Error when trying to use a filter for a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "404 Error when trying to use a filter for a servlet" Watch "404 Error when trying to use a filter for a servlet" New topic
Author

404 Error when trying to use a filter for a servlet

Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Im trying to use a single filter for my servlets and jsp's. The filter works perfectly fine with the jsp's(placed in the pages subfolder ). However , when I try to apply the same filter to a set of servlets by modifying their url patterns I face a difficulty .
I modify the mapping in the web .xml as below


and a corresponding change in the associated jsp by making it submit to the following path

When I submit to the servlet from the corresponding jsp , I get 404 error , any idea what I could be doing wrong?
Thank you for your time.


Learning and Learning!-- Java all the way!
amit punekar
Ranch Hand

Joined: May 14, 2004
Posts: 511
Hello,
Try to print the Action URL before posting the form. This might give you a clue if URL is being formed incorrectly.

Regards,
Amit
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Well , the fact is that the' Action ' was working perfectly up until the addition of 'secure/' to enable the mapping of URL's .Also how do I print the action URL in the servlet?
amit punekar
Ranch Hand

Joined: May 14, 2004
Posts: 511
I was asking you to print the URL in JSP using JSTL.
Anyway did you try using "<servlet-name>" instead of "<url-pattern>". I know that this would be workaround but that will help you to check if request reaches to the servlet via filter.

Regards,
Amit
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60748
    
  65

Does it work when you disable the filter?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Bear Bibeault wrote:Does it work when you disable the filter?

Nope. Doesnt work even when I disable filter as in remove the filter mapping to secure and leave the jsp and url pattern mapping in the web.xml untouched
@ Amit, I didnt quite understand what you meant there.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60748
    
  65

Then the whole filter thing is a red herring. You should have tried that first off. Is the web app even running? Anything in the logs?
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Sorry for following this up so late , had to be involved with another project. I've tried to fix this issue without success. The thing is the whole web app is running perfectly and everything is taken care except this security feature.
I've also tried logging and this is the log that I'm able to generate when I access the system through the servlet. And with reference to Bear's earlier question , I only meant to say that the system does not work when I change the url mappings .
This is the code in my filter:

And the logs, seem perfectly normal too:
Coming from http://localhost:8080/CISSource/
The previous page null
com.enzen.cis.bean.User@db23f1
Checking does not belong to be checked
The previous page null
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

I lost track of what you think your problem is. If it's that "previous page" business... you shouldn't be surprised that it always shows up as null, because you don't have any code which assigns a value to it. And it must be an instance variable of the filter, which seems like a really bad idea to me, because it certainly isn't a local variable in the method you posted.

I also can't see where you have anything which logs "Coming from". Perhaps the code you showed us isn't the code which is running?
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Thanks for the reply Paul
Yes I understand that , the problem is that I'm not being able to secure the servlets . in a manner that Im trying to secure my web pages . I have already explained what Im trying to do to secure it , in the first post of this thread .
And No I did not mean to show the 'previous page ' as the source of the problem , I only meant to show whatever logs that are generated when the filter is called.And In context ,it doesnt even make sense . Would you mind telling me what logs are you wanting to see?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60748
    
  65

As already pointed out, if the servlet doesn't work without the filter, adding one will not make it magically start to work. Have you gotten the servlet to work without the filter? If not, we're all wasting our time.
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

In short ,Yes the servlet does work without the filter . The only instance during which it fails is when I modify the url mapping , and here too its not failing or anything . Its just throwing a 404 when I redirect to it from the JSP .Hope Im being clear in getting my point across.
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

I'm also open to furnishing any more info , If required . As of now , Im just stuck rather hopelessly :-/
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60748
    
  65

Vic Hood wrote: a 404 when I redirect to it from the JSP .Hope Im being clear in getting my point across.
In that case, the next thing I'd do is to check the redirect URL as the next most likely culprit.
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Thanks for the reply !
If by redirect url you mean the
<url-pattern > in web.xml , and the corresponding path to which the jsp submits to be modified , this is what Im doing(Like I tried in the first post)
web.xml


And in the web page .
.
This throws the 404 error.
Or are you meaning something completely different by redirect-url?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60748
    
  65

No, how is that a "redirect URL"? You wrote:

Vic Hood wrote:Its just throwing a 404 when I redirect to it from the JSP .


What is the URL you are using to redirect? Does it contain the context path?
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Uhm , My bad , so I think you mean this?
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

An UPDATE:
I tried this too , but with no success..

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60748
    
  65

You didn't answer my question, but I can see that the redirect URL does not start with the context path. Page-relative URLs such as that which you are using are almost guaranteed to cause trouble. Start all client-URLs (which includes redirect URLs) with the context path.
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Bear Bibeault wrote:You didn't answer my question, but I can see that the redirect URL does not start with the context path. Page-relative URLs such as that which you are using are almost guaranteed to cause trouble. Start all client-URLs (which includes redirect URLs) with the context path.

Okay two things .
Firstly your questions were.?
Bear Bibeault wrote:What is the URL you are using to redirect? Does it contain the context path?

I think I answered this in my post above and you clarified that it does not contain the context path .
Secondly,
How do I add the context path?
I was under the assumption that request.getContextPath(), got me the context path , if not what do I need to do to get it?

Thank you for your response.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60748
    
  65

You forgot the slash after the context path.

Note that for the root application, the context path is the empty string.
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Bear Bibeault wrote:You forgot the slash after the context path.

Note that for the root application, the context path is the empty string.

Okay fine. Im doing that now and getting to print the url as well . in this snippet

The URL prints as --URL/CISSource/pages/search.jsp but the 404 still remains , to clarify the structure of my project , CISSource is the name of my project and all my pages including the search page are stored inside the pages folder , what else am I missing from the url?
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3816

What have you mapped /pages/* in to (any servlets)? Because your redirect is formed with that pattern and not /secure/* ?


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Thanks for your reply Vijitha!
The problem stands solved , like Bear pointed out it, I had to just insert a blank space to indicate the empty string . in place of the context path .
As for your question Im mapping all my jsp's to the /pages/* as they are placed in the pages folder.
Once again Thanks a ton Bear and Paul!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: 404 Error when trying to use a filter for a servlet
 
Similar Threads
Form-based authentication not working
Problems with Java Melody
Using a filter to secure a set of servlets.
NullPointerException in AutoScrollPhaseListener
Deploy JSF Problem