本文介绍了在转发到JSP时,如何检测Java Servlet中的URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的servlet:

I have a servlet that looks something like this:

public class ExampleServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.getWriter().println(request.getPathInfo());
    }
}

带有web.xml映射,如:

with a web.xml mapping like:

<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>

它给了我我期望的......如果我去它打印/ foo。但是,如果我将servlet更改为转发到JSP文件:

and it gives me exactly what I expect... If I go to http://localhost:8080/example/foo it prints "/foo". However, if I change the servlet to forward to a JSP file:

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。

then when I check the value of getPathInfo() it now reports "whatever.jsp" instead of "foo".


  1. 为什么在转发到JSP之前这已经改变了?

  2. 如何检测用户正在寻找的网址?

编辑:万一重要,这是在Google App Engine上。不要认为应该这样。

Just in case it matters this is on Google App Engine. Don't think it should though.

推荐答案

这个问题模糊不清(servlet是否会再次呼唤每个前锋? ),但听起来很像你需要 request.getAttribute(javax.servlet.forward.request_uri)

The question is vague and ambiguous (is the servlet calling itself on every forward again?), but it much sounds like that you need request.getAttribute("javax.servlet.forward.request_uri").

这篇关于在转发到JSP时,如何检测Java Servlet中的URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 20:23