开发者

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

开发者 https://www.devze.com 2023-01-21 20:15 出处:网络
I\'m using http://example.com/area.jsp?id=1 and want create a mapping path http://examp开发者_运维知识库le.com/newyork

I'm using

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

and want create a mapping path

http://examp开发者_运维知识库le.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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号