Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Jsp form validation using bean with int  RSS feed

 
Sandip Panchal
Greenhorn
Posts: 1
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using JSP with Servlet and bean. I've one int property defined in bean and using it in jsp. Now when I try to enter non-numeric value in that bound input box, I'm getting java.lang.NumberFormatException exception which is shown as in attached image.

I found Similar issue but was unable to find solution on that thread.

Where am I supposed to handle the exception?

Checkout below example..

UserBean.java





JSP





Capture.PNG
[Thumbnail for Capture.PNG]
StackTrace
 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sandip,
Always paste stack trace in code tags, do not attach stacktrace images.
Exception handling is necessary, but you should restrict the textbox to accept only numeric values in the first place.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65828
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tapas Chand wrote:Exception handling is necessary, but you should restrict the textbox to accept only numeric values in the first place.

This is true in both ways: the input should be restricted, but the server must still handle the case where it's not a number because the client cannot be trusted.

In this case, I would either check the value before conversion to avoid the exception entirely, or handle the exception with try/catch at the point of conversion. This is an expected error condition and so this exception should not be propagated up the servlet chain. That's for exceptions for truly exceptional conditions; not mundane user input errors.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!