aspose file tools*
The moose likes Servlets and the fly likes handling wrong data type entry through servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "handling wrong data type entry through servlet" Watch "handling wrong data type entry through servlet" New topic
Author

handling wrong data type entry through servlet

shaz baluch
Greenhorn

Joined: Dec 19, 2010
Posts: 9
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

Joined: Mar 22, 2000
Posts: 12835
    
    5
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

Joined: Jul 14, 2010
Posts: 754

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.


[uaiHebert.com] [Full WebApplication JSF EJB JPA JAAS with source code to download] One Table Per SubClass [Web/JSF]
Rakesh Chaudhary
Ranch Hand

Joined: Feb 19, 2008
Posts: 120
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

Joined: Jan 10, 2002
Posts: 61769
    
  67

What does the session is have to do with validating data?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18992
    
    8

How does equalsIgnoreCase("") work differently than equals("")? That code seems to be a convoluted and hard-to-understand rewriting of
Rakesh Chaudhary
Ranch Hand

Joined: Feb 19, 2008
Posts: 120
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

Joined: Nov 23, 2008
Posts: 48
Hi everybody,
If you use equalIgnoreCase() it will optimise performance instead of using equals.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: handling wrong data type entry through servlet