简体   繁体   中英

What's the best way to mapping /newyork to /area.jsp?id=1?

I'm using

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

and want create a mapping path

http://example.com/newyork

mapping to /area.jsp?id=1

How do I do this best?

Note: I'm using Resin(java) + Nginx

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

http://wiki.nginx.org/NginxHttpRewriteModule

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://example.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(); // example.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;
     }
}
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM