• 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

How to check for a particular word in a string.

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have to do validation for a string passed in a particular field in a jsp.The string that is passed must not contain words like "script","delete","insert" etc.If these words are somehow found in the string then I will redirect the current page to an error page.Now I will have to use a java class file which will compare the strings passed with a standard set of words and special characters.I have been able to do the check for special characters.But how to check for a particular word in a string,say "delete"???

Please help.....
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are several ways to do it; you can use the methods of the String class, or a regular expression. You can split the String, put the values into a Set (preferably sorted) and interrogate the Set.

Bound to be many other ways.
 
Ranch Hand
Posts: 282
Eclipse IDE PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at the java.lang.String class in the API docs. The contains() and matches() methods should help you.
 
rajarshi roy
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Michael Angstadt wrote:Look at the java.lang.String class in the API docs. The contains() and matches() methods should help you.



I thought about it.But the basic problem with contains() is it is a feature of jre 1.5 ,and I want my code to be compatible in computers with older jre,say 1.4.Now matches() is compliant with jre 1.4.Let me have a look.

 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are other methods that can be used to check for presence, other than these two. Hint: they return an int which you can check against a value that denotes absence.
 
rajarshi roy
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:There are other methods that can be used to check for presence, other than these two. Hint: they return an int which you can check against a value that denotes absence.



Will something like this do???
 
Michael Angstadt
Ranch Hand
Posts: 282
Eclipse IDE PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

rajarshi roy wrote:



What happens if indexOf() returns zero?
 
rajarshi roy
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Michael Angstadt wrote:

rajarshi roy wrote:



What happens if indexOf() returns zero?



Thank you for pointing out.Well,I will do something like this-


Or I can append something at the beginning of the string.Well,I was working on this,and this approach works.Now,again I have a problem.

As I have said before,my objective is to do validation for the strings that are passed in a jsp from another using request.getparameter().
I am passing the request object to a method,running an Enumeration and validating the strings that are passed one by one using another method.
Something like this::



But,I am facing a very basic issue here.I am doing validation against special characters and some keywords here
There are various fields in the jsp where various kinds of data are passed.Say,for example there is a field where "date" is passed.Now date is in the format mm/dd/yyyy. Hence it contains the character "/".Similarly,the field "company_name" may contain characters like "-" and ".".
Till now,I was passing the request class for validation.Like this:



But,I cannot use this approach because as I said some of the objects may pass Strings containing "date" or "company_name" which are bound to contain some special characters.
I can obviously pass each request.getparameter() to a method performing validation,but thats too cumbersome to do.I want to know what is the proper approach in situations like this.

Thank you.

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
go for Regular Expressions concept ,see in java API "java.util .regex" package there are "Pattern and Matcher" classes and so many methods digg about those things
 
Michael Angstadt
Ranch Hand
Posts: 282
Eclipse IDE PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would suggest the following: Create an abstract class that contains methods for doing common validations, such as checking if a field is empty and if a date is in a correct format. This abstract class will have an abstract method which performs the validation. A sub class is then created for each form in your application, which validates the particular fields for that form:



Then, call the validator in your servlet:



There are validation tools out there which do this sort of thing in a nice, elegant way, but these tools take time to learn, so in the short term, I might write my own validation.
 
rajarshi roy
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ Michael Angstadt

Thank you very much.The use of abstract classes is a good idea.I will give it a try.Though it might not be they way you are saying.I am already using javascript embedded in the jsp to do basic validations like the field is empty or not,whether the user is entering numerics only in a field meant for phone numbers.
As for example:


I will try to elaborate on what I am trying to achieve here.Basically I want to develop a simple java class file that will take care of "Cross Site Scripting".I want a simple java class file as I want to re-use it in various other jsp.
But this idea of using Abstract class is good.Let me see what I can do.

Thanks again for your efforts.
 
Michael Angstadt
Ranch Hand
Posts: 282
Eclipse IDE PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Javascript can be turned off, so you should also consider server-side validation.
 
Water proof donuts! Eat them while reading this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic