WordPress-like Urlrewriting in JSP, Tomcat with Urlrewritefilter

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.

Read More
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?

Related posts

Leave a Reply

2 comments

  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()
        {
    
        }
    }