转发到JSP时如何检测Java Servlet中的URL?
我有一个看起来像这样的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上。不要以为应该。