【问题标题】:How come request.getPathInfo() in service method returns null?service方法中的request.getPathInfo()怎么会返回null?
【发布时间】:2010-09-19 10:10:40
【问题描述】:

我编写了前端控制器模式并运行了测试。当它应该返回路径信息时,request.getPathInfo() 以某种方式返回 null。

1.调用 servlet 的 HTML

<a href="tmp.do">Test link to invoke cool servlet</a>

2。在 DD 中映射 servlet。
任何具有 .do 扩展名(例如 tmp.do)的东西都会调用 servlet“重定向器”

<!-- SERVLET (centralized entry point) -->
    <servlet>
        <servlet-name>RedirectHandler</servlet-name>
        <servlet-class>com.masatosan.redirector.Redirector</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>RedirectHandler</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

3.从 *.do 接收请求的 servlet

 public class Redirector extends HttpServlet {

        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            try {
                //test - THIS RETURNS NULL!!!!
                System.out.println(request.getPathInfo());

                Action action = ActionFactory.getAction(request); //return action object based on request URL path
                String view = action.execute(request, response); //action returns String (filename) 
                if(view.equals(request.getPathInfo().substring(1))) {
                    request.getRequestDispatcher("/WEB-INF/" + view + ".jsp").forward(request, response);
                }
                else {
                    response.sendRedirect(view);
                }
            }
            catch(Exception e) {
                throw new ServletException("Failed in service layer (ActionFactory)", e);
            }
        }
    }//end class

问题是 request.getPathInfo() 返回 null。基于 Head First 书,

servlet 生命周期从 "does not exist" 状态为 "initialized" 状态(表示准备就绪 服务客户的请求)开始 及其构造函数。初始化() 总是在第一次调用之前完成 到服务()。

这告诉我,在构造函数和 init() 方法之间的某个地方,servlet 不是完全成熟的 servlet。

因此,这意味着,在调用 service() 方法时,servlet 应该是完全成熟的 servlet,并且请求方法应该能够调用 getPathInfo() 并期望返回有效值而不是 null。

UDPATE

非常有趣。 (http://forums.sun.com/thread.jspa?threadID=657991)

(HttpServletRequest - getPathInfo())

如果网址如下:

http://www.myserver.com/mycontext/myservlet/hello/test?paramName=value.

如果 web.xml 将 servlet 模式描述为 /mycontext/* getPathInfo() 将返回 myservlet/hello/test 并且 getQueryString() 将返回 paramName=value

(HttpServletRequest - getServletPath())

如果网址如下:

http://hostname.com:80/mywebapp/servlet/MyServlet/a/b;c=123?d=789

String servletPath = req.getServletPath();

它返回“/servlet/MyServlet”

【问题讨论】:

    标签: jsp tomcat servlets service


    【解决方案1】:

    @Vivien 是正确的。您想改用HttpServletRequest#getServletPath()(抱歉,我在写answer 时忽略了这一点,您无疑正在阅读,我已经更新了答案)。

    澄清一下:getPathInfo()包含web.xml中定义的servlet路径(仅其后的路径)和getServletPath()基本上返回 servlet路径正如web.xml 中定义的那样(因此不是之后的路径)。如果 url 模式包含通配符,尤其是 that 部分。

    【讨论】:

    • 我应该看看文档,不知道 getServletPath() 存在!
    • Gottya 所以在 DD 中,如果我指定 /foo/*.do,getPathInfo() 只会在“.do”之后获取路径
      也就是说,/foo/test.do/blah?name=myname 然后它返回“/blah?name=myname”
    • /foo/*.do 不是有效的 url 模式,因此它无法返回任何内容 :) 即便如此,理论上它只会给出 /blah。它不包括查询字符串。为此,您有 getQueryString() 方法(或只是通常的 getParameter() 方法)。
    • 在使用 url 模式“/something/*”实现时遇到了同样的问题。感谢 masato-san 的链接,这为我节省了很多时间 exampledepot.com/egs/javax.servlet/GetReqUrl.html @BalusC:您可能希望将此链接添加到您的答案中......这是非常互补的
    【解决方案2】:

    根据Javadoc

    返回与客户端发出此请求时发送的 URL 关联的任何额外路径信息。额外的路径信息在 servlet 路径之后,但在查询字符串之前。如果没有额外的路径信息,此方法返回 null。

    使用前缀映射(在您的情况下为*.do)时,您没有任何路径信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-10
      • 2014-06-11
      • 1970-01-01
      • 2018-09-23
      • 2015-05-27
      相关资源
      最近更新 更多