We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now


problem with RequestDispatcher forwarding to a jsp located one level down

Emilie asked
Medium Priority
Last Modified: 2012-05-11
I am trying to have a servlet forward the request back to my index page if the user identity isn't validated.  Problem is that this servlet has a url mapping (aka applicationRoot/servlet/doSomething.do), and when it forwards the request back to my index page, all my links, images, etc, which are relative to the root of the application (aka applicationRoot/index.jsp) fail to be retrieved because it is loading as applicationRoot/servlet/index.jsp.  

Any solution?
Watch Question


can you check or paste the code which your forward back to index.jsp page


Included are the basic methods found in my servlet.  Each action has a success page and a fail page defined.  If no error occurs, next is assigned the success page by the action page, otherwise, it's assigned the fail page (in this case "/index.jsp").  

This servlet url mapping is as follows:
public void init() throws ServletException {
actions = new HashMap<String, Action>();

        // map the request to the correct action and define the default fail and success foward pages
        // associate the action to its ejb
        MyAction cda = new MyAction("/dbConfig.jsp", "/index.jsp");
        actions.put("configure", cda);

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String next = "";
        String op = getOperation(request.getRequestURL());
        RequestDispatcher dispatcher;

        // get the action associated to this request and process it
        Action act = actions.get(op);
        if (act != null) {
            next = act.perform(request);

        // Get the dispatcher
        dispatcher = getServletContext().getRequestDispatcher(next);

if (dispatcher != null) {
            dispatcher.forward(request, response);

public String getOperation(StringBuffer requestURL) {
        int lastslash = requestURL.lastIndexOf("/");
        int lastdot = requestURL.lastIndexOf(".");
        String op = requestURL.substring(lastslash + 1, lastdot);
        return op;

Open in new window

Mick BarryJava Developer
Top Expert 2010

use absolute paths instead of relative ones



I don't want to use absolute paths since there's a possibility that this web application could be moved to another URL at some point.  

Getting the real path does not appear to do anything helpful since the real path does not provide a url, but rather the file's actual location on the server.

Basically, the problem appears to be that though the resquestURI in the dispatcher is correct, the page still gets loaded with a mapped url of /servlet/something.do.  The page index.jsp is found, but all paths inside the page are incorrect.  

objects, is there a way to force a path in a jsp page (ie <img src="myAbsolutePath" /> to always be relative to the base url without actually having to include the base url?  I.E. for the example above, this would always yield http://www.mybaseurl.com/myAbsolutePath/.
Mick BarryJava Developer
Top Expert 2010

> to always be relative to the base url without actually having to include the base url?

what you are defining is an absolute url (without the leading slash) :)


Sorry, maybe I'm slow, but then what is wrong with this:

In my index.jsp file, I have, for instance, a reference to
<a href="myServlet/action.do">link</a>

Which works fine when I load it from www.baseurl.com/index.jsp.

But when it's a call to www.baseurl.com/myServlet/action.do that tries to forward back to index.jsp in some particular cases, depending on the result of the logic that was performed, I get index.jsp loaded back but with broken url and img src.  For instance, the example above will then try to load www.baseurl.com/myServlet/myServlet/action.do.  Doesn't seem very absolute to me..
Java Developer
Top Expert 2010
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.