【问题标题】:What's the best way to mapping /newyork to /area.jsp?id=1?将 /newyork 映射到 /area.jsp?id=1 的最佳方式是什么?
【发布时间】:2018-05-16 11:12:48
【问题描述】:

我正在使用

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

并想创建一个映射路径

http://example.com/newyork

映射到/area.jsp?id=1

我怎样才能做到最好?

注意:我使用的是 Resin(java) + Nginx

【问题讨论】:

    标签: java nginx


    【解决方案1】:

    使用 nginx 的重写模块将该 URL 映射到 area.jsp?id=1 URL

    http://wiki.nginx.org/NginxHttpRewriteModule

    【讨论】:

    • 这是最好的答案。即使您可以在 Apache 中使用 ModRewrite。
    【解决方案2】:

    这是我的想法,当您收到类似的请求时,在您的 Web 应用程序中创建一个过滤器 /area.jsp?id=1,在doFilter方法中,将请求转发给http://example.com/newyork

    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>
    

    编写下面的类,放在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;
         }
    }
    }
    

    【讨论】:

    • 如何在 nginx.conf 或 web.xml 中配置这个过滤器?怎么办?
    【解决方案3】:

    在您存储这些区域 ID 的数据库中,添加一个名为“slug”的列并用您要使用的名称填充它。 id 1 的“slug”将是“newyork”。现在,当对这些 URL 之一的请求进入时,通过“slug”而不是 id 来查找行。

    【讨论】:

    • 谢谢,我只需要映射 /newyork 到 /area.jsp?id=1 ,只有这个,没有其他,如何配置?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-15
    • 2016-02-06
    • 2012-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多