search engine friendly URLs in JSP

September 21st, 2007
This article describes how to achieve search engine friendly URLs in a JSP-based website. I don't know if what is described here is the best you can do, because I couldn't find much information on the web, and this is what I personally came up with.

Let's say that you have a servlet that requests an article from a database and displays it. A classic URL pattern for this purpopse could be something like:

http://www.mywebsite.com/article.jsp?id=1234

This works but it is not what people call a search engine friendly or search engine optimized URL. Most search engine bots usually ignore pages which contain the '?' character in the URL. A search engine rather likes URLs of the form:

http://www.mywebsite.com/article.jsp/1234

or even better:

http://www.mywebsite.com/article.jsp/title-of-my-article

We will now try to improve the article servlet, so that it supports these friendly URLs.

First, you have to tell Tomcat that your servlet should be also accessed by the /article.jsp/* URL pattern. Go in your web.xml file and find the <servlet-mapping> tag for your servlet. It should look like this:

<servlet-mapping>
<servlet-name>ArticleServlet</servlet-name>
<url-pattern>/article.jsp</url-pattern>
</servlet-mapping>


Add one more tag that will match the new URL pattern, like this:

<servlet-mapping>
<servlet-name>ArticleServlet</servlet-name>
<url-pattern>/article.jsp/*</url-pattern>
</servlet-mapping>


Then, you should add some code in your servlet's doGet method to allow it to know whether the servlet was invoked using a friendly URL:

import java.util.regex.Matcher;
import java.util.regex.Pattern;
String servletPath = request.getServletPath();
String requestURI = request.getRequestURI();
Pattern pattern = Pattern.compile(".*"+servletPath+"/(.*)");
Matcher matcher = pattern.matcher(requestURI);
if (matcher.matches())
{
String param = matcher.group(1);
// do stuff with param here..
}


This way param contains the portion of the URL that follows the /article.jsp/ and you can do whatever you want with that.

However, there is one more thing you have to take care of. By introducing additional slashes in the URL you influence the way your browser determines the URLs for relative links and references. For example, if you have a css stylesheet reference in your page, like this:

<link href="style.css" rel="stylesheet" type="text/css"/>

it may not be found when you use friendly URLs. This happens because the current page's URL may end with /article.jsp/my-title, and the browser will look for the style.css file at /article.jsp/style.css, which does not exist.

To overcome this problem you have two choices. One, you can switch all your links from relative to absolute. But that's not a wise thing to do. Relative links are good, for several reasons. The other thing to do, is to use the <base> tag:

<head>
<base href="http://www.mywebsite.com/"/>
...
</head>


You set the base URL of your website and all your relative paths will be searched from there. If you do not want to hard code any part of the URL in your page, you can use this code inside your JSP page:

<%
String baseUrl = request.getRequestURL().
substring(0,request.getRequestURL().
indexOf(request.getServletPath()))+"/";
%>
<base href="<%=baseUrl%>"/>


I hope this helps.

One Response to “search engine friendly URLs in JSP”

  1. Andreas Says:
    Hi i read your article and i try it but i have problem. please can you send me a demo of that source or i download it.

Leave a Reply