aspose file tools*
The moose likes JSP and the fly likes Problem in error-page tag Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Problem in error-page tag" Watch "Problem in error-page tag" New topic
Author

Problem in error-page tag

Arun Somasundaram
Ranch Hand

Joined: Jul 25, 2006
Posts: 63
Hi this is Arun,

I am having a problem in <error-page> tag of my web.xml.

In my web.xml file I added the follwing snippet between </welcome-file-list> and </web-app>.



I am having my file.jsp in the root directory of my web application.

When I request abc.jsp which is not deployed in my webapplication

My browser renders the following:-



The page cannot be found
The page you are looking for might have been removed, had its name changed, or is temporarily unavailable.

--------------------------------------------------------------------------------

Please try the following:

If you typed the page address in the Address bar, make sure that it is spelled correctly.

Open the sun home page, and then look for links to the information you want.
Click the Back button to try another link.
Click Search to look for information on the Internet.



HTTP 404 - File not found
Internet Explorer



Instead of the contents of file.jsp

More over the tomcat generated error page is also not displayed.

The code for file.jsp is:-




What should be done inorder to display the contents of file.jsp if the requested resource is not available in the webapp.


Arun.V.Somasundaram
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

MSIE is seeing the 404 response header and showing you it's own 404 page.

To disable:
From the Tools -> Options -> Advanced window in MSIE, uncheck the "Show Friendly Http Error Page".


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Arun Somasundaram
Ranch Hand

Joined: Jul 25, 2006
Posts: 63
Thanks my friend Ben.I got it!!!.When ever I am in trouble you are resolving it.So thanks again!!!
Arun Somasundaram
Ranch Hand

Joined: Jul 25, 2006
Posts: 63
It troubles again.I didn't see the URL displayed by the browser at first.Just now I saw.The URL displayed by the browser is file.jsp

i.e I am getting the output as:

"http://sun/jspbook/file.jsp" is not Found in web application.Please try a valid URL.

But I browsed for abc.jsp which is not deployed in my webapp.

I should get the output as:

"http://sun/jspbook/abc.jsp" is not Found in web application.Please try a valid URL.

But I don't.
[ August 03, 2006: Message edited by: Arun Somasundaram ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

If the server redirects to the file.jsp then that's what the requestURI will be.

If you want to see the original error, read up on the isErrorPage attribute and the implicit "exception" object provided to JSPs by the container.
[ August 03, 2006: Message edited by: Ben Souther ]
Arun Somasundaram
Ranch Hand

Joined: Jul 25, 2006
Posts: 63
Mr Ben I think the request is not redirected by the server it is forwarded to file.jsp.So the requested URL would be abc.jsp.Your opinion?
[ August 03, 2006: Message edited by: Arun Somasundaram ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

If you want to know whether it's being redirected or forwarded, download the LiveHttpHeaders plugin for FireFox and watch for the 30x header.
If you see it, then it's being redirected.

In either case, if you want a proper error page, you will need to look into the things I mentioned earlier.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I just did this and you are correct, the container forwards to the error page but the requestURI gets updated as well.

As I said, in either case, if you want to see the original error message, look into the isErrorPage attribute to the "page" directive and read up on the implicit 'exception' object provided by the container to your JSP.
[ August 03, 2006: Message edited by: Ben Souther ]
Arun Somasundaram
Ranch Hand

Joined: Jul 25, 2006
Posts: 63
Thanks Ben!.Let me read through the link you have given.
Arun Somasundaram
Ranch Hand

Joined: Jul 25, 2006
Posts: 63
One thing Ben.I am not handling the errors through attributes provided by the page directive.I am handling the error through web.xml . Here web.xml handles the error and then it forwards the request to file.jsp . So I think there is no need to see the isErrorpage attribute of the page directive.

There are two ways to handle exceptions in webapplications

1.) micro managing exceptions(exceptions handled on individual basis)
2.) macro managing exceptions(exceptions handled on application wide basis)

In micro managing exceptions the exceptions are handled on an individual basis using try catch finally statements.There it is necessary to use errorpage and isErrorpage attributes.

When it comes to macro managing exceptions the exceptions are caught on an application wide basis using web.xml.I follwed this way to handle exceptions.So I think there is no need to worry about errorPage and isErrorpage attributes.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Arun Somasundaram:
One thing Ben.I am not handling the errors through attributes provided by the page directive.I am handling the error through web.xml . Here web.xml handles the error and then it forwards the request to file.jsp . So I think there is no need to see the isErrorpage attribute of the page directive.

There are two ways to handle exceptions in webapplications

1.) micro managing exceptions(exceptions handled on individual basis)
2.) macro managing exceptions(exceptions handled on application wide basis)

In micro managing exceptions the exceptions are handled on an individual basis using try catch finally statements.There it is necessary to use errorpage and isErrorpage attributes.

When it comes to macro managing exceptions the exceptions are caught on an application wide basis using web.xml.I follwed this way to handle exceptions.So I think there is no need to worry about errorPage and isErrorpage attributes.


If you declare your error page for an error-code in web.xml then you won't need to explictly use an errorPage="myErrorPage.jsp" directive in all the pages that could throw an exception.

You will, however need to add the isErrorPage="true" directive to you actual error page if you want to work with the exception.

Arun Somasundaram
Ranch Hand

Joined: Jul 25, 2006
Posts: 63
Let me list the problem once again

I am having a problem in <error-page> tag of my web.xml.

In my web.xml file I added the follwing snippet between </welcome-file-list> and </web-app>.






I am having my file.jsp in the root directory of my web application.

When I request abc.jsp which is not deployed in my webapplication

My browser renders the following:-

i.e I am getting the output as:

"http://sun/jsp-practice/file.jsp" is not Found in web application.Please try a valid URL.

But I browsed for abc.jsp which is not deployed in my webapp.

I should get the output as:

"http://sun/jsp-practice/abc.jsp" is not Found in web application.Please try a valid URL.

But I don't.


Here:-

I am not handling the errors through attributes provided by the page directive.I am handling the error through web.xml . Here web.xml handles the error and then it forwards the request to file.jsp . So I think there is no need to see the isErrorpage attribute of the page directive.

There are two ways to handle exceptions in webapplications

1.) micro managing exceptions(exceptions handled on individual basis)
2.) macro managing exceptions(exceptions handled on application wide basis)

In micro managing exceptions the exceptions are handled on an individual basis using try catch finally statements.There it is necessary to use errorpage and isErrorpage attributes.

When it comes to macro managing exceptions the exceptions are caught on an application wide basis using web.xml.I follwed this way to handle exceptions.So I think there is no need to worry about errorPage and isErrorpage attributes.

MoreOver I don't want to work with the exception object so I don't want to use the isErrorPage="true" attribute in the error page file.jsp.

Just I want to see the actual output when I browse for an undeployed resource in my webapp.

for example when I browse for

"http://sun/jsp-practice/abc.jsp" which is not deployed in the webapp

jsp-practice.


My browser must render the following actual output

"http://sun/jsp-practice/abc.jsp" is not Found in web application.Please try a valid URL.

which I dont get . Instead my browser renders the follwing output:-

"http://sun/jsp-practice/file.jsp" is not Found in web application.Please try a valid URL.

which is not the actual output.

The code for file.jsp is:-

<HTML>
<HEAD>
<TITLE>Error Page</TITLE>
</HEAD><BODY>
<%=request.getRequestURL()%> is not Found , Please try a valid URL.
</BODY>
</HTML>

[ August 03, 2006: Message edited by: Arun Somasundaram ]
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

The original request is redirected, that's why you get "file.jsp" instead of the original url.



[My Blog]
All roads lead to JavaRanch
Arun Somasundaram
Ranch Hand

Joined: Jul 25, 2006
Posts: 63
NO . If you check it properly you can see that the original request is forwarded and not redirected.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

NO

Using capital is considered as shouting, you should be careful.

You're right about the request being forwarded.
Arun Somasundaram
Ranch Hand

Joined: Jul 25, 2006
Posts: 63
Ok here after no NO's.Then what is the problem why the requested URL is not displayed.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

abc.jsp --- forward ---> file.jsp
The request's URI is now set to "file.jsp".

The original URI is stored in the request attribute "javax.servlet.forward.request_uri".
In fact, you can access the following attributes :

- javax.servlet.forward.request_uri
- javax.servlet.forward.context_path
- javax.servlet.forward.servlet_path
- javax.servlet.forward.path_info
- javax.servlet.forward.query_string

For error pages, some more attributes are set :
- javax.servlet.error.status_code
- javax.servlet.error.exception_type
- javax.servlet.error.message
- javax.servlet.error.exception
- javax.servlet.error.request_uri
- javax.servlet.error.servlet_name

About error pages, an ErrorPage object is instanciated. You can get some information from there too.
${pageContext.errorData.requestURI}



Try this:
Arun Somasundaram
Ranch Hand

Joined: Jul 25, 2006
Posts: 63
Yes <%= request.getAttribute("javax.servlet.forward.request_uri") %>
gives the correct requested URL.Thanks Satou kurinosuke for giving a nice solution.

When I browse for abc.jsp I get the following rendered by the browser.

/jspbook/abc.jsp is not Found , Please try a valid URL.
error request_uri : /jspbook/abc.jsp ---> The output I expected
error exception_type : null
error message : /abc.jsp
error exception : null
error status_code : 404
error servlet_name : jsp

forward request_uri : /jspbook/abc.jsp ---> The output I expected
forward context_path : /jspbook
forward servlet_path : /abc.jsp
forward path_info : /file.jsp
forward query_string : null
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Problem in error-page tag