944,010 Members | Top Members by Rank

Ad:
  • JSP Discussion Thread
  • Unsolved
  • Views: 1306
  • JSP RSS
Oct 16th, 2009
0

JSP: search string for bad characters.

Expand Post »
I am trying to make a cookie that should contain a number value. It all work fine, unless the form that sends the info to my receiver page sends and empty string. Then something strange happens. It does not seem to be empty, so my error checker does not detect that it is empty (null or ""). So when i try to make the string into an integer the server get angry with me. If there is a good way to search a string for characters, in my case, return false unless it is a number (0,1,...9), it would help a lot. Or if there is a way to make sure the form does not send empty strings in the first place.

Ps. My code is correct, so I won't post it.

Thanks for the help!
Similar Threads
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
Cort3z is offline Offline
56 posts
since Oct 2009
Oct 24th, 2009
0
Re: JSP: search string for bad characters.
Seriously, no one know?
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
Cort3z is offline Offline
56 posts
since Oct 2009
Oct 25th, 2009
0
Re: JSP: search string for bad characters.
You say that the form sends an empty string but your error checker doesn't detect empty strings? Have you tried debugging your code to see what *actually* the error checker is checking against?

There are a number of ways in which the problem can be rectified but without knowing the actual business context, suggesting an appropriate solution might be a bit difficult. How are you actually sending that value to the server? Setting it using Javascript? Sending it via hidden variables? Is that value mandatory i.e. should that value always be a digit?

As far as the solution is considered, there are a couple of them:
- If a check for blank string fails it means that the value sent consists of spaces. Try trimming the string before checking it for blank.
- You can always catch the NumberFormatException which is thrown when parsing the fails thereby handling all invalid input.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 720
Failure as a human
~s.o.s~ is offline Offline
8,872 posts
since Jun 2006
Oct 25th, 2009
0
Re: JSP: search string for bad characters.
Click to Expand / Collapse  Quote originally posted by ~s.o.s~ ...
You say that the form sends an empty string but your error checker doesn't detect empty strings? Have you tried debugging your code to see what *actually* the error checker is checking against?

There are a number of ways in which the problem can be rectified but without knowing the actual business context, suggesting an appropriate solution might be a bit difficult. How are you actually sending that value to the server? Setting it using Javascript? Sending it via hidden variables? Is that value mandatory i.e. should that value always be a digit?

As far as the solution is considered, there are a couple of them:
- If a check for blank string fails it means that the value sent consists of spaces. Try trimming the string before checking it for blank.
- You can always catch the NumberFormatException which is thrown when parsing the fails thereby handling all invalid input.
I have actually managed to fix the problem of not getting an empty string. I needed to trim it, but my main question is how i can search for certain characters. I have been trying to find this out for quite some time now, and I just cant seem to find the answer anywhere. What i want to do is to search the string, and return a value if anythig in that string is not a number (0,1, ... ,9). So if someone fills in "abc" in my 3 digit html form, i want the string to return null.

Thanks for the help!
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
Cort3z is offline Offline
56 posts
since Oct 2009
Oct 25th, 2009
0
Re: JSP: search string for bad characters.
> What i want to do is to search the string

Depends on what kind of search it is; if it's just searching for a single character or a string you can use the overloaded versions of the indexOf method. If you need power searching i.e. search for digits only or search for a pattern which starts with a ! and ends with #, you need to use Regular Expressions, which I admit might take a bit of learning.

> So if someone fills in "abc" in my 3 digit html form, i want the string to
> return null

You are now talking about validation which is a specific application of string searching. You can use regular expressions for such purpose. But IMO you'd be better off using a validation library like Commons Validator which would relieve you of writing all the validation code yourself. But this again comes with a bit of learning curve.
Last edited by ~s.o.s~; Oct 25th, 2009 at 12:47 pm.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 720
Failure as a human
~s.o.s~ is offline Offline
8,872 posts
since Jun 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JSP Forum Timeline: Problem calling jsp files
Next Thread in JSP Forum Timeline: javascript inserting between jsp





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC