Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

CMS - Approach to handle requests using directory names

 
Jeppe Sommer
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

In a content management system pages are often requested by using URL directory names (path) instead of using an unique parameter id, when getting content stored in a database, i.e:

Solution 1: URL request using directory name:
www.myDomain.com/about/
www.myDomain.com/about/thisDepartment/

Solution 2: URL request using unique parameter id:
www.myDomain.com?page.jsp?pid=233

I am looking for an approach on how to use the directory name (path) as the unique id, like solution 1.

I am wondering about if there is any good practice on how to handle URL request using directory names? I guess we will need some main class or jsp page handling the request, getting the requested URI path and the getting the content related to the unique directory name '/about/thisDepartment/'.

Can anyone help me with some ideas? Do we use servlet listener, a 404 page, a basic RequestDispatcher to handle this?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64620
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you are inquiring about are called "friendly URLs". A Front Controller that fields the requests for parsing is a good way to approach this.

The value of the URL after the servlet path is available as the "path info".
 
Jeppe Sommer
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great, now I got a better chance to google it on the internet. Thanks.

I have some other questions regarding this approach:

I guess the front controller is a basic servlet. The front controller gets the path info from the http request, and use a RequestDispatcher object that forward to the jsp page using the path info as parameter?

If this is how it could be done, I guess we are using the path info as the unique id to get content from the database and not a unique record parameter like 'pid=233'

I also guess that the front controller is handling every single http request. How do we avoid it to run in an infinite loop if it´s called for each request, like?

<servlet-mapping>
<servlet-name>FrontController</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64620
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, that's pretty much it. Except never map /* to anything!

Use a mapping like /xyz/* to address the front controller where 'xyz' is something that has meaning to your application.
 
Jeppe Sommer
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Yeah, that's pretty much it. Except never map /* to anything!

Use a mapping like /xyz/* to address the front controller where 'xyz' is something that has meaning to your application.


Okay, but this way we always have to predefine the 'first path info name' in the web.xml, right?

Let´s say we have three main subjects in our website:

1) www.myDomain.com/about/
2) www.myDomain.com/product/
3) www.myDomain.com/contact/

( I call the path /about/ = 'first path info name' )

Then we will need to predefine these three path info names in our web.xml like:

<servlet-mapping>
<servlet-name>RequestDispather</servlet-name>
<url-pattern>/about/*</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>RequestDispather</servlet-name>
<url-pattern>/product/*</url-pattern>
</servlet-mapping>
...

What if the these path info names are not predefined, but created by the webmaster from an user interface and he hasn´t access to the web.xml?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64620
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create another prefix that always directs to your front controller. You just can't map /* because that will field all requests for everything! Including images, stylesheets, JavaScript files, etc.
 
Jeppe Sommer
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Create another prefix that always directs to your front controller. You just can't map /* because that will field all requests for everything! Including images, stylesheets, JavaScript files, etc.


Okay, maybe I´m 'dull-witted' :-), but if I create a prefix called 'myPrefix', like:


- well then all links on the website has to point to this prefix, like:

www.myDomain.com/myPrefix/about/
www.myDomain.com/myPrefix/product/
www.myDomain.com/myPrefix/contact/

- or am I wrong or how can we avoid this?
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The front controller can also just ignore the request when the pathinfo is unknown.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic