Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

replace method giving me guff  RSS feed

 
Ben Johnson
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, everyone. I am working on a profanity filter that replaces profane words with asterisks. This works just great on my Apache Tomcat server, but fails with the following exception on an August, 2002 build of the Resin server. Below, I have pasted the error message, and below that, is the portion of source code that is causing the error.

Any ideas as to why this doesn't work on the Resin server? I'm new to Java. (This is in a JSP page, by the way, not a servlet.)

500 Servlet Exception

/mysite/filter_badwords.jsp:54:
replace(char,char) in java.lang.String cannot be applied to (java.lang.String,java.lang.String)
result[i] = result[i].replace(badwords[j], replacementString);
^ (NOTE: this carat is actually supposed to appear just below the third angle bracket on this line "[")
1 error


Resin 2.1.4 (built Fri Aug 2 14:16:52 PDT 2002)



[ July 22, 2005: Message edited by: Ben Johnson ]
[ July 22, 2005: Message edited by: Ben Johnson ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like the error message says, String.replace(char,char) doesn't work with strings as arguments.

If you're using j2sdk1.4 or higher, you can use the String.replaceAll(regexp,String) method.


I'm new to Java

Javadocs will be your best friend.
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html

See my signature for the J2EE api.
[ July 22, 2005: Message edited by: Ben Souther ]
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to the javadoc:


public String replace(char oldChar, char newChar)


You probably want to use replaceAll().

So Resin is right to complain.

As to why Tomcat allows it? Dunno. You can look at the java servlet created by Tomcat on behalf of the JSP to see what Tomcat did with it.

This is in a JSP page, by the way, not a servlet.


Take this as a painful lesson on why this is just one of the 5,327 reasons not to do this kind of processing in a JSP.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<bonk!> Bear and Ben bump heads yet again!
 
Ben Johnson
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, awesome. Thanks for the replies (and for the links to the API etc.)!

I gave replaceAll a shot, with some really simple code (see below), and this is what the server kicked back:

mysite/test.jsp:7: cannot
resolve symbol
symbol : method replaceAll (java.lang.String,java.lang.String)
location: class java.lang.String
output = input.replaceAll("s", "z");
^
1 error



Obviously, it doesn't work as expected. I am guessing that the inability to resolve a symbol is evidence of an older server that is running old Java software? Again, it works fine on Tomcat (though, you are right, that is really strange that the previous code I was using was working at all, given what you said!)

So, is this how it works... the server is running a particular version of the JDK that spawns virtual machines that do the servlet processing, and the JDK version is simply too old to support the regex methods that are contained in newer versions?

If so, is there any possible workaround? Unfortunately, I do not have access to the server on which this will be forced to run, and the IT people will assuredly refuse to do anything to facilitate this project.

Thanks again, your help is very much appreciated.

Ben
 
Jon Egan
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone. I think the reason "replace(String, String)" is working in Tomcat is that it's using Java 1.5. In 1.5, String has this new method:



And String implements CharSequence, so this matches.

As Ben Souther mentioned, the method:


didn't exist until 1.4, which was introduced in Feb 2002 - and evidently wasn't supported by the Aug 2002 build of Resin you are using.

Also, note that the first parameter to that method isn't interpretted as a literal String, but as a pattern from which to construct a "java.util.regex.Pattern" object and use regular expression matching... in your case, this shouldn't be an important distinction, since you are replacing "words". Oh yeah, and also not relevant because the method simply doesn't exist for you....

So, the Tomcat server seems to be running 1.5, and Resin seems to be running some version pre-1.4 (probably late 1.3).

-- Jon
 
Jeffrey Spaulding
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use

org.apache.commons.lang.StringUtils

Method is

StringUtils.replace(StringToWorkOn,StringYouLookFor,StringToInsertInstead)

Profanity Filter eh?

****



J.
 
Ben Johnson
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's odd... where'd the post go (I think it was Ben Souther's) that contained the "inefficient" function for replacing text without any fancy string manipulation packages?

And thank you for the reply, Jon. That makes perfect sense as to why Tomcat handles the call differently.


You could use
org.apache.commons.lang.StringUtils


Jeffrey, how does that all work? I haven't quite figured out how to use packages like the one you described, because I don't see that it listed in the JDK 5.0 API. Is there something additional I need to download to use that class?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
 
Jeffrey Spaulding
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can download the binaries from

http://www.apache.org/dist/jakarta/commons/lang/

You simply (well as far as classpath stuff can get simple) add the
jar to your classpath.

If you work with an IDE you can add the jar as external jar to your
project.

In Eclipse that would be
Project->Properties->JavaBuildPath->Libraries->Add external jars

Apidoc sits here

http://jakarta.apache.org/commons/lang/api/index.html

J.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!