• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem with using URIs as parameters

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

<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>

 
Alexey Gor
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Alexey Gor
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What about Filters, can a filter change URL and resubmit the request Tomcat?

thanks!
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic