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.

18.6 Matters of State: Cookies, Hidden Variables, and the Dreaded “Back” Butt<strong>on</strong>413in form fields before sending them to a Web server. You can see that in someURLs—space gets replaced with a “+” character, and special characters (suchas the plus sign) get replaced with a character sequence for hexadecimal values(for example, “+” becomes %2B ). The getParameter() method will automaticallydecode those. But we need to remember this if we want to generate anyliteral URLs in the HTML that we produce. (See the URLEncoder class in theJavadoc documentati<strong>on</strong> for servlets.)One more annoyance that must be dealt with: What if the URL c<strong>on</strong>tainsthe same argument twice—for example, www.google.com/search?cmd=search&cmd=bogus?If you make the call to getParameter() you will get the first value(search). If you want to handle such a situati<strong>on</strong> differently, you can callgetParameterValues() which will return an array of Strings for all thedifferent values. In our example,String [] allofem = getParameterValues("cmd");will return an array such that:allofem[0] = "search"allofem[1] = "bogus"If there was <strong>on</strong>ly <strong>on</strong>e value, then you get an array of <strong>on</strong>e element. If theparameter wasn’t used in the URL, getParameterValues() returns null.18.6MATTERS OF STATE: COOKIES, HIDDEN VARIABLES,AND THE DREADED “BACK” BUTTONThe toughest part about working with HTML is, perhaps, its statelessness.HTML and browsers were not designed to keep a c<strong>on</strong>necti<strong>on</strong> going. It’s not aph<strong>on</strong>e call type of c<strong>on</strong>necti<strong>on</strong>, where the line is kept open between the browserand the Web server. Rather, it’s a <strong>on</strong>e-shot, send-me-what-you’ve-got mechanismmore like postal mail (but without the stamp). Here’s the rub: Just becauseyou mail a letter, you can’t assume that you’ll get an answer back. Thereis no <strong>on</strong>-going c<strong>on</strong>necti<strong>on</strong> between browser and server, except for the durati<strong>on</strong>of the data transfer. Once you’ve got your complete page displayed, the

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

Saved successfully!

Ooh no, something went wrong!