aspose file tools*
The moose likes Struts and the fly likes Please advise how to catch runtimeException in servlet level. 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 » Frameworks » Struts
Bookmark "Please advise how to catch runtimeException in servlet level." Watch "Please advise how to catch runtimeException in servlet level." New topic
Author

Please advise how to catch runtimeException in servlet level.

Haifeng Jiang
Ranch Hand

Joined: Dec 14, 2008
Posts: 42
As you may know when program running and jsp contain RuntimeException(eg. NullPointerexception), it will display the error message in the jsp page. I want to catch it in servlet level. So I am thinking that if that can be fix. Would you please advise how can I handle it? Is there a method available?


Thinking a lot. Gaining a lot.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

What do you mean by "servlet level"?? Are you forwarding a request to a JSP from a servlet or are you talking about the servlet that is generated from a JSP page??


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
RaviNada Kiran
Ranch Hand

Joined: Jan 30, 2009
Posts: 528

throw the NullPointerException and use the

<error-page> tags in web.xml file .


If you want something you never had do something which you had never done
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
RuntimeExceptions in almost all cases indicate a fault in the code logic which the developer ought to fix.

You should never assume that they may occur in "real life".
Haifeng Jiang
Ranch Hand

Joined: Dec 14, 2008
Posts: 42
Thanks for all your response.
Would you please refer to this link for details?
http://www.coderanch.com/t/436551/Struts/Loop-issue-ExceptionHandlerServlet
Haifeng Jiang
Ranch Hand

Joined: Dec 14, 2008
Posts: 42
RaviNada Kiran wrote:
throw the NullPointerException and use the

<error-page> tags in web.xml file .


I defined below in web.xml file.


And I created NullPointerException in jsp file.


But when I run my program. The BPIMSExceptionHandlerServlet cannot catch the NullPointerException. Would you please help me on this? Thanks in advance.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
The problem is that you wrote raw Java code in a JSP file.
The JSP file itself already has initiated and committed the HTTP response.
When an exception occurs after that, the exception handler cannot change the response anymore.

Thumb rule: write Java code in Java classes and use JSP for presentation logic only. The EL and decent taglibs are capable of alternative handling when an object turns out to be null (e.g. displaying nothing and so on).
Haifeng Jiang
Ranch Hand

Joined: Dec 14, 2008
Posts: 42
Bauke Scholtz wrote:The problem is that you wrote raw Java code in a JSP file.
The JSP file itself already has initiated and committed the HTTP response.
When an exception occurs after that, the exception handler cannot change the response anymore.

Thumb rule: write Java code in Java classes and use JSP for presentation logic only. The EL and decent taglibs are capable of alternative handling when an object turns out to be null (e.g. displaying nothing and so on).

Thanks for your reply.
May I ask if this kind of exception(runtimeException) can be catched by servlet? Is there a method available to handle this case?

I did a test just now. Below just for an example. Added below code in my jsp file. The name and property doesn't exist.

When I run my program. The exception displayed in the page directly. I just want to let servlet catch the exception so it will not display in the front of user. Any suggestions will be appreciated.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61421
    
  67

When you move the code that could throw the NPE out of the JSP as advised, the deployment descriptor error handling should be able to effectively handle the errors.

Until you have a proper web application structure, things that should be easy, like error handling, will be hard.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Eric Jiang wrote:May I ask if this kind of exception(runtimeException) can be catched by servlet? Is there a method available to handle this case?
First of all: you should not catch RuntimeExceptions unless you're really knowing what you're doing. A RuntimeException is in almost all cases caused by a developer fault. You should just write the code so that a RuntimeException would never occur. Exceptions and Errors are another story.

That said, in case of a servlet you can just use the error-page directive in the web.xml the usual way.


When I run my program. The exception displayed in the page directly. I just want to let servlet catch the exception so it will not display in the front of user. Any suggestions will be appreciated.
You're using Struts. There is in fact no means of a servlet. Also Struts goes beyond my knowledge. Look/google around using the keywords "struts exception handling" or so.

[edit] removed your code in quote .. forum is bitching with HTML entities.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61421
    
  67

Moved to the Struts forum.
Haifeng Jiang
Ranch Hand

Joined: Dec 14, 2008
Posts: 42
Bauke Scholtz wrote:The problem is that you wrote raw Java code in a JSP file.
The JSP file itself already has initiated and committed the HTTP response.

Now I just have one question. My thinking is that jsp(it will be compiled to java class) run in server side. So if it contains NullPointerException, servlet should catch it. But it didn't.
Would you please explain more about this to me? Thank you so much for your kindly advice!
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
You talk about a servlet everytime. What servlet is it?
Haifeng Jiang
Ranch Hand

Joined: Dec 14, 2008
Posts: 42
Bauke Scholtz wrote:You talk about a servlet everytime. What servlet is it?

Below I copied from Sun website. FYI
JSP Technology and Java Servlets
JSP technology uses XML-like tags that encapsulate the logic that generates the content for the page. The application logic can reside in server-based resources (such as JavaBeans component architecture) that the page accesses with these tags. Any and all formatting (HTML or XML) tags are passed directly back to the response page. By separating the page logic from its design and display and supporting a reusable component-based design, JSP technology makes it faster and easier than ever to build Web-based applications.

JavaServer Pages technology is an extension of the Java Servlet technology. Servlets are platform-independent, server-side modules that fit seamlessly into a Web server framework and can be used to extend the capabilities of a Web server with minimal overhead, maintenance, and support. Unlike other scripting languages, servlets involve no platform-specific consideration or modifications; they are application components that are downloaded, on demand, to the part of the system that needs them. Together, JSP technology and servlets provide an attractive alternative to other types of dynamic Web scripting/programming by offering: platform independence; enhanced performance; separation of logic from display; ease of administration; extensibility into the enterprise; and, most importantly, ease of use.

Today servlets are a popular choice for building interactive Web applications. Third-party servlet containers are available for Apache Web Server, Microsoft IIS, and others. Servlet containers are usually a component of Web and application servers, such as BEA WebLogic Application Server, IBM WebSphere, Sun Java System Web Server, Sun Java System Application Server, and others.
Haifeng Jiang
Ranch Hand

Joined: Dec 14, 2008
Posts: 42
Now if I got the JspException/ServletException in jsp page, how can I catch it and give a friendly error page to the user? Anyone can advise me?
Kuldeep Tewari
Ranch Hand

Joined: May 22, 2006
Posts: 35
Do you want the user to redirect to an error page showing user friendly error message, instead of the staktrace?

If this is what you want to do then you'll need to create an error page say errorPage.jsp (<%@page isErrorPage="true" %> and whatever messages you want to show) .

and in your web.xml you need to have :

<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/errorPage.jsp</location>
</error-page>

also, look at this Link

But, as others have said a RuntimeException suggests a problem on part of the developer and you should try avoid writing such code.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Please advise how to catch runtimeException in servlet level.