• 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

handling wrong data type entry through servlet

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Salaam All!
This servlet works well when I enter the correct data type in the required fields but with wrong entry and blank entry it only shows exception error. So how would I handle this through a message? how to code this?
thanks



 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you will check the JavaDocs for java.lang.Integer you will find that parseInt can throw a NumberFormat Exception which will naturally stop execution of your method..

You will have to provide for checking all of the field values that are going to be parsed as integers.

In fact, it would be good practice to check all of the form values for valid values before modifying your database. Yes this will take a lot of coding, no there is no super simple shortcut.

Bill
 
Ranch Hand
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello shaz baluch,

If you take a look in my signature there is a basic servlet hello world that shows how to do this kind of action.

When you receive a string value from the the web it might come null.

You have to test the value before the parsing method be invoked.
 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

shaz baluch,

You can try validation for blank entries which looks something like the following before storing your values to the database for the values you receive.Than parse it to Integer and store it to the database.


and for wrong entries ... use if else blocks as per your conditions

I hope this works.....

 
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
What does the session is have to do with validating data?
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How does equalsIgnoreCase("") work differently than equals("")? That code seems to be a convoluted and hard-to-understand rewriting of
 
Rakesh Chaudhary
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul,

code seems to be a convoluted and hard-to-understand rewriting of



What I meant by this validation code is to check the null values coming from a dB..Sorry I read the question wrong..

But I think for other string values this guy can use my code.

How does equalsIgnoreCase("") work differently than equals("")?



I think for future reference for any kind of deep comparison ...equals may fail but equalsIgnoreCase wont ..

Please correct me if I am wrong





 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everybody,
If you use equalIgnoreCase() it will optimise performance instead of using equals.
 
Don't mess with me you fool! I'm cooking with gas! Here, read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic