• 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

Compilation Error

 
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I compile my servlet I receive the following error:
servletToJsp.java:11:
servletToJsp.java:11: error:Cannot find method "setAttribute(java.lang.String, java.lang.String)"
Here is my code:

I do not think there is anything wrong with the code. I am just trying to figure out how to get these things working. Anybody know what the problem may be. I have no problems with Beans or JSP's .
This is my java version: jdk1.3.1_09
Any help is greatly appreciated. Thanks.
 
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What about the servlet jar file.Are they in the CLASSPATH?
 
Kerry Wilson
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The classpath variable is dynamically set by Tomcat upon compilation. I am wondering if i have the correct java installed on my machine.
 
Ranch Hand
Posts: 541
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i know you shouldnt have to but what error do you get if you cast your second string to an object
 
Pradeep bhatt
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tim Baker:
i know you shouldnt have to but what error do you get if you cast your second string to an object


Why do we need to cast? String is a subclass of Object.
 
Tim Baker
Ranch Hand
Posts: 541
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i want to see if the error is the same. it is strange that it would complain on this object. if the servlet classes aren't being imported it should complain before hand and it is looking for a method string, string when it should find string, object. maybe the string being fixed is cuasing a problem. i'm just wanting to see what error we get.
 
Kerry Wilson
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did not cast it to string but I commented out the offending line and I got the message:
servletToJsp.java:16: error:
Cannot find method "getRequestDispatcher(java.lang.String)"
It seems to not find anything, I have already successfully compiled javaBeans and .jsp is working
 
Kerry Wilson
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I cannot compile servlets at all it keeps printing this message. Do I need a certain java kit like J2EE? I have J2SE
 
Tim Baker
Ranch Hand
Posts: 541
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you need to have the servlet packages on your computer yes, and these do come in j2ee
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How are you compiling your servlets? Are you sure that the servlet.jar is in the classpath? Tomcat will handle the classpath for you at run time (and for compiling JSPs) but won't help you at servlet build time.
I'd highly recommend the use of Ant to manage your build process.
You don't need j2ee unless you are using EJB's. The servlet.jar that came with Tomcat will suffice nicely; just be sure it's in your build-time classpath.
bear
[ November 13, 2003: Message edited by: Bear Bibeault ]
 
Kerry Wilson
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where does the servlet.jar file go?
I have it in the WEB-INF/classes folder. Which is where my servlet goes.
And How would I access it if I am correct it should be: http://domainname/context/servlet/servletName.
Where do you get Ant? I am working on a remote server does it support this?
Sorry for all the questions I just want to learn something else besides PHP w/o going to the evil empire. HA! thanks for any help.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I have it in the WEB-INF/classes folder. Which is where my servlet goes.


It should already be in your Tomcat installation. You do not need to copy it anywhere else or include it in the WEB-INF of your web application.


And How would I access it if I am correct it should be: http://domainname/context/servlet/servletName.


You need to clue us in on how you are building your servlets. It alsmost soounds as if you expect Tomcat to compile your servlet for you in the same way that it does for JSPs. Not so. You need to compile your serlvets, beans and other Java classes, and place the resulting package hierarchy under the WEB-INF/classes folder.
Hence Ant to help manage this process.


Where do you get Ant? I am working on a remote server does it support this?


Get Ant here.
bear
[ November 13, 2003: Message edited by: Bear Bibeault ]
 
Ranch Hand
Posts: 937
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can also IDE like eclipse which compile for you and then put the class file.
 
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the reason why you are getting this particular error is that you have changed the signature of the method. The doGet() method should be implemented with the method throwing a ServletException and IOException.

What do you think?
Craig
 
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by cj jack:
I think the reason why you are getting this particular error is that you have changed the signature of the method. The doGet() method should be implemented with the method throwing a ServletException and IOException.

What do you think?
Craig


No, I think you misunderstand sthing related to the signature of the doGet method... We can surely make the method access to be more public(not less than protected)... This is Java's OOP Overriding technique... U will understand more, if you review the overriding method topic in Java...
 
Kerry Wilson
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the complete code:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorldServlet extends HttpServlet
{
protected void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, java.io.IOException {
String theMessage = "Hello, World";
String target = "debug/helloFromServlet.jsp";
req.setAttribute("message", theMessage);
RequestDispatcher rd;
rd = getServletContext().getRequestDispatcher(target);
rd.forward(req, res);
}
}
the error:
HelloWorldServlet.java:11:
error:Cannot find method "setAttribute(java.lang.String, java.lang.String)"
This is from a sample in a book and it will not compile. I am perplexed.
 
Kerry Wilson
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe I have something setup wrong. My envs as follows:
servlet.jar location:
/usr/local/tomcat/common/lib/
-and-
/var/www/java_webapps/test/WEB-INF/lib
$JAVA_HOME:
/usr/local/java
Again, I have successfully compiled beans, but no servlets.
OK, I now know it is something to do with the servlet.jar location
When I set CLASSPATH=/usr/local/tomcat/common/lib/ and ran the instruction
javac -classpath $CLASSPATH HelloWorldServlet.java
I got this error:

HelloWorldServlet.java:7: error:Cannot find class "HttpServletRequest" [JLS 8]
HelloWorldServlet.java:7: error:Cannot find class "HttpServletResponse" [JLS 8]
HelloWorldServlet.java:7: error:Cannot find class "ServletException" [JLS 8]

So, How should I handle this problem? Where do you put servlet.jar such that you do not need to deal with the classpath?
Huge thanks to all that have/will help.
[ November 14, 2003: Message edited by: Kerry Wilson ]
 
Kerry Wilson
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In case anyone is having this problem here is how I fixed it.
I set
CLASSPATH=/usr/java/jdk1.3.1_09/jre/lib/rt.jar:/usr/java/jdk1.3.1_09/lib/tools.jar:/usr/local/tomcat/common/lib/servlet.jar
And executed javac -classpath $CLASSPATH Servlet.java
Thanks for the help.
Now to see if I can actually see it in a browser.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic