服务方法中的request.getPathInfo()为什么返回null? [英] How come request.getPathInfo() in service method returns null?

查看:862
本文介绍了服务方法中的request.getPathInfo()为什么返回null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了Front Controller Pattern并进行了测试.当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() 总是在第一个电话之前完成 到service().

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

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

UDPATE

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

(HttpServletRequest-getPathInfo())

如果URL如下所示:

http://www.myserver.com/mycontext/myservlet/你好/测试?paramName =值.

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

(HttpServletRequest-getServletPath())

如果URL如下所示:

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

String servletPath = req.getServletPath();

它返回"/servlet/MyServlet"

此页也非常好: http://www.exampledepot.com/egs/javax.servlet/GetReqUrl.html

解决方案

@Vivien是正确的.您想使用 HttpServletRequest#getServletPath() 代替(对不起,我在编写 answer时忽略了这一点无疑是您正在阅读的,我已经更新了答案).

为了澄清:getPathInfo()不会包含web.xml中定义的servlet路径(仅此后的路径),并且getServletPath()基本上返回 only servlet路径如web.xml中所定义(因此,其后的路径也没有).如果网址格式包含通配符,则特别包含那个部分.

I wrote Front Controller Pattern and ran the test. Somehow request.getPathInfo() is returning null when it should return the path info.

1. HTML that calls servlet

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

2. Map the servlet in DD.
Anything that has .do extension (ex tmp.do) will invoke the servlet "Redirector"

<!-- 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. The servlet that takes request from *.do

 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

The problem is that request.getPathInfo() returns null. Based on the Head First book,

The servlet life cycle moves from "does not exist" state to "initialized" state (meaning ready to service client's request) beginning with its constructor. The init() always completes before the first call to service().

This tells me that somewhere between the constructor and init() method, the servlet is not-fully-grown servlet.

So, that means, by the time service() method is called, the servlet should be fully-grown servlet and request method should be able to call getPathInfo() and expect the valid value to return instead of null.

UDPATE

Very interesting. (http://forums.sun.com/thread.jspa?threadID=657991)

(HttpServletRequest - getPathInfo() )

If the URL is like below:

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

If you web.xml discribe the servlet pattern as /mycontext/* getPathInfo() will return myservlet/hello/test and getQueryString() will return paramName=value

(HttpServletRequest - getServletPath() )

If the URL is like below:

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

String servletPath = req.getServletPath();

It returns "/servlet/MyServlet"

This page is also very good one: http://www.exampledepot.com/egs/javax.servlet/GetReqUrl.html

解决方案

@Vivien is correct. You'd like to use HttpServletRequest#getServletPath() instead (sorry, I overlooked that bit while writing the answer which you undoubtely was reading, I've updated the answer).

To clarify: getPathInfo() does not include servlet path as definied in web.xml (only the path thereafter) and getServletPath() basically returns only the servlet path as definied in web.xml (and thus not the path thereafter). If the url pattern contains a wildcard, particularly that part is included.

这篇关于服务方法中的request.getPathInfo()为什么返回null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆