Java Reference
In-Depth Information
/**
* Returns a short description of the servlet.
*/
public String
getServletInfo()
{
return "Very Simple Servlet";
} // getServletInfo()
} // class HiServlet
Whew! That is a lot of code for only a simple “Hello, world,” but remem-
ber that this is not just a run-on-your-desktop application. This is a network-
based servlet that can respond to concurrent requests from across the network
and talk to Web browsers. There's a lot of plumbing that needs to be connected
to a Web server for the servlet to run, and that's what most of this code is—just
the connections. The other verbose part is all of the HTML that we spit out
around our message. You can make it even more elaborate, with background
colors and other HTML decorations if you want to try it yourself.
Once you've written a servlet, though, you can't just run it from the
command line like any Java class. 1 Much of the work of a servlet is done behind
the scenes by the Web server (e.g., Tomcat). The tougher question is, “How
do you run a servlet?” That involves issues of configuring the Web server, set-
ting up directory locations, and so forth. It's the subject of the next chapter.
Once you've deployed this servlet (by reading the next chapter and/or with
help from your IDE), you can run the servlet and talk to it via your browser.
We've pointed a browser window at one such deployment to get a highly unin-
teresting Web page (Figure 18.2) whose HTML source (in your browser menu,
select View > Page Source ) is shown in Figure 18.3.
1. Well, actually, you could if it had a main() method defined. Our example doesn't, but a
servlet class is still a Java class, and you might define a public static void main()
method that would allow you to run it from the command line as a way to drive the rest of the
class for simple testing. Of course, such a simple test harness wouldn't be driving a Web
browser, and so on but technically it is possible. We didn't want to lie to you.
Search WWH ::




Custom Search