Monday, January 31, 2011

Current page or resource in J2EE servlet application

Every so often I get the same question on my plate: How to find the current invoked resource or page in a J2EE servlet application. Hopefully the examples below go straight to what you need:
((HttpServletRequest)request).getRequestURL().toString()
http://sample.com/sample-app/css/main.css

((HttpServletRequest)request).getRequestURI()
/sample-app/css/main.css

((HttpServletRequest)request).getSession().getServletContext().getRealPath(((HttpServletRequest)request).getContextPath())
/Users/nestor/tomcat/webapps/sample-app/css/main.css

No comments:

Followers