File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes How to check for a particular word in a string. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to check for a particular word in a string." Watch "How to check for a particular word in a string." New topic
Author

How to check for a particular word in a string.

rajarshi roy
Greenhorn

Joined: Feb 14, 2010
Posts: 21
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.....
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
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.
Michael Angstadt
Ranch Hand

Joined: Jun 17, 2009
Posts: 274

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


SCJP 6 || SCWCD 5
rajarshi roy
Greenhorn

Joined: Feb 14, 2010
Posts: 21
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.

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19726
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
rajarshi roy
Greenhorn

Joined: Feb 14, 2010
Posts: 21
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

Joined: Jun 17, 2009
Posts: 274

rajarshi roy wrote:


What happens if indexOf() returns zero?
rajarshi roy
Greenhorn

Joined: Feb 14, 2010
Posts: 21
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.

satish varma
Greenhorn

Joined: Feb 11, 2010
Posts: 27
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

Joined: Jun 17, 2009
Posts: 274

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

Joined: Feb 14, 2010
Posts: 21
@ 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

Joined: Jun 17, 2009
Posts: 274

Javascript can be turned off, so you should also consider server-side validation.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to check for a particular word in a string.