PDA

View Full Version : URL from Servlet


mitalub
Jun 10th, 2008, 09:33 AM
From a jsp, how can I access the URL used to request the page?

For example, assume the user would browse to "http://localhost:8080/servletName/app/viewName"
This is mapped to WEB-INF/jsp/viewNameView.jsp

From within the jsp, I need to get "viewName" (or "/servletName/app/viewName" would do).

Unfortunately ${pageContext.request.requestURL} returns the path to the jsp: "http://localhost:8080/servletName/WEB-INF/jsp/viewNameView.jsp" which is not what I want. I've tried a bunch of other implicint variables to no avail as well.

I could set a model parameter from my Java controller code… something like map.put("url", request.getPathInfo()) but this seems ugly. Request.pathInfo doesn't return anything in the jsp.

Thanks in advance for your help!

jfredrickson
Jun 11th, 2008, 12:12 AM
I don't know whether or not the view name is available to the JSP as an implicit variable anywhere, but your alternate solution (having the controller add the URL/path to the model) seems sensible enough from a MVC perspective. The URL/path information is simply another object to be passed to the view for rendering.

Sorry that I wasn't able to exactly answer your question, but my two cents is that it's OK to use the model for this.