File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Servlet Mapping, HTTP requests, and Error Handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet Mapping, HTTP requests, and Error Handling" Watch "Servlet Mapping, HTTP requests, and Error Handling" New topic

Servlet Mapping, HTTP requests, and Error Handling

Steven Walkling

Joined: Nov 17, 2011
Posts: 3
Howdy folks!

I've been reading the Ranch for a long time, but this is my first time actually submitting a question. I appreciate any advice I can get! I'm having some trouble understanding the way servlet mapping and URL patterns work.

Here's the problem: I'm trying to get my server to take an HTTP request with or without parameters (ex: http://my.server:31337/CONTEXT_PATH/index.jsp?param1=X ) and run a servlet which sends back either a text file with data based on the parameters (if everything works right) or the error page "/error.jsp" (if an exception is thrown while attempting to make the file).

What I get is that no matter what I throw at the server, I'm getting the same index.jsp file back, UNLESS I set the URL map from "/" to "/*" (which is bad, because now responding with error.jsp causes the server to go back to the servlet, thus perpetuating an infinite loop).

I've read through the specs and I've also read this thread: Both of which were helpful for syntax, but I think I'm stumbling over the implementation, or overlooking something important.

Here is my code for web.xml.

So, the intention is that regardless of what URL string I get from the client (no parameters, bad parameters, or good parameters), my server sends it to the servlet and lets it either send a file or an error page back to the client.

My understanding is that having <url-pattern>/</url-pattern> in the servlet mapping means anything that goes to my servlet's context path and isn't otherwise defined automatically goes through that Srv.

Instead, what I get with the above mapping is that Srv seems to get ignored altogether, and the client only sees the index.jsp page. Am I overlooking something here?

Let me know if this helps. Thanks!
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63870

If it's request parameters you are concerned with, they have nothing at all to do with the mapping. A URL that addresses a servlet mapping will invoked the serve lets regardless of the presence of parameters or not.

[Asking smart questions] [About Bear] [Books by Bear]
James Boswell

Joined: Nov 09, 2011
Posts: 1051

Hi Steven

What happens if you use this configuration:

Miku Ranjan
Ranch Hand

Joined: Oct 11, 2011
Posts: 98
As you are using in the servlet url pattern of web.xml as / or /* so every request will go to that servlet only so please check that if you need like that or what.
Steven Walkling

Joined: Nov 17, 2011
Posts: 3

Thanks. That clears up one of the concerns I was having.

James & Miku,

Using /* redirects EVERY url with that servlet's context path right to the servlet.

So, if I have an error page that's in the servlet, it'll cause an infinite loop from servlet to error page to servlet. As Bear mentioned in the other thread, it's seldom useful for most situations.


Found a solution for this issue. Instead of using / or /*, I instead assigned the url-pattern to "/Srv". My web.xml now looks like this:

The link I use to access the servlet now looks like this: http ://my.server:31337/CONTEXT_PATH/Srv?param1=X

Instead of this: http ://my.server:31337/CONTEXT_PATH/index.jsp?param1=X

The part that the mapping seems to care about is this part (bold & underline): http //

Added: Sure enough, just added a second mapping property to the web.xml.

Now this path works as well: http ://my.server:31337/CONTEXT_PATH/index.jsp?param1=X
I agree. Here's the link:
subject: Servlet Mapping, HTTP requests, and Error Handling
jQuery in Action, 3rd edition