Jsp 为什么服务方法中的request.getPathInfo()返回null?

Jsp 为什么服务方法中的request.getPathInfo()返回null?,jsp,tomcat,servlets,service,Jsp,Tomcat,Servlets,Service,我编写了前端控制器模式并运行了测试。不知何故,request.getPathInfo()在返回路径信息时返回null 1。调用servlet的HTML <a href="tmp.do">Test link to invoke cool servlet</a> 问题是request.getPathInfo()返回null。根据头一本书 servlet生命周期从 “不存在”状态为 “已初始化”状态(表示就绪 服务客户的请求)开始 用它的构造函数。init() 始终在第一次调

我编写了前端控制器模式并运行了测试。不知何故,request.getPathInfo()在返回路径信息时返回null

1。调用servlet的HTML

<a href="tmp.do">Test link to invoke cool servlet</a>
问题是request.getPathInfo()返回null。根据头一本书

servlet生命周期从
“不存在”
状态为
“已初始化”
状态(表示就绪 服务客户的请求)开始 用它的构造函数。init() 始终在第一次调用之前完成 服务()

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

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

乌德帕特 非常有趣。()

(HttpServletRequest-getPathInfo())

如果URL如下所示:

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

(HttpServletRequest-getServletPath())

如果URL如下所示:

字符串servletPath=req.getServletPath()

它根据以下命令返回“/servlet/MyServlet”

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


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

@Vivien是正确的。您希望改用(抱歉,我在写您无疑正在阅读的文章时忽略了这一点,我已经更新了答案)


为了澄清:
getPathInfo()
不包含
web.xml
中定义的servlet路径(仅包含其后的路径),而
getServletPath()
基本上只返回
web.xml
中定义的servlet路径(因此不返回其后的路径)。如果url模式包含通配符,尤其是包含该部分。

我应该查看文档,但不知道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的链接,这为我节省了很多时间@BalusC:你可能想添加这个链接,你的答案……这是非常互补的
<!-- 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>
 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