Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet URL parameters

 
Bart Stanz
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64698
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64698
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64698
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64698
86
IntelliJ IDE Java jQuery Mac Mac OS X
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic