I can't compile a very simple servlet code.

V

vectrum

Hello,

I can't compile the following simple servlet code. Please have a look
at it. I have started learning java and using java 2 (JDK 1.2 and JSDK 2.0)
because

all the book I follow based on java 2 so I installed aforementioned
packages.

Another reason of using old packages is that JSDK2.0 comes with
servletrunner.exe.

Running and testing simple servlet codes using servletrunner.exe is easy
than

testing with big webservers.

I tried to compile the folowing servlet code; (Filename is HelloServlet.java
and

tried javac HelloServlet.java)

import java.io.*;
import javax.servlet.*;

public class HelloServlet extends GenericServlet
{
public void service(ServletRequest request, servletResponse response)
throws ServletException, IOException {
response.SetContentType("text/html");
PrintWriter pw = response.getWriter();
pw.println("<B>Hello World");
pw.close();
}
}

using two different paths and classpaths. First I tried to compile from c:\
and get

two following errors;

C:\>javac HelloServlet.java

HelloServlet.java:2: Package javax.servlet not found in import.
import javax.servlet.*;
^
HelloServlet.java:4: Superclass GenericServlet of class HelloServlet not
found.
public class HelloServlet extends GenericServlet
^
2 errors

My path setting was;

PATH=C:\jdk1.2\bin;C:\JSDK2.0\bin;C:\JSDK2.0\lib\jsdk.jar;D:\BC5\BIN;C:\TASM
\BIN;%PA

TH%

SET CLASSPATH=.;C:\myjava;----myjava is the directory I created.

Second time, when I tried within myjava, the directory I created,stored

HelloServlet.java file in it and get four following errors;

HelloServlet.java:4: class HelloServlet must be declared abstract. It does
not
define void service(javax.servlet.ServletRequest,
javax.servlet.ServletResponse)
from class javax.servlet.GenericServlet.
public class HelloServlet extends GenericServlet
^
HelloServlet.java:6: Class servletResponse not found.
public void service(ServletRequest request, servletResponse response)
^
HelloServlet.java:8: Class servletResponse not found in void
service(javax.servl
et.ServletRequest, servletResponse).
response.SetContentType("text/html");
^
HelloServlet.java:9: Class servletResponse not found in void
service(javax.servl
et.ServletRequest, servletResponse).
PrintWriter pw = response.getWriter();
^
4 errors

Then path was;

PATH=C:\jdk1.2\bin;C:\JSDK2.0\bin;C:\JSDK2.0\lib\jsdk.jar;D:\BC5\BIN;C:\TASM
\BIN;%PA

TH%
SET
CLASSPATH=.;C:\myjava;C:\jdk1.2\bin;C:\JSDK2.0\bin;C:\JSDK2.0\lib\jsdk.jar;



I can compile simple console level java codes and applets but can't compile
this.
Please tell me how to compile that code and what I should do in future.
Mypost is getting longer but I am really frustrated and I think I should
describe in detail what I did.
Thanking you in anticipation,
Regards,
 
R

Ryan Stewart

vectrum said:
Hello,

I can't compile the following simple servlet code. Please have a look
at it. I have started learning java and using java 2 (JDK 1.2 and JSDK 2.0)
because

all the book I follow based on java 2 so I installed aforementioned
packages.

Just a thought: You don't mention J2EE anywhere in your post. If you don't
have that, you don't have servlets.
 
J

Jose Rubio

vectrum said:
Hello,

I can't compile the following simple servlet code. Please have a look
at it. I have started learning java and using java 2 (JDK 1.2 and JSDK 2.0)
because

all the book I follow based on java 2 so I installed aforementioned
packages.

Another reason of using old packages is that JSDK2.0 comes with
servletrunner.exe.

Running and testing simple servlet codes using servletrunner.exe is easy
than

testing with big webservers.

I tried to compile the folowing servlet code; (Filename is HelloServlet.java
and

tried javac HelloServlet.java)

import java.io.*;
import javax.servlet.*;

public class HelloServlet extends GenericServlet
{
public void service(ServletRequest request, servletResponse response)
throws ServletException, IOException {
response.SetContentType("text/html");
PrintWriter pw = response.getWriter();
pw.println("<B>Hello World");
pw.close();
}
}

using two different paths and classpaths. First I tried to compile from c:\
and get

two following errors;

C:\>javac HelloServlet.java

HelloServlet.java:2: Package javax.servlet not found in import.
import javax.servlet.*;
^
HelloServlet.java:4: Superclass GenericServlet of class HelloServlet not
found.
public class HelloServlet extends GenericServlet
^
2 errors

My path setting was;

PATH=C:\jdk1.2\bin;C:\JSDK2.0\bin;C:\JSDK2.0\lib\jsdk.jar;D:\BC5\BIN;C:\TASM
\BIN;%PA

TH%

SET CLASSPATH=.;C:\myjava;----myjava is the directory I created.

Second time, when I tried within myjava, the directory I created,stored

HelloServlet.java file in it and get four following errors;

HelloServlet.java:4: class HelloServlet must be declared abstract. It does
not
define void service(javax.servlet.ServletRequest,
javax.servlet.ServletResponse)
from class javax.servlet.GenericServlet.
public class HelloServlet extends GenericServlet
^
HelloServlet.java:6: Class servletResponse not found.
public void service(ServletRequest request, servletResponse response)
^
HelloServlet.java:8: Class servletResponse not found in void
service(javax.servl
et.ServletRequest, servletResponse).
response.SetContentType("text/html");
^
HelloServlet.java:9: Class servletResponse not found in void
service(javax.servl
et.ServletRequest, servletResponse).
PrintWriter pw = response.getWriter();
^
4 errors

Then path was;

PATH=C:\jdk1.2\bin;C:\JSDK2.0\bin;C:\JSDK2.0\lib\jsdk.jar;D:\BC5\BIN;C:\TASM
CLASSPATH=.;C:\myjava;C:\jdk1.2\bin;C:\JSDK2.0\bin;C:\JSDK2.0\lib\jsdk.jar;



I can compile simple console level java codes and applets but can't compile
this.
Please tell me how to compile that code and what I should do in future.
Mypost is getting longer but I am really frustrated and I think I should
describe in detail what I did.
Thanking you in anticipation,
Regards,
You need to include the servlet.jar to the classpath. Those classes are not
part of the standard sdk distribution. This is normally found when you
download a servlet container like Tomcat or a full blown J2EE app server.
 
J

Jon Skeet

Jose Rubio said:
You need to include the servlet.jar to the classpath. Those classes are not
part of the standard sdk distribution. This is normally found when you
download a servlet container like Tomcat or a full blown J2EE app server.

servlet.jar comes from a later version of the servlet package. The OP
*does* have jsdk.jar in his classpath.

Vectrum: do you actually *have* a file c:\JSDK2.0\lib\jsdk.jar?
That would be the first thing to check.
 
R

Ryan Stewart

Sudsy said:
Um, not quite. All you need is a servlet container, like the reference
version known as Tomcat (<http://jakarta.apache.org/tomcat>). There are
also other possibilities shy of a full-blown J2EE server.

Right, sorry. What I meant was at least some of the J2EE libraries in some
form. I didn't think long enough about that post.
 
V

vectrum

I would like to express my my best wishes to to u all.
Thank u very much Ryan, Jose, Jon and Sudsy. Thanks for your suggestions. I
have solved the problem I want u
to check.. The servlet code HelloServlet has typos (I wrote SetContenType..
instead of setContenType..)and
I had set path and classpath incorrectly. And yes jsdk.jar is important.
It was not in the path. Please check the code and path below,
import java.io.*;
import javax.servlet.*;
public class HelloServlet extends GenericServlet
{
public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
pw.println("<B>Hello world from HelloServlet");
pw.close();
}
}
I have set my path and classpath as
PATH=C:\jdk1.2\bin;C:\JSDK2.0\bin;C:\JSDK2.0\lib;D:\BC5\BIN;C:\TASM\BIN;%PAT
H%
SET CLASSPATH=.;C:\myjava;

Also I had to place HelloServlet.java in example directory which is in
jsdk2.0 otherwise when I run servletrunner
and ie using http://localhost:8080/servlet/HelloServlet I get
error message "page not found".

Regards and Thanks again
:)
 
T

Tony Morris

I suspect you want to inherit from HttpServlet (not GenericServlet), and
override doGet/doPost/doXxx to handle HTTP requests.

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,191
Latest member
BuyKetoBeez

Latest Threads

Top