View Full Version : Getting the request uri which the user has entered
kherpel
Feb 8th, 2006, 11:56 AM
Hi there,
i have a problem getting the request uri which the user has entered in the browser's address field. I only get the filename of the called jsp.
My bean definition file looks like this
<bean id="urlMapping" class="...SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/ek/hotline.htm">hotline</prop>
</props>
</property>
</bean>
<bean id="hotline" class="...HotlineForm">
<property name="formView" value="ek-hotline" />
</bean>
I didn't find a way to determine the uri "http://localhost/ek/hotline.htm" from the HotlineForm object. The attributes of the request are containing only the filename of the jsp file (ek-hotline.jsp) and the request uri to this file:
getRequestURL()=http://localhost/application/WEB-INF/jsp/ek-hotline.jsp
getRequestURI()=/application/WEB-INF/jsp/ek-hotline.jsp
getServletPath()=/WEB-INF/jsp/ek-hotline.jsp
Is there a way to determine the uri which the user has entered?
Thanks in advance,
Kristian
Kees de Kooter
Feb 8th, 2006, 12:16 PM
Use getQueryString():
"Returns the query string that is contained in the request URL after the path. This method returns null if the URL does not have a query string. Same as the value of the CGI variable QUERY_STRING."
kherpel
Feb 8th, 2006, 12:47 PM
No, getQueryString() doesn't work either. Sorry that I forgot it in the list but it just contains the GET parameters not the URI:
/application/hotline.htm
getQueryString() is null
/application/hotline.htm?id=1
getQueryString() is id=1.
But I can't get the part before the parameter list...
Thanks,
Kristian
cuong
Feb 8th, 2006, 01:19 PM
From my experience, this problem isn't restricted to Spring and is more to do with how the servlet container handles and stores request information during internal 'forwards' between URLs - e.g. for Struts this might be /action1.do -> /action2.do -> /action3.do -> /myPage.jsp.
At some points, I was never quite sure whether getRequestURI would return '/action1.do' or '/action2.do' etc. This frustration was compounded by the fact the behaviour is different on different servlet engines :(
IIRC the way to get round this is to use a ServletFilter that gets run before any forwarding. In the filter, the requestURI (and any other original data) is stored as a request attribute and this is used rather than relying on requestURI.
Hope that helps,
Cuong.
martinl
Feb 8th, 2006, 02:15 PM
I'm not sure if this is the "right" way to work around this, but I've created a HandlerInterceptor to do this:
public class AddGlobalObjectsToModelInterceptor implements HandlerInterceptor {
public void postHandle(HttpServletRequest req, HttpServletResponse res, Object handler, ModelAndView mav) {
if (mav != null) {
// Don't add objects to redirections, as they would then be a added to the query string of the redirect.
if (mav.getView() instanceof RedirectView || mav.getViewName().startsWith("redirect")) { return; }
// Add the "original" request's paths/addresses.
mav.addObject("servletPath", req.getServletPath());
mav.addObject("requestURI", req.getRequestURI());
mav.addObject("requestURL", req.getRequestURL());
}
}
// "Empty" method implementations to satisfy the interface.
public boolean preHandle(HttpServletRequest req, HttpServletResponse res, Object handler) { return true; }
public void afterCompletion(HttpServletRequest req, HttpServletResponse res, Object handler, Exception ex) {}
}
...which I then add to the HandlerMapping configurations where I need this:
<bean id="globalObjectsInterceptor" class="path.to.package.AddGlobalObjectsToModelInterceptor"/>
<bean id="mappingsWithGlobalObjects" class="org.springframework.web.servlet.handler.SimpleUrlH andlerMapping">
<property name="interceptors"><list><ref bean="globalObjectsInterceptor"/></list></property>
<property name="mappings">
<props>
<prop key="/whatever.html*">whateverController</prop>
...
</props>
</property>
</bean>
Then, if using JSP/JSTL/EL, I can get to the values using for example ${servletPath} in the .jsp file.
But what do you need this for? If it's for creating self-referencing URIs, usually it's easier just using "?" as the URI. For example a form posting to "itself":
<form method="post" action="?">
...
</form>
kherpel
Feb 8th, 2006, 03:46 PM
thanks for your help, i've added a ServletFilter as recommened by cuong and now everything works fine... :-)
@martinl: I needed the uri to generate a link to switch the language of the current page and redisplay the same information as before. The link is thereby created with a custom tag. But there was a problem on some pages if no script name was given (but I don't remember the problem right now :-))...
Thanks again,
Kristian
vBulletin® v3.7.3, Copyright ©2000-2008, Jelsoft Enterprises Ltd.