problem with RequestDispatcher forwarding to a jsp located one level down

Posted on 2011-04-25
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/, 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?
Question by:Emilie
    LVL 20

    Expert Comment

    by:Sathish David Kumar N
    can you check or paste the code which your forward back to index.jsp page

    Author Comment

    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

    LVL 92

    Expert Comment

    use absolute paths instead of relative ones
    LVL 20

    Expert Comment

    by:Sathish David Kumar N

    Author Comment

    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/  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
    LVL 92

    Expert Comment

    > 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) :)

    Author Comment

    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/">link</a>

    Which works fine when I load it from

    But when it's a call to 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  Doesn't seem very absolute to me..
    LVL 92

    Accepted Solution

    Cause thats a relative url. What I meant was if you're always writing things as relative to the base url then you might as well add the leading slash.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Suggested Solutions

    Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
    Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
    The viewer will learn how to implement Singleton Design Pattern in Java.
    This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now