13.07.2015 Views

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

410Chapter 18Servlets: Java Pressed into Service/*** Returns a short descripti<strong>on</strong> of the servlet.*/public StringgetServletInfo(){return "Very Simple Servlet";} // getServletInfo()} // class HiServletWhew! That is a lot of code for <strong>on</strong>ly a simple “Hello, world,” but rememberthat this is not just a run-<strong>on</strong>-your-desktop applicati<strong>on</strong>. This is a networkbasedservlet that can resp<strong>on</strong>d to c<strong>on</strong>current requests from across the networkand talk to Web browsers. There’s a lot of plumbing that needs to be c<strong>on</strong>nectedto a Web server for the servlet to run, and that’s what most of this code is—justthe c<strong>on</strong>necti<strong>on</strong>s. The other verbose part is all of the HTML that we spit outaround our message. You can make it even more elaborate, with backgroundcolors and other HTML decorati<strong>on</strong>s if you want to try it yourself.Once you’ve written a servlet, though, you can’t just run it from thecommand line like any Java class. 1 Much of the work of a servlet is d<strong>on</strong>e behindthe scenes by the Web server (e.g., Tomcat). The tougher questi<strong>on</strong> is, “Howdo you run a servlet?” That involves issues of c<strong>on</strong>figuring the Web server, settingup directory locati<strong>on</strong>s, and so forth. It’s the subject of the next chapter.Once you’ve deployed this servlet (by reading the next chapter and/or withhelp from your IDE), you can run the servlet and talk to it via your browser.We’ve pointed a browser window at <strong>on</strong>e such deployment to get a highly uninterestingWeb 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 aservlet 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 theclass for simple testing. Of course, such a simple test harness wouldn’t be driving a Webbrowser, and so <strong>on</strong> but technically it is possible. We didn’t want to lie to you.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!