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.

406Chapter 18Servlets: Java Pressed into ServiceRather than require fancier browsers with more dynamic querying or other capabilities,Web servers became smarter and were able to talk to other programsthat would generate HTML <strong>on</strong> the fly and send it back as the resp<strong>on</strong>se to anincoming request. In the Java envir<strong>on</strong>ment, this mechanism includes theServlet and related classes.As for requests coming from a browser, they come in two flavors—GETand POST. The GET request is a request via a URL. Simple URLs that appearas hyperlinks <strong>on</strong> a Web page are sent as GET requests. Any additi<strong>on</strong>al parametersappear at the end of the URL as name=value pairs separated by “&”. Theparameters are separated from the URL with a “?” character:http://www.google.com/search?hl=en&ie=ISO-8859-1&q=javaThe example URL includes three parameters:• hl=en• ie=ISO-8859-1• q=javaThe POST is virtually the same, except that the name=value pairs d<strong>on</strong>’tappear <strong>on</strong> the URL but are sent in a less visible way. The net result is the same,and the same methods can be used in the servlet to retrieve the parameters. ThePOST requests typically come from HTML form elements, as when you fill inthe fields of a form and press a submit butt<strong>on</strong> (though forms can specify thatthe browser use GET as the submissi<strong>on</strong> mechanism for a particular form). Thebiggest advantage to posting the form is that the parameters d<strong>on</strong>’t appear inthe URL, which is both more aesthetically pleasing and avoids problems fromaccidentally revisited pages or user-altered parameters.One further twist: URLs are not necessarily literal paths to files anymore.The Web server can interpret parts of the URL as an alias for some other program.So http://www.google.com/search may not actually refer to a directorynamed search <strong>on</strong> the Google site, but more likely tells the Web server touse its search program. We’ll discuss this more in Chapter 19.So servlets are given requests which have come from browsers (and otherWeb clients), and then they resp<strong>on</strong>d with output. In our examples, we’ll besending HTML back. There are lots of other choices, too. Since browsers understandother formats, a servlet might also send back plain text or even imagedata. Another choice gaining popularity is having the servlet generate XML andthen using a c<strong>on</strong>versi<strong>on</strong> via stylesheets to produce HTML. This allows for the

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

Saved successfully!

Ooh no, something went wrong!