![](https://csdnimg.cn/release/download_crawler_static/9901121/bg14.jpg)
CHAPTER 1 ■ INTRODUCTION TO SERVLETS
17
Note ■ To run the example, deploy the JSFByExample application to your application server container, and then
enter the following address into your browser: http://localhost:8080/JSFByExample/chapter01/math.html.
This assumes you are using default port numbers for your application server installation. If using the NetBeans project
that was packaged with the sources, you do not need to worry about copying the code as everything is pre-configured.
Explanation
Servlets make it easy to create web applications that adhere to a request and response life cycle. They have the
ability to provide HTTP responses and also process business logic within the same body of code. The ability to
process business logic makes servlets much more powerful than standard HTML code. The solution to this example
demonstrates a standard servlet structure for processing requests and sending responses. An HTML web form
contains parameters that are sent to a servlet. The servlet then processes those parameters in some fashion and
publishes a response that can be seen by the client. In the case of an HttpServlet object, the client is a web browser,
and the response is a web page.
Values can be obtained from an HTML form by using HTML <input> tags embedded within an HTML <form>.
In the solution to this example, two values are accepted as input, and they are referenced by their id attributes as
numa and numb. There are two more <input> tags within the form; one of them is used to submit the values to the form
action, and the other is used to reset the form fields to blank. The form action is the name of the servlet that the form
values will be passed to as parameters. In this case, the action is set to MathServlet. The <form> tag also accepts a
form-processing method, either GET or POST. In the example, the POST method is used because form data is being sent
to the action; in this case, data is being sent to MathServlet. You could, of course, create an HTML form as detailed
as you would like and then have that data sent to any servlet in the same manner. This example is relatively basic; it
serves to give you an understanding of how the processing is performed.
The <form> action attribute states that the MathServlet should be used to process the values that are contained
within the form. The MathServlet name is mapped back to the MathServlet class via the web.xml deployment
descriptor or the @WebServlet annotation. Looking at the MathServlet code, you can see that a doPost method is
implemented to handle the processing of the POST form values. The doPost method accepts HttpServletRequest
and HttpServletResponse objects as arguments. The values contained with the HTML form are embodied within the
HttpServletRequest object. To obtain those values, call the request object’s getParameter method, passing the id
of the input parameter you want to obtain. In this example, those values are obtained and stored within local String
fields.
String numA = req.getParameter("numa");
String numB = req.getParameter("numb");
Once the values are obtained, they can be processed as needed. In this case, those String values are converted
into int values, and then they are added together to generate a sum and stored into an int field. That field is then
presented as a response on a resulting web page.
int solution = Integer.valueOf(numA) + Integer.valueOf(numB);
As mentioned, the HTML form could be much more complex, containing any number of <input> fields.
Likewise, the servlet could perform more complex processing of those field values. This example is merely the tip
of the iceberg, and the possibilities are without bounds. Servlet-based web frameworks such as Java Server Pages
and Java Server Faces hide many of the complexities of passing form values to a servlet and processing a response.
However, the same basic framework is used behind the scenes.
www.it-ebooks.info