RequestDispatcher(file).forward(req, res) - 404?

5 views
Skip to first unread message

Mark van Wyk

unread,
May 6, 2011, 5:04:41 PM5/6/11
to ctjug...@googlegroups.com
Hi there,

Any idea how to "catch" the 404 if the file does not exist? Googling really not helping here.

Simple sticking it in a try catch not workin...

Ideas?


Mark van Wyk



Mobile: 082 831 9227

Mike

unread,
May 7, 2011, 9:12:18 AM5/7/11
to ctjug...@googlegroups.com
On 06/05/2011 23:04, Mark van Wyk wrote:

> Any idea how to "catch" the 404 if the file does not exist? Googling
> really not helping here.

Not sure I understand your problem... Do you mean you get a request
(URL) for a page/resource that you don't have, and so are unable to
forward to in your frontcontroller servlet? If that's what you mean,
just do a
response.setStatus( HttpServletResponse.SC_NOT_FOUND ); //aka 404
and return from the servlet.

(All from memory not my IDE, so modulo spelling/syntax errors.)

BTW you're probably better off mapping your universal servlet to "/"
rather than "/*" which makes it the "default" servlet. All other
mappings are longer than it, so will be matched preferentially.
--
mike morris :: mikro2nd (at) gmail (dot) com

http://mikro2nd.net/
http://mikro2nd.net/blog/planb/
http://mikro2nd.net/blog/mike/

This email is [X]bloggable [ ]ask-first [ ]private

Mark van Wyk

unread,
May 7, 2011, 9:40:12 AM5/7/11
to ctjug...@googlegroups.com
Hi Mike,

Sorry for being unclear.

My filter happily forwards appropriate requests to my controller servlet, and then my controller servlet in turn forwards the requests onto a JSP view. If the view is not there, however, the container returns the default 404 or specified error page.

I, however, would like my controller to be aware of the JSP view not being found when I call requestDispatcher("/somejsp.jsp").forward(req, res). I tried embedding this line in a try / catch, but I think that there is some callback mechanism that needs to be used. 

Ideas appreciated.

Thanks for the tips.

Mark van Wyk



Mobile: 082 831 9227




--
You received this message because you are subscribed to the Google Groups "CTJUG Forum" group.
To post to this group, send email to CTJUG...@googlegroups.com
To unsubscribe from this group, send email to CTJUG-Forum...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/CTJUG-Forum
For the ctjug home page see http://www.ctjug.org.za
For jobs see http://jobs.gamatam.com/

Len Weincier

unread,
May 8, 2011, 1:09:51 AM5/8/11
to ctjug...@googlegroups.com
Hi

Are you trying to have the jsp be dynamically chosen ?

I think the only way to avoid the 404 is to try open the actual jsp file before the forward. I.e. Work out where in the file system it is as a physical file and use File to open it and then you can catch FileNotFound.

Or do you want control over what is displayed on the 404 ?

Len 


Mark van Wyk

unread,
May 8, 2011, 11:51:23 AM5/8/11
to ctjug...@googlegroups.com
Hi Len,

Ag, I'm just going to let Tomcat show the 404 error page specified in web.xml and then I'll put some code in there to record that the page did not exist I think. Just would have liked it integrated into the controller when it tries to call a view. Thanks for your response.

Mark van Wyk



Mobile: 082 831 9227



Reply all
Reply to author
Forward
0 new messages