Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
2.2 A Simple Servlet Generating Plain Text
23
2.2 A Simple Servlet Generating
Plain Text
Listing 2.2 shows a simple servlet that just generates plain text, with the out
put shown in Figure 2 1. Section 2.3 (A Servlet That Generates HTML)
shows the more usual case where HTML is generated. However, before mov
ing on, it is worth spending some time going through the process of installing,
compiling, and running this simple servlet. You'll find this a bit tedious the
first time you try it. Be patient; since the process is the same each time, you'll
quickly get used to it, especially if you partially automate the process by
means of a script file such as that presented in the following section.
Listing 2.2 HelloWorld.java
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");
}
}
Figure 2 1
Result of Listing 2.2 (HelloWorld.java).
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