The moose likes Servlets and the fly likes Servlet URL parameters Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Servlets
Reply 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.

This message was edited 2 times. Last update was at by Bart Stanz

Bear Bibeault
Author and opinionated walrus
Marshal

Joined: Jan 10, 2002
Posts: 48842

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

This message was edited 1 time. Last update was at by Bear Bibeault



[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Bear Bibeault
Author and opinionated walrus
Marshal

Joined: Jan 10, 2002
Posts: 48842

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

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 opinionated walrus
Marshal

Joined: Jan 10, 2002
Posts: 48842

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.

This message was edited 2 times. Last update was at by Bear Bibeault

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

This message was edited 1 time. Last update was at by Bart Stanz

Bear Bibeault
Author and opinionated walrus
Marshal

Joined: Jan 10, 2002
Posts: 48842

 
 
subject: Servlet URL parameters
 
developer file tools

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture