如何在JSP中获取由Servlet转发的请求URL?
如果我在JSP中运行以下代码,
System.out.println("servlet path= " + request.getServletPath());
System.out.println("request URL= " + request.getRequestURL());
System.out.println("request URI= " + request.getRequestURI());
Run Code Online (Sandbox Code Playgroud)
然后我得到JSP的服务器端路径.但是我想在浏览器的地址栏中看到URL.我可以在转发到JSP的Servlet中获取它,但我希望在JSP中获取它.
我正在使用OpenId4Java和JSF应用程序我已经在我的web.xml中添加了类似这样的安全约束.
<security-constraint>
<web-resource-collection>
<web-resource-name>Restricted</web-resource-name>
<url-pattern>/core/*</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
</web-resource-collection>
</security-constraint>
<login-config>
<auth-method>FORM</auth-method>
<form-login-config>
<form-login-page>/user/login.xhtml</form-login-page>
<form-error-page>/user/logout.xhtml</form-error-page>
</form-login-config>
</login-config>
Run Code Online (Sandbox Code Playgroud)
现在,当应用程序启动并且用户想要访问时
在web.xml的帮助下我现在可以向用户显示一个登录页面我在这里用openid登录如谷歌,雅虎现在我的问题是我怎么能告诉openid我的返回网址是
或者如果用户来自
此网址成功登录后,用户将转到此页面.
我有一个看起来像这样的servlet:
public class ExampleServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().println(request.getPathInfo());
}
}
Run Code Online (Sandbox Code Playgroud)
使用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>
Run Code Online (Sandbox Code Playgroud)
它给了我我期望的结果......如果我去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);
}
}
Run Code Online (Sandbox Code Playgroud)
然后当我检查getPathInfo()的值时,它现在报告"whatever.jsp"而不是"foo".
编辑:以防万一,这是在谷歌应用引擎上.不要以为应该这样.
我正在向Display.jspTrialShow.jsp页面发送请求,但每当我${pageContext.request.requestURL}在TrialShow JSP页面中调用时,我都会将其http://localhost:8081/newjsp1/TrialShow.jsp作为输出.如何http://localhost:8081/newjsp1/Display.jsp在TrialShow JSP页面中显示 ?