Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
2.4 Packaging Servlets
27
Core Approach
Always set the content type
before
transmitting the actual document.
The second step in writing a servlet that builds an HTML document is to
have your
println
statements output HTML, not plain text. The structure of
an HTML document is discussed more in Section 2.5 (Simple HTML Build
ing Utilities), but it should be familiar to most readers. Listing 2.3 gives an
example servlet, with the result shown in Figure 2 2.
Listing 2.3 HelloWWW.java
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();
String docType =
"
"Transitional//EN\">\n";
out.println(docType +
"\n" +
"Hello WWW\n" +
"\n" +
"Hello WWW
\n" +
"");
}
}
2.4 Packaging Servlets
In a production environment, multiple programmers may be developing
servlets for the same server. So, placing all the servlets in the top level servlet
directory results in a massive hard to manage directory and risks name con
flicts when two developers accidentally choose the same servlet name. Pack
ages are the natural solution to this problem. Using packages results in
changes in the way the servlets are created, the way that they are compiled,
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