Handling the client
request to form Data If you?ve ever used a search engine, visited an on-line bookstore, tracked stocks on the Web, or asked a Web-based site for quotes on
plane tickets, you?ve probably seen funny-looking URLs like
http://host/path?user=Marty+Hall&origin=bwi&dest=lax. The part after the question mark (i.e., user=Marty+Hall&origin=
bwi&dest=lax) is known as form data (or query data) and is the most common
way to get information from a Web page to a server-side program. Form data can be attached to the end of the URL after a question mark (as
above), for GET requests, or sent to the server on a separate line, for POST
requests. One of the nice features of servlets is that all of this form parsing is handled
automatically. You simply call the getParameter method of the Http-
ServletRequest, supplying the case-sensitive parameter name as an argument. You use getParameter exactly the same way when the data is sent by
GET as you do when it is sent by POST.
to using JSP HTML Form This chapter discusses using HTML forms as front ends to servlets or other server-side programs. These forms provide simple and reliable
user interface controls to collect data from the user and transmit it to the servlet. The following chapter discusses the use of applets as servlet front
ends. Using applets in this role requires considerably more effort and has some
security limitations. HTML forms let you create a variety of user interface controls to collect
input on a Web page. Each of the controls typically has a name and a value, where the name is specified in the HTML and the value comes either from
the HTML or by means of user input. The entire form is associated with the URL of a program that will process the data, and when the user submits
the form the names and values of the controls are sent to the designated URL as a string of the form.
The JSP page Directive: Structuring Generated Servlets A JSP directive affects the overall structure of the servlet that results
from the JSP page. The following templates show the two possible forms for directives. Single quotes can be substituted for the double
quotes around the attribute values, but the quotation marks cannot be omitted
altogether. In JSP, there are three types of directives: page, include, and
taglib. The page directive lets you control the structure of the servlet by importing
classes, customizing the servlet super class, setting the content type, and the
like. A page directive can be placed anywhere within the document; its use is
the topic of this chapter. The second directive, include, lets you insert a file
into the servlet class at the time the JSP file is translated into a servlet.
JSP Scripting element JavaServer Pages (JSP) technology enables you to mix regular, static HTML with dynamically generated content from servlets. You simply
write the regular HTML in the normal manner, using familiar Web-page-building tools. You then enclose the code for the dynamic
parts in special tags, most of which start with <% and end with. Separating the static HTML from the dynamic content provides a number
of benefits over servlets alone, and the approach used in JavaServer Pages offers several advantages over competing technologies such as ASP,
PHP, or ColdFusion. Section 1.4 gives some details on these advantages, but they basically boil down to two facts: that JSP is widely supported
and thus doesn?t lock you into a particular operating system or Web server and that JSP gives you full access to servlet and Java technology for the
dynamic part, rather than requiring you to use an unfamiliar and weaker special-
integrating servlet and JSP Servlets are great when your application requires a lot of real programming
track sessions, save information between requests, compress pages, access databases, generate GIF images on-the-fly, and perform many other tasks
flexibly and efficiently. But, generating HTML with servlets can be tedious and can yield a result that is hard to modify. That?s where JSP comes in; it lets
you separate much of the presentation from the dynamic content. That way, you can write the HTML in the normal manner, even using HTML-specific
tools and putting your Web content developers to work on your JSP documents.
JSP expressions, scriptlets, and declarations let you insert simple Java code into the servlet that results from the JSP page, and directives let you
control the overall layout of the page. For more complex requirements, you can wrap up Java code inside beans or define your own JSP tags.
The including files and applets in JSP
Documents JSP has three main capabilities for including external pieces into a JSP
document. The include directive lets you reuse navigation bars, tables, and other elements in multiple pages. The included elements can contain JSP code and thus are inserted into the page before the page is translated into a
servlet. This capability is discussed in Section 12.1. Although including external pieces that use JSP is a powerful capability,
other times you would rather sacrifice some power for the convenience of
being able to change the included documents without updating the main JSP page.
Although this book is primarily about server-side Java, client-side Java in the form of Web-embedded applets continues to play a role, especially within
fast corporate intranets. The jsp:plugin element is used to insert applets that use the Java Plug-In into JSP pages.
The Core Servlet and JSP
Second edition Thorough guide to Web application development with JSP 2.0 and servlets 2.4. Detailed treatment of form processing, HTTP, cookies, session tracking, JDBC, beans, MVC, the JSP 2.0 expression language, and much more. Also gives detailed configuration and usage information for Apache Tomcat, Macromedia JRun, and Caucho Resin.
The Java Servlets programming Second Edition
The second edition of this popular book has been completely updated to add the new features of the Java Servlet API Version 2.2, and new chapters on servlet security and advanced communication. In addition to complete coverage of the 2.2 specification, we have included bonus material on the new 2.3 version of the specification.
Servlets are an exciting and important technology that ties Java to the Web, allowing programmers to write Java programs that create dynamic web content. Java Servlet Programming covers everything Java developers need to know to write effective servlets. It explains the servlet lifecycle, showing how to use servlets to maintain state information effortlessly. It also describes how to serve dynamic web content, including both HTML pages and multimedia data, and explores more advanced topics like integrated session tracking, efficient database connectivity.
The Using Javabeans with JSP The JavaBeans API provides a standard format for Java classes. Visual manipulation tools and other programs can automatically discover
information about classes that follow this format and can then create and manipulate the classes without the user having to explicitly write any code.
Full coverage of JavaBeans is beyond the scope of this book. The one exception to this naming convention is with boolean
properties: they use a method called is Xxx to look up their values. So, for example, your Car class might have methods called
isLeased (which takes no arguments and returns a boolean) and setLeased (which takes a boolean and has a void return
type), and would be said to have a boolean property named leased . Although you can use JSP scriptlets or expressions to access arbitrary
methods of a class, standard JSP actions for accessing beans can only make use of methods that use the
getXxx/setXxx or isXxx/setXxx design pattern.
The Servlet and JSP Filters Perhaps the single most important new capability in version 2.3 of the servlet API is
the ability to define filters for servlets and JSP pages. Filters provide a powerful and
standard alternative to the nonstandard ?servlet chaining? supported by some early
servers. A filter is a program that runs on the server before the servlet or JSP page with
which it is associated. A filter can be attached to one or more servlets or JSP pages
and can examine the request information going into these resources. After doing so,
it can choose among the following options.
* Invoke the resource in the normal manner.
* Invoke the resource with modified request information.
* Invoke the resource but modify the response before sending it to the
* Prevent the resource from being invoked and instead redirect to a different resource, return a particular status code, or generate
Web Development with JSP The second edition of the bestselling Web Development with JavaServer Pages updates and expands the original. In the entirely rewritten first part of the book the authors provide a gentle introduction to the important technologies on which JSP depends. The book then launches into its updated coverage of the JSP 1.2 and Servlet 2.3 standards. New chapters on servlet filters, tag-library validation, and non-HTML content are filled with fresh examples.
This second edition shares the strengths of the first, based on the authors' substantial experience with real-world development. The book covers the complete feature set of JSP 1.2, and both the advantages and the "gotchas" associated with those features. Its depth of coverage has been an important contributor to this book's success.