File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes RequestDispatcher.forward problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "RequestDispatcher.forward problem" Watch "RequestDispatcher.forward problem" New topic
Author

RequestDispatcher.forward problem

Matthew Phillips
Ranch Hand

Joined: Mar 09, 2001
Posts: 2676
I have an application that forwards to a page with frames. I am forwarding to a relative url in a sub-folder of the web app. It is able to find the relative url, but it does not find the frames. I get a 404 error in each frame telling me that it did not look in the same folder that the main page is in. Does anyone have any ideas on how to resolve this?


Matthew Phillips
John Fontana
Ranch Hand

Joined: Feb 28, 2002
Posts: 235
Originally posted by Matthew Phillips:
I have an application that forwards to a page with frames. I am forwarding to a relative url in a sub-folder of the web app. It is able to find the relative url, but it does not find the frames. I get a 404 error in each frame telling me that it did not look in the same folder that the main page is in. Does anyone have any ideas on how to resolve this?

The most "modular" way to do it is by mapping the servlet to a URL in the same subdirectory of your frameset. So if the frameset is in some-sub-directory, add a mapping to web.xml:
<servlet>
<servlet-name>test-me</servlet-name>
<servlet-class>com.testme</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>test-me</servlet-name>
<url-pattern>/some-sub-directory/testme</url-pattern>
</servlet-mapping>
Then when you call the servlet with http-app-path/some-sub-directory/testme, the relative paths will work.


www.websiteandsound.com
"If you do what you've always done, you'll get what you've always gotten."
Matthew Phillips
Ranch Hand

Joined: Mar 09, 2001
Posts: 2676
Can a servlet have multiple url mappings? The servlet could return 1 of three screens based on input data. The framesets are in 3 seperate folders. I also already have a url map so that I do not have to call the servlet by /servlet/servlet-name.
Yuriy Fuksenko
Ranch Hand

Joined: Feb 02, 2001
Posts: 413
For this forward (I mean since it forward), the url for your frames should be relative to your original location.
If user tryed to access folder1\file1.html,
but you forward him to ..\folder2\file2.html, and file2.html contains some frames, request for this frames will be sent by a browser, and for browser you are still at folder1\file1.html. So if you frame url was frame2.html and frame2.html locate at folder2, frame url should be ..\folrder2\frame2.html
John Fontana
Ranch Hand

Joined: Feb 28, 2002
Posts: 235
Originally posted by Matthew Phillips:
Can a servlet have multiple url mappings? The servlet could return 1 of three screens based on input data. The framesets are in 3 seperate folders. I also already have a url map so that I do not have to call the servlet by /servlet/servlet-name.

Yes, you can have multiple URL mappings. According to "Core Servlets and Java Server Pages", this is the preferred approach.
Matthew Phillips
Ranch Hand

Joined: Mar 09, 2001
Posts: 2676
Originally posted by Yuriy Fuksenko:
For this forward (I mean since it forward), the url for your frames should be relative to your original location.
If user tryed to access folder1\file1.html,
but you forward him to ..\folder2\file2.html, and file2.html contains some frames, request for this frames will be sent by a browser, and for browser you are still at folder1\file1.html. So if you frame url was frame2.html and frame2.html locate at folder2, frame url should be ..\folrder2\frame2.html

I tried that and it didn't seem to work.
Matthew Phillips
Ranch Hand

Joined: Mar 09, 2001
Posts: 2676
I am being really dense today. Let me post some code and maybe that will help.
This is the page my web app is forwarding to:


My directory struction is as follows:
alumni -- web app directory
administratorPages -- contains the above and all frames
The error I am getting in each frame is "/alumni/"frameName".jsp does not exist.
My web.xml file looks like this:



I have also tried the following mappings:


The url that I am passing to the forward method of RequestDispatcher is "/administratorPages/framesMain.jsp". framesMain.jsp is the name of the page above. What am I missing on this?
[ May 15, 2002: Message edited by: Matthew Phillips ]
John Fontana
Ranch Hand

Joined: Feb 28, 2002
Posts: 235
If you use the second mapping that uses the administratorpages uri, then you do not want to specify that directory in your RequestDispatcher. It would look for a subdirectory in administratorpages called administratorpages.
Just use /framesmain.jsp in your RequestDispatcher, and use the second url-mapping you listed.
Matthew Phillips
Ranch Hand

Joined: Mar 09, 2001
Posts: 2676
Originally posted by John Fontana:
If you use the second mapping that uses the administratorpages uri, then you do not want to specify that directory in your RequestDispatcher. It would look for a subdirectory in administratorpages called administratorpages.
Just use /framesmain.jsp in your RequestDispatcher, and use the second url-mapping you listed.

That one yeilded a 404 error "/alumni/framesMain.jsp was not found on this server."
John Fontana
Ranch Hand

Joined: Feb 28, 2002
Posts: 235
Now I see that it's an MVC-pattern controller servlet (correct me if I'm wrong)...A little tricky to troubleshoot without seeing the Servlet code (for example, is there a parameter being passed to the servlet and an if-then block to redirect?).
As an aside, if you don't have to pass request/response content to each JSP, then you could get away with using sendRedirect instead of RequestDispatcher....this will actually change the URL in the browser and you won't need to worry about mappings.
Anyway, feel free to send me the code if you like...I'm studying for SCWCD anyway and could use the practice...Also preferential treatment for fellow Kiss fans/musicians
Matthew Phillips
Ranch Hand

Joined: Mar 09, 2001
Posts: 2676
Your wish is my command:
The controller servlet is as follows:

It is pretty much the same as the example in Chapter 6 of Professional JSP Second Edition.
The action that handles login is as follows:

The forward method is in ActionBase and is as follows:

My understanding of url mapping certainly isn't the greatest, but it seems like this should work. The frame set opens ok by going directly to the url. When the servlet forwards to it, it just doesn't seem to pick up the other frames. Could this be a jsp issue?
Matthew Phillips
Ranch Hand

Joined: Mar 09, 2001
Posts: 2676
sendRedirect works, so I will stick with that. I am still curious as to why forward doesn't. Thanks for the assistance so far guys.
John Fontana
Ranch Hand

Joined: Feb 28, 2002
Posts: 235
I was able to write a little mini-app that works...here's a very stripped down version of it:
The "controller":

I put the frameset and its files in a subdirectory called administrator pages. Here's the mapping:

I called it using url: http(app-path)/administratorpages/test-me and it took me right to the frameset with no 404 errors.
Hope this helps!
[ May 15, 2002: Message edited by: John Fontana ]
Matthew Phillips
Ranch Hand

Joined: Mar 09, 2001
Posts: 2676
Originally posted by John Fontana:
I called it using url: http(app-path)/administratorpages/test-me and it took me right to the frameset with no 404 errors.
Hope this helps!
[ May 15, 2002: Message edited by: John Fontana ]

I may not have been clear at the beginning. Calling the path that way has always worked. When the LoginAction forwards to the relative url is when I have the problem.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: RequestDispatcher.forward problem