Freddy Daoud pisze:
>> Thanks for the reply. I did try that URL mapping in the first instance  
>> and it didn't seem to work - even though I had identical settings in  
>> web.xml. However, as I'm using Glassfish it seems that may be the  
>> reason! I don't have to use Glassfish but it seemed a nice contained  
>> EE server - as I do need JMS.
>>     
>
> For what it's worth, I usually use an index.jsp file as described
> in the book, or a simple index.html file with a redirect:
>
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
>   "http://www.w3.org/TR/html4/strict.dtd";>
> <html>
>   <head>
>     <meta http-equiv="refresh" content="0;url=login">
>   </head>
> </html>
>
> I know that using an index.{jsp,html} was not what you wanted, but
> it is simple and straightforward (and it works).

I noticed that index.jsp is an oftenly proposed solution, but I don't 
think it's good for Search Engine Optimization reasons. I believe 
rewriting URL is a better solution, as the user (and Google, etc.) will 
still see the original URL even though we serve them something different 
(for instance, when we match the URL to / we can serve what Stripes has 
bound to /home but the user still sees / in his address bar).

Here's the simple UrlRewritingFilter I've developed, it allows me to 
bypass most of the problems (such as Glassfish going to // instead of /, 
the need for normal 404s instead of going to /, etc.) AND to control URL 
mapping using a configuration table, EJB method, or whatever. Feel free 
to change it (there's a logic for matching appropriate CMS nodes in my 
code) and use it.

It needs to be configured in web.xml like that (put it BEFORE Stripes 
filter):

    <filter>
        <filter-name>UrlRewritingFilter</filter-name>
        
<filter-class>pl.krop.web.servlet.filter.UrlRewritingFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>UrlRewritingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>




package pl.krop.web.servlet.filter;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import pl.krop.nawigacja.ejb.FrontFacadeLocal;

/**
 *
 * @author Grzegorz Krugły
 */
public class UrlRewritingFilter implements Filter {
    private FilterConfig filterConfig;

    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
    }

    public void doFilter(ServletRequest request, ServletResponse 
response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;

        // since all requests are going through this filter,
        // it's not a bad place to fix the XX-century assumption
        // that Glassfish makes about request encoding (it always assumes
        // it's iso-8859-1 but we use UTF-8 everywhere)
        // thanks to this line, POSTed form data doesn't break 
diacritical characters
        req.setCharacterEncoding("UTF-8");

        String rewrittenUrl = null;

        // TODO GK check URL mapping in the database table and redirect 
if needed

        Long nodeId = 
lookupFrontFacadeBean().getNodeId(req.getRequestURI());

        if ("/".equals(req.getRequestURI()) || 
"//".equals(req.getRequestURI())) {
            // if root is requested - even as // which is Glassfish bug 
- view the home page
            nodeId = lookupFrontFacadeBean().getNodeId("/");
            rewrittenUrl = String.format("/node/%d", nodeId);
        } else if (nodeId != null) {
            // if there's a node with URL matching to the current one, 
view it
            rewrittenUrl = String.format("/node/%d", nodeId);
        }

        if (rewrittenUrl == null) {
            chain.doFilter(request, response);
        } else {
            
filterConfig.getServletContext().getRequestDispatcher(rewrittenUrl).forward(request,
 
response);
            return;
        }
    }

    public void destroy() {
        this.filterConfig = null;
    }

    private FrontFacadeLocal lookupFrontFacadeBean() {
        try {
            Context c = new InitialContext();
            return (FrontFacadeLocal) 
c.lookup("java:comp/env/FrontFacadeBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, 
"exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

}



HTH,
Grzegorz


------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay 
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Stripes-users mailing list
Stripes-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/stripes-users

Reply via email to