Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Permalinks with JSP

 
Peter Bergoff
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

I'm wondering how I can do wordpress type permalinks in Java (or specifically JSP). For example:
http://www.homes.com/styles.jsp?type=brick
to
http://www.homes.com/brick
or
http://www.homes.com/styles/brick (where styles signals which JSP needs to be called)
or something like that to avoid the parameters.

Ideally this would be dynamic so I wouldn't have to set up individual permalinks for each page.

Thanks!

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65118
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure, you'd just add the proper mappings to the deployment descriptor.

But... a servlet would be a better target; JSPs are rarely the target of a URL -- the servlet controller should handle any processing before forwarding to a JSP to render the view.
 
Peter Bergoff
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bear! I am familiar with mapping to shorten and change servlet names. Is there a way to take this a step further and use the mapping to avoid placing parameters in the URL when calling that servlet?
Something like:
http://www.homes.com/styles-brick
instead of
http://www.homes.com/styles?type=brick

Basically I want to make my URLs pretty without having to create a new servlet for each page. I suppose in theory I could create a unique servlet for each variation but it would be nice to avoid that. Is there a standard for this type of thing?

Thanks!
 
Paul Clapham
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know about a "standard", but it's more traditional to do http://www.homes.com/styles/brick than http://www.homes.com/styles-brick. In other words, if there's a hierarchy then make it look like one, with the slashes separating level names.

And sure, there isn't anything stopping you from mapping a variety of URLs to a single servlet.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65118
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Investigate the methods of HttpServletRequest -- what you seek is there.

(Hint: the magic word is path.)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic