my dog learned polymorphism*
The moose likes JSP and the fly likes CMS - Approach to handle requests using directory names Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "CMS - Approach to handle requests using directory names" Watch "CMS - Approach to handle requests using directory names" New topic
Author

CMS - Approach to handle requests using directory names

Jeppe Sommer
Ranch Hand

Joined: Jan 07, 2004
Posts: 270
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

Joined: Jan 10, 2002
Posts: 60804
    
  65

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


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jeppe Sommer
Ranch Hand

Joined: Jan 07, 2004
Posts: 270
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

Joined: Jan 10, 2002
Posts: 60804
    
  65

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

Joined: Jan 07, 2004
Posts: 270
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

Joined: Jan 10, 2002
Posts: 60804
    
  65

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

Joined: Jan 07, 2004
Posts: 270
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

Joined: Oct 08, 2006
Posts: 2458
The front controller can also just ignore the request when the pathinfo is unknown.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: CMS - Approach to handle requests using directory names
 
Similar Threads
Chapter 6(Session Management) notes (HFSJ) for revision
FileUpload with Orion Package
File outside war
Oracle Chart (Graphs, Charts)
setting parameters in Portlet URL that other portlet can also retrieve?