How do I complie a Servlet?

Hello I have downloaded the latest JSDK and I have JDK1.2.1.
I am new to servlets and here is some code that I want to complie and run:


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

public class HelloWWW extends HttpServlet {
  public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
      throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
                                        "Transitional//EN\">\n" +
                "<HTML>\n" +
                "<HEAD><TITLE>Hello WWW</TITLE></HEAD>\n" +
                "<BODY>\n" +
                "<H1>Hello WWW</H1>\n" +
                "</BODY></HTML>");
  }
}

In dos what do I set the classpath to and what do I use to complie it??
Could you please give me a list of instrustions.
Thanks
(I only have 20 points)
LVL 1
dvd99Asked:
Who is Participating?
 
g_senthilkumarConnect With a Mentor Commented:
Hi,
  If you want to compile your servlet program,the compiler (javac)will be able to compile your program.But the thing is the servlet packages you are importing will be available in (for ex)c:\jsdk2.0\lib\jsdk.jar.This file you have to add it in your classpath.Because the java compiler will look the class files which are available in the CLASSPATH entry.
   In dos edit autoexec.bat file and give like this:
 set CLASSPATH=
..;c:\jsdk2.0\lib\jsdk.jar;c:\jdk1.2.1\lib\dt.jar;...(like that 1.2.1 having more than one jar file,specify all the files).Then save it and execute the autoexec.bat file.
  In windows,you right click
"mycomputer" icon and select Environment there also you can set CLASSPATH environment,in the value field give the full path  jsdk.jar file name.
   This is the way you can set the classpath after that compile your program.And then go to "examples" directory within c:\jsdk2.0 directory.
There servlet.properties file will be there.Register your servlet name within that file.Copy the servlet class file within that directory.And then goto the browser give http://yourmachine:8080/servlet/MyServlet.
   This way you can see the result also.
0
 
Jan LouwerensSoftware EngineerCommented:
You still use javac to compile it, just like any other java program. You will have to add the servlet jar file (sorry, I don't remember the file name or path - probably something like jsdk.jar) to your classpath. Sorry I don't have specific instructions, but it should be enough to get you started...
0
 
Ravindra76Commented:

add  the folowing directory ( IF you are uisng windoiws )

in path variable

c:\jsdkhome\bin;

add this in

classpath variable

c:\jsdkhome\lib\jsdk.jar

where jsdkhome is your jsdk installed directory.

Best of luck



0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
amitdCommented:
just add the class path to system as :c:\jsdkhome\lib\jsdk.jar
0
 
amitdCommented:
ok boss no matter if u don't give point for that should get ur basic doubts
clearified
for compiling go to console
go to directory where u have stored the file
give command as javac filename.java

then while running type command as java filename(actually class name is given)
for e.g if my file name is
servlet.java then for compling
javac servlet.java
for running give
java servlet
BestLuck
0
 
rjackmanCommented:
hey man as said by amitd add c:\jsdkhome\bin;c:\jsdkhome\lib\jsdk.jar

to the path then go to console and execute the commands given by our friend
---Rjackman
0
 
g_senthilkumarCommented:
Hi,
  Even if u unable to set the classpath,you can specify the place of jar file as a argument to java compiler like this.
javac -classpath C:\jsdk2.0lib\jsdk.jar  Serv1.java

   Try this also
0
 
rjackmanCommented:
hey dvd u got the answers prior to tis also
hope ur doubt is clarified
0
 
dvd99Author Commented:
This might help you more to help me!! :)


I am having trouble compling this servlet.
I set my classpath like this:

set classpath=%%classpath%%;c:\jsdk2.0\lib\jsdk.jar

But when I complie this code below,

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

public class HelloWorld extends HttpServlet {
  public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
      throws ServletException, IOException {
    PrintWriter out = response.getWriter();
    out.println("Hello World");
  }
}

I get this error,

HelloWorld.java:2: Package javax.servlet not found in import.
import javax.servlet.*;
       ^
HelloWorld.java:3: Package javax.servlet.http not found in import.
import javax.servlet.http.*;
       ^
HelloWorld.java:5: Superclass HttpServlet of class HelloWorld not found.
public class HelloWorld extends HttpServlet {
                                ^
3 errors
Exit code: 1
There were errors

I'm using jdk1.2.1 (i gonna get java3!) and jsdk2.0.
Thanks I hope you can help!!
0
 
Jan LouwerensSoftware EngineerCommented:
Are you absolutely sure that the path C:\jsdk2.0\lib\jsdk.jar is correct?

Also, are you sure that the classpath is set correctly? type "SET" on the command line and make sure that CLASSPATH is set correctly.

I don't see any other reason why it wouldn't be working for you...
0
 
g_senthilkumarCommented:
Hi
  if u are using jsdk2.0 means the jsdk.jar onlly you have to set it in the classpath.Why dont you try this one:
 javac -classpath c:\jsdk2.0\lib\jsdk.jar Helloworld,
   assume that you installed in the c:\jsdk2.0 directory.If you have installed in some other directory give that name.
  Try this and let us know the result.
0
All Courses

From novice to tech pro — start learning today.