简体   繁体   English

将/ newyork映射到/area.jsp的最佳方法是什么?id = 1?

[英]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 映射到/area.jsp?id=1

How do I do this best? 我如何做到最好?

Note: I'm using Resin(java) + Nginx 注意:我正在使用Resin(java)+ Nginx

Use nginx's rewrite module to map that one URL to the area.jsp?id=1 URL 使用Nginx的重写模块将一个URL映射到area.jsp?id = 1 URL

http://wiki.nginx.org/NginxHttpRewriteModule 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 . 这是我的想法,在您的Web应用程序中创建一个过滤器,当您在doFilter方法中收到类似/area.jsp?id=1的请求时,将该请求转发到http://example.com/newyork

In web.xml : 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 : 编写以下类并将其放在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. 在存储这些区域ID的数据库中,添加一个名为“ slug”的列,并在其中填充要使用的名称。 The "slug" for id 1 would be "newyork". ID为1的“子弹”为“ newyork”。 Now when a request comes in for one of these URLs, look up the row by "slug" instead of by id. 现在,当对这些URL之一的请求进入时,请按“ slug”而不是id查找行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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