【发布时间】:2009-06-23 05:48:54
【问题描述】:
我有一个看起来像这样的 servlet:
public class ExampleServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().println(request.getPathInfo());
}
}
使用 web.xml 映射,例如:
<servlet>
<servlet-name>example</servlet-name>
<servlet-class>com.example.ExampleServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>example</servlet-name>
<url-pattern>/example/*</url-pattern>
</servlet-mapping>
它完全符合我的期望...如果我转到http://localhost:8080/example/foo,它会打印“/foo”。但是,如果我将 servlet 更改为转发到 JSP 文件:
public class ExampleServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// do something here to check the value of request.getPathInfo()
request.getRequestDispatcher("whatever.jsp").forward(request, response);
}
}
然后当我检查 getPathInfo() 的值时,它现在报告“whatever.jsp”而不是“foo”。
- 为什么在转发到 JSP 之前发生了变化?
- 如何检测用户正在查找的 URL?
编辑:以防万一,这在 Google App Engine 上。不过不应该这样。
【问题讨论】:
-
第二种情况,你是在转发请求之前还是之后检查getPathInfo?或者你在jsp文件中这样做?
-
我之前在做,我把评论放在那里。(//在这里做一些事情来检查request.getPathInfo()的值)