• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 411
  • Last Modified:

problem with RequestDispatcher forwarding to a jsp located one level down

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?
0
Emilie
Asked:
Emilie
  • 3
  • 3
  • 2
1 Solution
 
Sathish David Kumar NCommented:
can you check or paste the code which your forward back to index.jsp page
0
 
EmilieAuthor Commented:
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:
<servlet-mapping>
        <servlet-name>MyServlet</servlet-name>
        <url-pattern>/servlet/*</url-pattern>
    </servlet-mapping>
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");
        cda.setMyEJB(myEJB);
        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

0
 
objectsCommented:
use absolute paths instead of relative ones
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Sathish David Kumar NCommented:
getServletContext().getRealPath()
0
 
EmilieAuthor Commented:
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/.
0
 
objectsCommented:
> 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) :)
0
 
EmilieAuthor Commented:
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..
0
 
objectsCommented:
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.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now