In this excerpt from chapter 4 of Joel Murach's Java Servlets and JSP, you'll learn how to develop a web application that consists of HTML pages and JavaServer Pages (JSPs). As you will see, JSPs work fine as long as the amount of processing that's required for each page is limited. When you complete this chapter, you should be able to use JSPs to develop simple web applications of your own.
To retrieve multiple values for one parameter name, you can use the getParameterValues method as illustrated by the second example. This method is useful for controls like list boxes that allow multiple selections. After you use the getParameterValues method to return an array of String objects, you can use a loop to get the values from the array.
To get the names of all the parameters sent with a request, you can use the getParameterNames method to return an Enumeration object that contains the names. Then, you can search through the Enumeration object to get the parameter names, and you can use the getParameter method to return the value for each parameter name. This is illustrated by the third example.
If you're not familiar with the Enumeration class, you can learn more about it though the API. For most purposes, though, you only need to know that an Enumeration object is a collection that can be searched element by element. To determine if more elements exist in the collection, you can use the hasMoreElements method, which returns a boolean value. And to get the next element in the collection, you can use the nextElement method.
Where and How to Save a JSP
As Figure 6 shows, you normally save the JSPs of an application in the same directory that you use for the HTML pages of the application. The difference is that the name for a JSP requires a jsp extension. So if you're using a text editor that's not designed for working with JSPs, you may have to place the filename in quotation marks to make sure the file is saved with the jsp extension.
JSPs are normally saved in the same directory as the HTML pages. This directory should be a subdirectory of the web applications directory for your server. If you're running Tomcat on your PC, that directory is usually c:tomcatwebapps or c:jakarta-tomcatwebapps.
For the first 16 chapters of this book, the document root directory for all applications is the murach directory. As a result, the HTML and JSP files for each application are stored in this directory or one of its subdirectories.
If you're using Tomcat on your local system, you can also use webappsROOT as the root directory for your applications. The ROOT directory is automatically set up when you install Tomcat, and it is the default document root directory.
To make sure that the filename for a JSP is saved with the jsp extension when you're using an HTML or text editor, you can enter the filename within quotes.
Like HTML pages, JSPs must be saved in a directory that's available to the web server. For Tomcat 4.0, you can use any directory under the webapps directory. The root directory for the web applications that are presented in the first 16 chapters of this book is the webappsmurach directory, and the subdirectory for the Email List application that's presented in this chapter is email4. Note, however, that you can also use the webappsROOT directory that Tomcat sets up as the default root directory. Or, you can create your own subdirectories under the webapps directory.
Remember: this is from chapter four ofJoel Murach's Java Servlets and JSP (Mike Murach & Associates, ISBN 1890774189, 2003). Grab a copy at your favorite book store today!