【问题标题】:How do you access second parameter of a servlet's web.xml file?如何访问 servlet 的 web.xml 文件的第二个参数?
【发布时间】:2011-02-02 15:09:46
【问题描述】:

在我的 web.xml 文件中,我定义了一个这样的 servlet:

<url-pattern>/MyURL/*</url-pattern>

如何访问在我的 servlet 中通过 * 传递的任何内容?我打算将此方案用于漂亮(-ish)的 URL。

【问题讨论】:

    标签: java google-app-engine jsp servlets friendly-url


    【解决方案1】:

    在HttpServlet 的doGet 或doPost 方法中,您可以使用HttpServletRequest 对象的getRequestURI 方法来检索URL 的路径部分。由于听起来您还想切断映射到 serlvet 的路径部分,因此可以使用 getServletPath 方法,然后执行以下操作:

    String path = request.getRequestURI();
    if(path.startsWith(request.getServletPath())) {
        path = path.substring(request.getServletPath().length());
    }
    

    【讨论】:

      【解决方案2】:

      HttpServletRequest#getPathInfo() 正是为此目的。

      String path = request.getPathInfo();
      

      就是这样。无需按照此处另一个答案中的建议从中提取 servlet 路径。另请参阅我在您的other question 上的回答。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-07-25
        • 1970-01-01
        • 1970-01-01
        • 2011-05-10
        • 2012-12-08
        • 1970-01-01
        • 2019-07-15
        • 1970-01-01
        相关资源
        最近更新 更多