Section 18.5. Input, Output


18.5. Input, Output

OK, so we've dynamically created a Web pagebut the contents of that page don't change. The real use for servlets comes from having them produce dynamic content, not just from dynamically producing content.

One way for the content to be dynamic is to extract it from a database. Using what we described in Chapter 15, you can add code to pull values from tables in a database. Consider a query that will return multiple rows of results. Each row could be displayed as a row in an HTML table for display on a Web page.

Using a loop, we can generate lots of HTML with little code. This is handy for generating HTML tables. We would likely generate the <table> tag outside a for loop, but the <tr> and <td> tags would be output from within the loop, one for each iteration of the loop. (If you're not picturing that, be patient. There are examples of this coming up. If you're not conversant in HTML, then you better check out some of the HTML references at the end of this chapter. We're going to assume that you speak HTML fluently. Come onwe can't cover everything in one book.)

The other side of dynamic content comes from variable input. Google's search engine, for example, generates different pages for different search strings. It is the variation in user input that results in varying output pages. On a Web page, user input typically comes from an HTML form. The form values can be passed either as parameters on the URL or as POST values. URL parameters are also easy to generate by hand, or to code in place in <a> tags. For example,

 <a href="/servlet/doSuch?cmd=find&value=joe"> 

is an HTML tag for a hyperlink which will invoke the doSuch servlet and pass in the parameters cmd and value. (It's a servlet not because the pathname is /servlet, but we use that for illustrative purposes. In fact, the servlet invoked may not even be called doSuch; it all part of servlet mapping that recognizes certain URLs as aliases for particular servlets. See Chapter 19 for a fuller explanation.)

The point is, we can invoke the same servlet repeatedly (even simultaneously) but with different values for our parameters, so we can program it for different behaviors and different output.

These parameters are available to the servlet via the request argument of the doGet() and doPost() methods. You can get an enumerator over all of the arguments (using getParameterNames()), or if you know it's name (and you likely would, since you're writing the program) you can ask for a particular argument.

The previous example used an argument called cmd, whose value we could retrieve thus:

 String act = request.getParameter("cmd"); 

The parameters all come as Strings. If your arguments are numeric, you'll have to parse them (and error-check themHTML forms are, understandably, weak on validating their input; tons of JavaScript have been written to deal with this, but this is beyond the scope of this book.)

Some parameters may have embedded spaces and other special characters that would disrupt a URL. To deal with that, browsers encode the characters in form fields before sending them to a Web server. You can see that in some URLsspace gets replaced with a "+" character, and special characters (such as the plus sign) get replaced with a character sequence for hexadecimal values (for example, "+" becomes %2B). The getParameter() method will automatically decode those. But we need to remember this if we want to generate any literal URLs in the HTML that we produce. (See the URLEncoder class in the Javadoc documentation for servlets.)

One more annoyance that must be dealt with: What if the URL contains the same argument twicefor 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 situation differently, you can call getParameterValues() which will return an array of Strings for all the different values. In our example,

 String [] allofem = getParameterValues("cmd"); 

will return an array such that:

 allofem[0] = "search" allofem[1] = "bogus" 

If there was only one value, then you get an array of one element. If the parameter wasn't used in the URL, getParameterValues() returns null.



    Java Application Development with Linux
    Java Application Development on Linux
    ISBN: 013143697X
    EAN: 2147483647
    Year: 2004
    Pages: 292

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net