jsp - 服务方法中的 request.getPathInfo() 怎么会返回 null?

标签 jsp tomcat servlets service

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

<强>1。调用servlet的HTML

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

<强>2。在 DD 中映射 servlet。
任何具有 .do 扩展名(ex tmp.do)的东西都会调用 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。从 *.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。基于《深入浅出》一书,

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().

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

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

更新

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

(HttpServletRequest - getPathInfo())

如果 URL 如下所示:

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

如果您的 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”

最佳答案

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

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

关于jsp - 服务方法中的 request.getPathInfo() 怎么会返回 null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3745275/

相关文章:

java - 如何在Eclipse中调试动态网页?

Java 网络应用程序 : Global application scope object

成功登录复杂化后的 Tomcat JDBCRealm 重定向

java - 为什么我无法从浏览器中看到我的 js 文件 - tomcat 7

java - jsp页面中的Applet(Eclipse)

java - 如何启用 java HotSpot VM 编译器

tomcat - JAX-RS/Jersey + Tomcat ServletContext 为空

java - 如何从 HttpServletRequest 获取客户端在 java 中的时区?

java - Tomcat 5.5 找不到我的 Servlet

javascript - 如何划分表格以分页显示?表格数据用jsp动态填充