aspose file tools*
The moose likes Servlets and the fly likes Compilation Error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Compilation Error" Watch "Compilation Error" New topic
Author

Compilation Error

Kerry Wilson
Ranch Hand

Joined: Oct 29, 2003
Posts: 254
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.


http://www.goodercode.com
SCJP 1.4
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

What about the servlet jar file.Are they in the CLASSPATH?


Groovy
Kerry Wilson
Ranch Hand

Joined: Oct 29, 2003
Posts: 254
The classpath variable is dynamically set by Tomcat upon compilation. I am wondering if i have the correct java installed on my machine.
Tim Baker
Ranch Hand

Joined: Oct 04, 2003
Posts: 541
i know you shouldnt have to but what error do you get if you cast your second string to an object


Kim Jong II (North Korea's Dear Leader) said:Nuclear weapons don't kill people, people kill people.
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

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

Joined: Oct 04, 2003
Posts: 541
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

Joined: Oct 29, 2003
Posts: 254
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

Joined: Oct 29, 2003
Posts: 254
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

Joined: Oct 04, 2003
Posts: 541
you need to have the servlet packages on your computer yes, and these do come in j2ee
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61193
    
  66

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 ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Kerry Wilson
Ranch Hand

Joined: Oct 29, 2003
Posts: 254
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61193
    
  66

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 ]
sunitha reghu
Ranch Hand

Joined: Dec 12, 2002
Posts: 937
You can also IDE like eclipse which compile for you and then put the class file.
Craig Jackson
Ranch Hand

Joined: Mar 19, 2002
Posts: 405
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
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
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...


Co-author of SCMAD Exam Guide, Author of JMADPlus
SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
Kerry Wilson
Ranch Hand

Joined: Oct 29, 2003
Posts: 254
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

Joined: Oct 29, 2003
Posts: 254
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

Joined: Oct 29, 2003
Posts: 254
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Compilation Error