4

I am developing a simple cms for an online health magazine using JSP,Tomcat and urlrewritefilter for url rewriting. I am migrating content from wordpress and should keep the permalinks on the site. Permalinks looks like below with only letters and numbers.

http://www.example.com/post-or-category-name-with-letters-or-1234/

I want to rewrite my url in my jsp application so that I can have urls like above. Rewrite Rule should work as follows.

http://www.example.com/post/?pid=1234&name=post-name
http://www.example.com/category/?cid=1234&slug=category-slug

into

http://www.example.com/post-name/
http://www.example.com/category-slug/

And of course vice versa.

How can I have a wordpress-like permalink structure using urlrewritefilter? Do I need to write a Servlet for getting the id of name or slug from DB?

Anybody has an idea how to do that or done it before?

Mayur Birari
  • 5,837
  • 8
  • 34
  • 61
mutoprak
  • 43
  • 6

2 Answers2

1

I've already done a JavaServer Faces CMS with custom URL for posts and categories. I've used basically javax.servlet.Filter and javax.faces.application.ViewHandler. Since you are in straight JSP you won't need javax.faces.application.ViewHandler.

How I declared my filter :

<filter>
    <filter-name>URLFilter</filter-name>
    <filter-class>com.spectotechnologies.jsf.filters.URLFilter</filter-class>
    <async-supported>true</async-supported>
</filter>

<filter-mapping>
    <filter-name>URLFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>INCLUDE</dispatcher>
    <dispatcher>ERROR</dispatcher>
</filter-mapping>

Basic Filter implementation :

/**
 *
 * @author Alexandre Lavoie
 */
public class URLFilter implements Filter
{
    @Override
    public void doFilter(ServletRequest p_oRequest, ServletResponse p_oResponse, FilterChain p_oChain) throws IOException, ServletException
    {
        // Determining new url, get parameters, etc
        p_oRequest.getRequestDispatcher("newurl").forward(p_oRequest,p_oResponse);
    }

    @Override
    public void init(FilterConfig p_oConfiguration) throws ServletException
    {

    }

    @Override
    public void destroy()
    {

    }
}
Alexandre Lavoie
  • 8,711
  • 3
  • 31
  • 72
  • I have implemented something similar, however I used tomcat's error page forwarding for 404 code(e.g error.jsp) and made DBWorker calls, got the id and loaded the actual page content in this error.jsp. I will try your implementation also. – mutoprak Dec 04 '12 at 20:34
0

I think your answer lies into : How to rewrite URL in Tomcat 6

Is there a url rewriting engine for Tomcat/Java?

Community
  • 1
  • 1
Amber
  • 1,229
  • 9
  • 29
  • I have already checked those before. I am trying the examples section in tuckey's urlrewritefilter however couldn't come up with a solution using only tomcat + urlrewritefilter. – mutoprak Dec 04 '12 at 12:44