• 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

jsp form validation using bean with int

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to validate forms as shown in the Java Developers Almanac example. My problem is when I use beans with numeric values such as int. If I submit the form with String values in the int field, the page throws NumberFormatException. My validation code is never reached. What do I need to do to perform my own validation?

 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Integer.parseInt(yourString) will convert a String to an int (providing the string you pass to it can be converted to an int .
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ben Souther:
Integer.parseInt(yourString) will convert a String to an int (providing the string you pass to it can be converted to an int .



if can not be converted into String, he will again run into problem.
Use some utility method of your own. like below,



It is returning Integer, instead of int. Actually I prefer to use Integer when writing VOs. Because there is a lot difference between 0 and null according the the database especially. If you want to write your VO into database you wouldn't want to write null as 0. and int can not be a null.

cheers.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, there's no need to change your data type.
If your user is passing non-numerics in, you want to catch that right up front.


Java now (starting with version 1.4) has regular expression tools.
You could also write a quick isInt(String) method using those.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah this sounds better.
Actually, I was not really saying to change the type . However, I explained the case, why change to Integer.

thanks.
 
Tim Troy
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The error shown in the stacktrace is caused by the line,
.
The tag reads the form and populates the bean. If the form field contains a String and the bean field is an int, the NumberFormatException is thrown. If my jsp form bean can only use Strings, then the usefulness of setting all properties with "*" is dimminished. Is there a way to catch bad input data using this example? I understand the suggestions posted, but, how can they be applied since the jsp:setProperty tag is causing the problem before I can run my error checking code?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm, first thought....




Second thought:




These are just a couple ideas. Not sure the second one will even compile.
I generally handle all the validation and transformation in servlets so I'm
interested in seeing if someone has a more graceful or "established best practice" way of doing this.
[ January 13, 2005: Message edited by: Ben Souther ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic