tags:

views:

24

answers:

3

now,i using

http://xxx.com/area.jsp?id=1

i want create a maping path

http://xxx.com/newyork

maping to /area.jsp?id=1

how to do is best?

BTW:i using Resin(java) + Nginx

A: 

In your database where you store these area IDs, add a column called "slug" and populate it with the names you want to use. The "slug" for id 1 would be "newyork". Now when a request comes in for one of these URLs, look up the row by "slug" instead of by id.

Dan Grossman
thanks,i only need maping /newyork to /area.jsp?id=1 ,only this one,no else,how to config this?
Zenofo
+1  A: 

This is my idea, create a filter in your web application , when u receive a request like "/area.jsp?id=1" , in doFilter method , forward the request to "http://xxx.com/newyork " . In web.xml

<filter>
    <filter-name>RedirectFilter</filter-name>
    <filter-class>
        com.filters.RedirectFilter
    </filter-class>
</filter>
<filter-mapping>
    <filter-name>RedirectFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

Write the following class and place it in WEB-INF/classses

class RedirectFilter implements Filter 
{
public void doFilter(ServletRequest request, 
                     ServletResponse response, 
                     FilterChain chain)
    throws IOException, ServletException
{


      String scheme = req.getScheme(); // http 
      String serverName = req.getServerName(); // xxx.com 
      int serverPort = req.getServerPort(); // 80 
      String contextPath = req.getContextPath(); // /mywebapp 
      String servletPath = req.getServletPath(); // /servlet/MyServlet 
      String pathInfo = req.getPathInfo(); // area.jsp?id=1 
      String queryString = req.getQueryString(); 
      if (pathInfo.IndexOf("area.jsp") > 1) 
      {
          pathInfo   = "/newyork"; 
          String url = scheme+"://"+serverName+contextPath+pathInfo; 
          filterConfig.getServletContext().getRequestDispatcher(login_page).
          forward(request, response);
     } else
    {
        chain.doFilter(request, response);
        return;
    }
}

}

Suresh S
how to config this filter ?in nginx.conf or web.xml? how to do?
Zenofo
+2  A: 

Use nginx's rewrite module to map that one URL to the area.jsp?id=1 URL

http://wiki.nginx.org/NginxHttpRewriteModule

Dan Grossman
this is the best answer.Even you can do using ModRewrite in Apache.
Suresh S