aspose file tools*
The moose likes Servlets and the fly likes Servlet URL parameters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet URL parameters" Watch "Servlet URL parameters" New topic
Author

Servlet URL parameters

Bart Stanz
Greenhorn

Joined: Feb 06, 2010
Posts: 13
Hi, you know on digg.com when you click on an article "This is an article" it links to a url in the form digg.com/this_is_an_article. Is this possible to do with Servlets? Seems like the parameter is being passed as part of the url but not using the typical ?story=this_is_an_article. I tried google but not sure what it's called.

Thanks.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61770
    
  67

Please be sure to take the time to compose descriptive subjects for your posts; read this for more information.

Using a title of "Servlet question" in a forum completely dedicated to questions on Servlets isn't very helpful. What everyone named their post "Servlet question"?

Please go back and change your post to add a more meaningful subject by clicking the

Also, please read this with regards to using the term "urgent".


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61770
    
  67

Bart Stanz wrote:Is this possible to do with Servlets?

Of course. There are methods on the HttpServletRequest instance to obtain every part of the URL.

Be sure that your servlet mapping is correctly mapped to the prefix you want to use.
Tim McGuire
Ranch Hand

Joined: Apr 30, 2003
Posts: 820

Bart Stanz wrote:Hi, you know on digg.com when you click on an article "This is an article" it links to a url in the form digg.com/this_is_an_article. Is this possible to do with Servlets? Seems like the parameter is being passed as part of the url but not using the typical ?story=this_is_an_article. I tried google but not sure what it's called.

Thanks.



This is called URL rewriting or friendly URLs. In servlet containers like tomcat it can be done with UrlRewriteFilter.
check out http://www.theserverside.com/blogs/thread.tss?thread_id=47588

If you are using a web framework like struts 2, it should let you swap out the ActionMapper for one that uses friendly URLs. If you are doing straight servlets, you will have to do it yourself with URLRewriteFilter.
Bart Stanz
Greenhorn

Joined: Feb 06, 2010
Posts: 13
Thanks Bear, I fixed the ambiguity in the title.

Tim: Thanks, I think this is what I am looking for. Cheers!

-Bart
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61770
    
  67

Note that re-writing is not necessary. The servlets can respond directly if mapped and written correctly to accept REST-style URLs.

Re-writing is only necessary to retro-fit friendly URLs into "non-friendly" URLs when necessary.
Bart Stanz
Greenhorn

Joined: Feb 06, 2010
Posts: 13
Bear: you're right, in my case I didn't really need to use rewriting. It's actually very simple (excuse my newbiness).

In my case this worked out fine:

I made a servlet mapping with url-patter like this:
<url-pattern>/article/*</url-pattern>

and then in the servlet I put this code:

String servletPath = request.getPathInfo();

System.out.println(servletPath);

So if the URL was www.site.com/article/this_is_some_article, request.getPathInfo(); would return /this_is_some_article.

Thanks guys,

Bart
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61770
    
  67

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet URL parameters