| | |
JSP: search string for bad characters.
Please support our JSP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Oct 2009
Posts: 3
Reputation:
Solved Threads: 0
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!
Ps. My code is correct, so I won't post it.
Thanks for the help!
0
#3 33 Days Ago
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.
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 don't accept change; I don't deserve to live.
•
•
Join Date: Oct 2009
Posts: 3
Reputation:
Solved Threads: 0
0
#4 33 Days Ago
•
•
•
•
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.
Thanks for the help!
0
#5 33 Days Ago
> 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.
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~; 33 Days Ago at 12:47 pm.
I don't accept change; I don't deserve to live.
![]() |
Similar Threads
- pls Help!! how to search for string in a file with C# and Web Services?? (C#)
- pls Help!! how to search for string in a file with C# and Web Services?? (RSS, Web Services and SOAP)
- Search A String for proper format (C++)
- Search A String (C++)
- search in a string (Assembly)
- Search for a string in a variable--- (PHP)
- How to search for a string in a file (C#)
- Search string in a text file (C)
- Search a string of ',' (C++)
- New User (C)
Other Threads in the JSP Forum
- Previous Thread: Problem calling jsp files
- Next Thread: javascript inserting between jsp
| Thread Tools | Search this Thread |
apache backbutton combobox connection database development directorystructure dynamicpagetitles eclipse frames glassfish ie8 imagetodatabse imageupload integer internet java javaee javascript jsf jsp jsppagetitles levels mvc2 mvcmodel2 network parameters passing ping printinserverinsteadofclient redirect request.getparameter response servlet servletdopost()readxml sessions software ssl state_saving_method stocks sun tomcat tutorial update video web






