"Can't find method getOutputStream()" and "Can't find method setContentType(Ljava/lang/String;)"

Hey,

I'm new to Tomcat and I'm trying to develop an application.

Rot13.java (it's just a simple web app):
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Rot13 extends HttpServlet {
        public void doGet(HttpServletRequest res, HttpServlet req) throws ServletException, IOException {
                res.setContentType("text/html");
                ServletOutputStream out = res.getOutputStream();
                out.println("lol @ you");
        }
}

However, when I compile it (by typing gcj Rot13.java), I get:
Rot13.java: In class 'Rot13':
Rot13.java: In method 'Rot13.doGet(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServlet)':
Rot13.java:7: error: Can't find method âsetContentType(Ljava/lang/String;)â in type âjavax.servlet.http.HttpServletRequestâ.
                res.setContentType("text/html");
                      ^
Rot13.java:8: error: Can't find method âgetOutputStream()â in type âjavax.servlet.http.HttpServletRequestâ.
                ServletOutputStream out = res.getOutputStream();
                                                ^
2 errors

My CLASSPATH is:
/usr/share/tomcat5/common/lib/servlet-api.jar:.:/usr/share/tomcat5/webapps/rot13/WEB-INF/classes:/usr/share/tomcat5/server/lib/servlets-common.jar

Can anybody help me ?

Many thanks,


J
LVL 2
jonnykeoghAsked:
Who is Participating?
 
Computer101Commented:
PAQed with points refunded (500)

Computer101
EE Admin
0
 
asood314Commented:
You're getting those errors because the HttpServletRequest class does not have methods getOuputStream() or setContentType().
0
 
jonnykeoghAuthor Commented:
I've found the error and fixed it, however it still doesn't compile. The source code is now:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Rot13 extends HttpServlet {
        public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
                res.setContentType("text/html");
                ServletOutputStream out = res.getOutputStream();
                out.println("lol @ you");
        }
}


The errors when compile (with gcj Rot13.class):
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../lib/crt1.o: In function `_start':
../sysdeps/i386/elf/start.S:115: undefined reference to `main'
/tmp/ccmKdcWs.o: In function `void Rot13::doGet(javax::servlet::http::HttpServletRequest*, javax::servlet::http::HttpServletResponse*)':
Rot13.java:(.text+0x1c): undefined reference to `javax::servlet::http::HttpServletResponse::class$'
Rot13.java:(.text+0x4d): undefined reference to `javax::servlet::http::HttpServletResponse::class$'
/tmp/ccmKdcWs.o: In function `Rot13::Rot13()':
Rot13.java:(.text+0x91): undefined reference to `javax::servlet::http::HttpServlet::HttpServlet()'
/tmp/ccmKdcWs.o:(.data+0x28): undefined reference to `void javax::servlet::GenericServlet::destroy()'
/tmp/ccmKdcWs.o:(.data+0x2c): undefined reference to `java::lang::String* javax::servlet::GenericServlet::getInitParameter(java::lang::String*)'
/tmp/ccmKdcWs.o:(.data+0x30): undefined reference to `java::util::Enumeration* javax::servlet::GenericServlet::getInitParameterNames()'
/tmp/ccmKdcWs.o:(.data+0x34): undefined reference to `javax::servlet::ServletConfig* javax::servlet::GenericServlet::getServletConfig()'
/tmp/ccmKdcWs.o:(.data+0x38): undefined reference to `javax::servlet::ServletContext* javax::servlet::GenericServlet::getServletContext()'
/tmp/ccmKdcWs.o:(.data+0x3c): undefined reference to `java::lang::String* javax::servlet::GenericServlet::getServletInfo()'
/tmp/ccmKdcWs.o:(.data+0x40): undefined reference to `void javax::servlet::GenericServlet::init(javax::servlet::ServletConfig*)'
/tmp/ccmKdcWs.o:(.data+0x44): undefined reference to `void javax::servlet::GenericServlet::init()'
/tmp/ccmKdcWs.o:(.data+0x48): undefined reference to `void javax::servlet::GenericServlet::log(java::lang::String*)'
/tmp/ccmKdcWs.o:(.data+0x4c): undefined reference to `void javax::servlet::GenericServlet::log(java::lang::String*, java::lang::Throwable*)'
/tmp/ccmKdcWs.o:(.data+0x50): undefined reference to `void javax::servlet::http::HttpServlet::service(javax::servlet::ServletRequest*, javax::servlet::ServletResponse*)'
/tmp/ccmKdcWs.o:(.data+0x54): undefined reference to `java::lang::String* javax::servlet::GenericServlet::getServletName()'
/tmp/ccmKdcWs.o:(.data+0x5c): undefined reference to `long long javax::servlet::http::HttpServlet::getLastModified(javax::servlet::http::HttpServletRequest*)'
/tmp/ccmKdcWs.o:(.data+0x60): undefined reference to `void javax::servlet::http::HttpServlet::doHead(javax::servlet::http::HttpServletRequest*, javax::servlet::http::HttpServletResponse*)'
/tmp/ccmKdcWs.o:(.data+0x64): undefined reference to `void javax::servlet::http::HttpServlet::doPost(javax::servlet::http::HttpServletRequest*, javax::servlet::http::HttpServletResponse*)'
/tmp/ccmKdcWs.o:(.data+0x68): undefined reference to `void javax::servlet::http::HttpServlet::doPut(javax::servlet::http::HttpServletRequest*, javax::servlet::http::HttpServletResponse*)'
/tmp/ccmKdcWs.o:(.data+0x6c): undefined reference to `void javax::servlet::http::HttpServlet::doDelete(javax::servlet::http::HttpServletRequest*, javax::servlet::http::HttpServletResponse*)'
/tmp/ccmKdcWs.o:(.data+0x70): undefined reference to `void javax::servlet::http::HttpServlet::doOptions(javax::servlet::http::HttpServletRequest*, javax::servlet::http::HttpServletResponse*)'
/tmp/ccmKdcWs.o:(.data+0x74): undefined reference to `void javax::servlet::http::HttpServlet::doTrace(javax::servlet::http::HttpServletRequest*, javax::servlet::http::HttpServletResponse*)'
/tmp/ccmKdcWs.o:(.data+0x78): undefined reference to `void javax::servlet::http::HttpServlet::service(javax::servlet::http::HttpServletRequest*, javax::servlet::http::HttpServletResponse*)'
/tmp/ccmKdcWs.o:(.data+0x110): undefined reference to `javax::servlet::http::HttpServlet::class$'
collect2: ld returned 1 exit status


J
0
 
asood314Commented:
> The errors when compile (with gcj Rot13.class):

it should be gcj Rot13.java
0
 
jonnykeoghAuthor Commented:
Sorry, that's what I meant.

I've solved this, and it all compiles correctly. I was using the wrong application to compile it. I should have been using javac instead of gcj.

I appreciate all your help, but I figured it out on my own!

Can a moderator close this question please?


J
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.