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 problem with using URIs as parameters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "problem with using URIs as parameters " Watch "problem with using URIs as parameters " New topic
Author

problem with using URIs as parameters

Alexey Gor
Greenhorn

Joined: Jul 13, 2004
Posts: 27
hello,

Not sure if I am just confused or is this is a real problem...

I am coding an app where URI of the request identifies information to show. This URI should be processed by the central Servlet. This servlet should then forward to a JSP.

So I make all URIs go through my servlet by mapping it in web.xml like so:

<servlet-mapping>
<servlet-name>URIdecoderServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

The problem is, I cannot dispatch requests because of this! I hit the same URIdecoderServlet if I call getRequestDispatcher() for any JSP page. This results in a recursive call to URIdecoderServlet.

I read the Servlet Spec and it basically says that any /* mapping will take over extension "*.jsp" mapping... I can get the JSP dispatcher by name, but then how would I tell it which JSP to pull?

Thank You
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61224
    
  66

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


Never do this. By doing so you are telling the container to route all requests, including those for JSPs, images, scripts, style sheets and so on through your servlet.

Is that really what you want?

Conventially, you use a prefix (or suffix) to specify URLs (not URIs) that are to be mapped to your servlet. Something like:

<url-pattern>/whatever/*</url-pattern>


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Alexey Gor
Greenhorn

Joined: Jul 13, 2004
Posts: 27
i removed the * from uri-pattern to make my URI servlet the default servlet for my context. "*.JSP" mapping started to get picked up as a result, so RequestDispatcher started working.

Now any and all URIs for my context that do not get picked up by implicit mappings are forwarded to my servlet.

Can I include JSPs without keeping that implicit mapping? Can I have all URIs forward to my servlet while keeping ability to include JSPs?
Alexey Gor
Greenhorn

Joined: Jul 13, 2004
Posts: 27
I appreciate all the help... yes it would not be a big deal if somebody entered an address ending with .jsp and got a 404.. I am asking mostly for academic interest... I never had to get down and dirty with URIs before and clearly I never understood mapping requests to servlets...

It seems there must be a way to include JSPs without leaving a chance for a 404 error ever coming up. Yet that is percisely what would happen if the *.jsp mapping is used and the JSP is not there. Am I missing something? Thank You.
Alexey Gor
Greenhorn

Joined: Jul 13, 2004
Posts: 27
hmmmm more questions. I returned everything to original state and set my servlet to match "/test/*" URI pattern. My servlet outputs "HI," gets request dispatcher from and includes /jsp/default.jsp. There is a valid JSP page in /jsp/default.jsp.

Now strangest things are happening.

http://localhost:8080/myContext/jsp/default.jsp
Shows everything good. Pictures come up.

http://localhost:8080/myContext/test
(no slash)
Pictures come up but not layout is a little different.. maybe a CSS does not getr included somewhere.

http://localhost:8080/myContext/test/
(with a slash)
JSP does get included, HI gets printed before it. Pictures do not come up, CSS does not get picked up.

http://localhost:8080/myContext/test/anthing.at.all
JSP gets included, HI does NOT get printed. Pictures and CSS do not get picked up.

This is really strange. What is going on?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

It just sounds like you don't have your relative links for the images and CSS sheets right for the way you're calling your pages.

The easiest way to avoid this problem is to fully qualify your references to other resources from the context path.

Example:


or for containers that don't support JSP 2.0:


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

Joined: Jul 13, 2004
Posts: 27
Thanks for your help! Images work now.

As for the servlet mapping issue, I was thinking of using an extension filter instead, a'la Struts. I am currently looking at ways to employ URL rewriting to do this. If you have any comments - please share. Here is what I want to do:

1) Set up MyServlet for "*.ag" URLS

2) Intercept all incoming URLs.

3) Leave URL alone if it has an extension

4) Attach ".ag" to URL if it does not have an extension.

Result - all images and JSPs work, but all other URLs are forwarded to my servlet. Hopefully this will work.. I have not worked with URL rewriting yet, but I'm sure there is info out there. Thanks!
Alexey Gor
Greenhorn

Joined: Jul 13, 2004
Posts: 27
I wrote a nice long response to it, pressed the button, got error message, went back and poof! it's gone No biggie. I did not do the double-post on purpose. I understand that asking the same question twice does not increase chances of response As for the cross-post... i thought that Subject was not good. If I had the ability to delete that thread or edit the Subject... In either case, sorry about that.

We are building a bridge to serve content from CMS. Client wants to use URLs to identify content withing CMS.

http://localhost/alex/foo/bar

Means we should get content from CMS located in alex/foo/bar. This means all requests should go to 1 servlet that does this. I tried using "/*" mapping but it screwes everything up (evn including JSPs results in recursive call because it overrides "*.jsp").

I was thinking of using extension mappings a'la Struts .do, but they want to do "http://localhost/alex/foo/bar," not "http://localhost/alex/foo/bar.SOMETHING"

So I was thinking to intercept all incoming URLs and add ".SOMETHING" to it unless it has a 3 letter extension (to keep JSPs and images working). Seems like this is the only way to keep ability to include JSPs while forwarding all other requests to CentralServlet. I am probably overthinking this, so any comments are appreciated. Thank You!

(ctrl-a/ctrl-C before I click this time )
Alexey Gor
Greenhorn

Joined: Jul 13, 2004
Posts: 27
What about Filters, can a filter change URL and resubmit the request Tomcat?

thanks!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: problem with using URIs as parameters