Two Laptop Bag*
The moose likes Servlets and the fly likes query from Marty Hall's Core Servlets--search engines Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "query from Marty Hall Watch "query from Marty Hall New topic
Author

query from Marty Hall's Core Servlets--search engines

preeti dengri
Ranch Hand

Joined: Nov 30, 2000
Posts: 111
Hi all,
i tried compiling the classes of the example --a front end to various search engines i.e listing 6.1 of Marty Hall's Core Servlets and JSP
and got these errors
searchengines.java:36: non-static method getName() cannot be referenced from a s
tatic context
System.out.println(searchspec.getName() +"hi");
^
searchengines.java:40: non-static method makeURL(java.lang.String,java.lang.Stri
ng) cannot be referenced from a static context
String url= searchspec.makeURL(searchstring,numResults);

where searchspec.java is a helper class to construct a search String for a particular search engine
i tried exactly the way it is given in the book but got the above errors.
What is considered to be a static context ---doGet() method???
I am unable to figure out the problem and please help me if somebody did go thru this example.
Thank You
Preeti
JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
Preeti: I have done this SearchEngine example in Marty Hall's book. Where (I mean the directory) did you put SearchEngine.java and SearchSpec.java files? And how did you do the compilation? I may be able to contribute some thoughts.
preeti dengri
Ranch Hand

Joined: Nov 30, 2000
Posts: 111
Hi Jen,
I have saved these files, SearchEngine.java and SearchSpec.java in the webclient/servlets under the webapps dir of tomcat and after compilation i saved the class files in webclient/web-inf/classes directory. this is how i have been working with the examples of the book and they were working fine.

the error i mentioned
searchengines.java:36: non-static method getName() cannot be referenced from a s
tatic context
System.out.println(searchspec.getName() +"hi");
^
is just for testing purposes i was using,but even when i removed this i got the second error that is
searchengines.java:40: non-static method makeURL(java.lang.String,java.lang.Stri
ng) cannot be referenced from a static context
String url= searchspec.makeURL(searchstring,numResults);

appreciate your help
preeti
JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
So, you did not have any problem with the source code downloaded directly from the coreservlet site. The problem occurs after you made some changes to the original source code. Is it right? The error message you have usually indicate that your static method and non-static methods are mis-placed. What changes did you make to the original code?
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
Originally posted by preeti dengri:
Hi Jen,
I have saved these files, SearchEngine.java and SearchSpec.java in the webclient/servlets under the webapps dir of tomcat and after compilation i saved the class files in webclient/web-inf/classes directory. this is how i have been working with the examples of the book and they were working fine.

the error i mentioned
searchengines.java:36: non-static method getName() cannot be referenced from a s
tatic context
System.out.println(searchspec.getName() +"hi");
^
is just for testing purposes i was using,but even when i removed this i got the second error that is
searchengines.java:40: non-static method makeURL(java.lang.String,java.lang.Stri
ng) cannot be referenced from a static context
String url= searchspec.makeURL(searchstring,numResults);

appreciate your help
preeti

It looks like you saved the .java files with the wrong names. Could that be it? Your error message references a searchengines.java, but it should be SearchEngines.java, and SearchSpec.java
Looking at the example in the book, your capitalization isn't matching what the book has. For example, in SearchEngines.java we have these relevant lines:
SearchSpec searchSpec = commonSpecs[i];
...
String url = searchSpec.makeURL(searchString, numResults);
According to what you posted it looks like you named your file searchspec.java (so therefore probably the class too), and you mistyped the line as:
String url = searchspec.makeURL(searchString, numResults);
So therefore Java thinks you are trying to access a static method.
Hope this helps.
Jason
Bob White
Greenhorn

Joined: Jun 23, 2001
Posts: 9
I don't know if you have this working by now, or not. But all three of the following need to be declared static:
searchspec, getName and makeURL. If this is not feasible for your example, then searchspec cannot be referenced from a static context.
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
Originally posted by Bob White:
I don't know if you have this working by now, or not. But all three of the following need to be declared [b]static:
searchspec, getName and makeURL. If this is not feasible for your example, then searchspec cannot be referenced from a static context.[/B]

No, according to the book, only SearchSpec.getCommonSpecs() is static. Making this change would probably let it work, but the problem you are having appears to be capitalization, which is an easy fix.
Jason
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: query from Marty Hall's Core Servlets--search engines
 
Similar Threads
Using method on a program
Polymorphic Method Call
Books for WCD
compiling with packages
Simple Servlet Not Working