Display error page using web.xml <error-page>

I need to dispaly error page when IOException in thrown. How to do that with web.xml error-page tag?

start.jsf

public String doButton1Action()throws IOException{

String value = getText1().getValue().toString();

try{

File f =new File(value);

InputStream in =new FileInputStream(f);

...

}catch (IOException e){

thrownew IOException();

}

return"success";

}

I have this in web.xml:

<error-page>

<exception-type>java.io.IOException</exception-type>

<location>/ioerror.jsp</location>

</error-page>

[1123 byte] By [basti78a] at [2007-11-27 2:28:35]
# 1

You have to catch error-code 500, this is because the Exception is caught by the FacesServlet.

Also see http://wiki.apache.org/myfaces/Handling_Server_Errors

By the way, it's not a good practice to suppress or overwrite exceptions, you'll never get the cause and the trace of the originating exception then.

So, remove the whole try/catch block or just gently do a "throw e;" instead of "throw new IOException();".

BalusCa at 2007-7-12 2:40:53 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Ok, I removed try/catch block and changed exception-type to error-code

<error-page>

<error-code>500</error-code>

<location>/error.jsp</location>

</error-page>

Now I get Error Page Exception:

Error page exception

The server cannot use the error page specified for your application to handle the Original Exception printed below. Please see the Error Page Exception below for a description of the problem with the specified error page.

Is it enough to use just a plain jsp page with some text as an error page?

basti78a at 2007-7-12 2:40:53 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
Depends on your design requirements :)You can apply some layout however. This article describes just a basic example.
BalusCa at 2007-7-12 2:40:53 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

Let's say I want (for now) just a page with simple text, nothing else, just to make it work first.

Part of my web.xml:

<servlet id="Servlet_1169826798543">

<servlet-name>Faces Servlet</servlet-name>

<servlet-class>

javax.faces.webapp.FacesServlet</servlet-class>

<load-on-startup>-1</load-on-startup>

</servlet>

<servlet id="Servlet_1169826805063">

<servlet-name>JavaScript Resource Servlet</servlet-name>

<servlet-class>

com.ibm.faces.webapp.JSResourceServlet</servlet-class>

<load-on-startup>-1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>Faces Servlet</servlet-name>

<url-pattern>/faces/*</url-pattern>

</servlet-mapping>

<servlet-mapping>

<servlet-name>Faces Servlet</servlet-name>

<url-pattern>*.faces</url-pattern>

</servlet-mapping>

<servlet-mapping>

<servlet-name>JavaScript Resource Servlet</servlet-name>

<url-pattern>/.ibmjsfres/*</url-pattern>

</servlet-mapping>

<welcome-file-list>

<welcome-file>index.html</welcome-file>

<welcome-file>index.htm</welcome-file>

<welcome-file>index.jsp</welcome-file>

<welcome-file>default.html</welcome-file>

<welcome-file>default.htm</welcome-file>

<welcome-file>default.jsp</welcome-file>

</welcome-file-list>

<error-page>

<error-code>500</error-code>

<location>/errorPage.jsp</location>

</error-page>

So I keep getting this error page from server, when trying to direct error to my error page:

Error page exception

The server cannot use the error page specified for your application to handle the Original Exception printed below. Please see the Error Page Exception below for a description of the problem with the specified error page.

Original Exception:

Error Message: #{pc_Start.doButton1Action}: javax.faces.el.EvaluationException: java.io.FileNotFoundException: c:/temp/ghostfile.txt

Error Code: 500

Target Servlet: Faces Servlet

Error Stack:

javax.faces.FacesException: #{pc_Start.doButton1Action}: javax.faces.el.EvaluationException: java.io.FileNotFoundException: c:/temp/ghostfile.txt

...

Error Page Exception:

Error Message: Cannot find FacesContext

Error Code: 0

Target Servlet: null

Error Stack:

javax.servlet.jsp.JspException: Cannot find FacesContext

Am I missing something here?

basti78a at 2007-7-12 2:40:53 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

> Error Message: Cannot find FacesContext

> Error Code: 0

> Target Servlet: null

> Error Stack:

> javax.servlet.jsp.JspException: Cannot find

> FacesContext

You need to apply url-pattern of the FacesServlet on the errorpage. *.faces or /faces/ so that the FacesServlet gets invoked.

Replace<location>/errorPage.jsp</location>

by<location>/errorPage.faces</location>

BalusCa at 2007-7-12 2:40:53 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

I tried that earlier but then I get error just printed to console and HTTP 500 - Internal server error -page, bubt not my errorPage.

[26.1.2007 21:19:16:543 EET] 00000038 jsfE com.sun.faces.lifecycle.InvokeApplicationPhase execute #{pc_Start.doButton1Action}: javax.faces.el.EvaluationException: java.io.FileNotFoundException: c:\temp\ghostfile.jpg

javax.faces.FacesException: #{pc_Start.doButton1Action}: javax.faces.el.EvaluationException: java.io.FileNotFoundException: c:\temp\ghostfile.jpg

at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:79)

at javax.faces.component.UICommand.broadcast(UICommand.java:312)

at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:298)

at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:412)

at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:77)

at com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:220)

at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:91)

at javax.faces.webapp.FacesServlet.service(FacesServlet.java:197)

at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:966)

at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:478)

at com.ibm.ws.wswebcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:463)

at com.ibm.ws.webcontainer.servlet.CacheServletWrapper.handleRequest(CacheServletWrapper.java:92)

at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:744)

at com.ibm.ws.wswebcontainer.WebContainer.handleRequest(WebContainer.java:1433)

at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:93)

at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:465)

at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewInformation(HttpInboundLink.java:394)

at com.ibm.ws.http.channel.inbound.impl.HttpICLReadCallback.complete(HttpICLReadCallback.java:102)

at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:152)

at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:213)

at com.ibm.io.async.AbstractAsyncFuture.fireCompletionActions(AbstractAsyncFuture.java:195)

at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:136)

at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:194)

at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:741)

at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:863)

at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1510)

Caused by: javax.faces.el.EvaluationException: java.io.FileNotFoundException: c:\temp\ghostfile.jpg

at com.sun.faces.el.MethodBindingImpl.invoke(MethodBindingImpl.java:131)

at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:73)

... 25 more

basti78a at 2007-7-12 2:40:53 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7
Ok, I got it working now. Thanx for your help.
basti78a at 2007-7-12 2:40:53 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8
What was the solution?
BalusCa at 2007-7-12 2:40:53 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9

Last line at error dump:

0000003d ServletWrappe ISRVE0242I: [ErrorTestEAR] [/ErrorTest] [/errorPage.jsp]: Initialization successful.

Page was loaded ok but was not displayed. I was using IE. Then I tried with Firefox and it worked fine. I tried to change IE's confoguration and unchecked 'Show friendly HTTP error messages' check box. Then my custom error page worked also in IE.

basti78a at 2007-7-12 2:40:53 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...