Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
2.4 Packaging Servlets
29
Listing 2.4 HelloWWW2.java
package coreservlets;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWWW2 extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String docType =
"
"Transitional//EN\">\n";
out.println(docType +
"\n" +
"Hello WWW\n" +
"\n" +
"Hello WWW
\n" +
"");
}
}
Compiling Servlets in Packages
There are two main ways to compile classes that are in packages. The first
option is to place your package subdirectory right in the directory where the
Web server expects servlets to go. Then, you would set the
CLASSPATH
vari
able to point to the directory above the one actually containing your servlets,
that is, to the main servlet directory used by the Web server. You can then
compile normally from within the package specific subdirectory. For exam
ple, if your base servlet directory is
C:\JavaWebServer2.0\servlets
and
your package name (and thus subdirectory name) is
coreservlets
, and you
are running Windows, you would do:
DOS>
set CLASSPATH=C:\JavaWebServer2.0\servlets;%CLASSPATH%
DOS>
cd C:\JavaWebServer2.0\servlets\coreservlets
DOS>
javac HelloWorld.java
The first part, setting the
CLASSPATH
, you probably want to do permanently,
rather than each time you start a new DOS window. On Windows 95/98 you
typically put the
set
CLASSPATH=...
statement in your
autoexec.bat
file
somewhere after the line that sets the
CLASSPATH
to point to
servlet.jar
Second edition of this book: www.coreservlets.com; Sequel: www.moreservlets.com.
Servlet and JSP training courses by book's author: courses.coreservlets.com.
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved