File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
preeti dengri
Ranch Hand
Posts: 111
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1309
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 111
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1309
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6450
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6450
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic