• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

handling wrong data type entry through servlet

 
shaz baluch
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • 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



 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • 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
 
Hebert Coelho
Ranch Hand
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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.
 
Rakesh Chaudhary
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • 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.....

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64718
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does the session is have to do with validating data?
 
Paul Clapham
Sheriff
Posts: 20983
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
  • 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





 
Habeeb Shaikh
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everybody,
If you use equalIgnoreCase() it will optimise performance instead of using equals.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic