• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Please advise how to catch runtimeException in servlet level.

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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??
 
Ranch Hand
Posts: 528
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

throw the NullPointerException and use the

<error-page> tags in web.xml file .
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all your response.
Would you please refer to this link for details?
https://coderanch.com/t/436551/Struts/Loop-issue-ExceptionHandlerServlet
 
Haifeng Jiang
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moved to the Struts forum.
 
Haifeng Jiang
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You talk about a servlet everytime. What servlet is it?
 
Haifeng Jiang
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic