As we can see, the doPost() and doGet() methods for our servlet simply invoke
the generated processRequest() method, passing along the request and response
parameters. If we wish our servlet to handle only POST request, we should delete
the generated doGet() method; similarly, if we wish the servlet to handle only GET
request, the doPost() method should be deleted.
We need our servlet to process the data entered by the user in the application's input
page, then invoke the output page, which will be modified to obtain its data from an
attribute in the HTTP request.
In our example we modified the processRequest() method of our servlet so that
it creates an instance of a JavaBean called SurveyData and populates it with values
from the request parameters.
SurveyData is a very simple JavaBean with two private properties and
corresponding getters and setters. Since it is so simple, it is not shown, it
is part of this topic's code download. This bean's role is to be the model in
our MVC architecture.
The instance of SurveyData is then stored as a request attribute by invoking the
setAttribute() method in the request object. Request attributes are visible as long
as no new HTTP request is generated from the application.
We can navigate to other pages by forwarding the request and its attributes will be
preserved. Redirecting the HTTP response through the sendRedirect() method
in the HttpServletResponse() interface, clicking on a link, submitting a page, or
entering an URL directly in the browser's location field are all actions that generate
a new request, causing previous request attributes to be lost.
The URL used as a parameter to sendRedirect() can be a page on any server,
Forwarding, on the other hand, is limited to pages or resources in the same server
as the one where the servlet or JSP doing the forwarding is deployed.