When the user submits form, his information is sent to the corresponding servlet file because we've set the ACTION
attribute to point to the servlet.
A D V E R T I S E M E N T
The form can use the GET method or POST method.
In GET method, if the user enter the name "Inigo Montoya," the request URL is http://server:8080/servlet/Hello?name=Inigo+Montoya in the form of querystring which is visible to the user.
The space in the name is specially encoded as a plus sign by the browser because
URLs cannot contain space.
A servlet's HttpServletRequest object gives it access to form data in its query string.
Difference between GET and POST Method
When the user enters information in a form and clicks Submit ,
there are two ways the information can be sent from browser to server: in the URL, or
within the body of the HTTP request.
The GET method, which was used in example earlier, appends name/value pairs to the URL.
Unfortunately, the length of URL is limited, so this method only works if there are only a few parameters.
The URL could be truncated if the form uses a large number of parameter or if the parameters contain large amounts of data.
Also, parameters passed on URL are visible in the address field of the browsernot the best place for a password to be displayed.
The alternative to the GET method is POST method.
This method packages the name/value pairs inside the body of HTTP request,
which makes for a cleaner URL and imposes no size limitation on the forms output.
It is more secure.
Overriding service, doGet, and doPost
When a request is made, Servlet Engine hands on the incoming data to
the Servlet engine, which processes the request, including form data,
cookies, session information, and URL name-value pairs, into an object
of type HttpServletRequest called the request object. Client metadata is
encapsulated as the object of type HttpServletResponse and is called the
response object. The Servlet engine passes both objects as parameters to
Servlets service() method.
The default service() method in an HTTP servlets routes the request to
another method based on the HTTP transfer method (POST, GET, etc.) For
example, HTTP POST requests are routed to doPost() method, HTTP GET
requests are routed to the doGet() method, and so on. This enables the
Servlet to perform different processing on request data depending on
the transfer method. Since routing takes place in service(), you
generally do not override service() in an HTTP Servlet. Instead,
override doGet() and/or doPost(), etc., depending on type of request
you expect.
The automatic routing in an HTTP Servlets is based simply on a call to
request.getMethod(), which provides the HTTP transfer method. In Servlets
Engine, request data is already preprocessed into a name-value list by
the time the Servlet sees the data, so you could simply override the
service() method in an HTTP Servlet without losing any functionality.
However, this does make the Servlet less portable, since it is now
dependent on preprocessed request data.
You must override service() method (for generic Servlets) or the
doGet() and/or doPost() methods (for HTTP servlets) to perform the tasks
needed to answer the request. Very often, this means accessing EJBs to
perform the business transactions, collating the needed information (in the
request object or in a JDBC ResultSet object), and then passing the
newly generated content to the JSP for formatting and delivery back to the
client.
Most operations that involve the forms use either a GET or a POST
operation, so for most servlets you override either doGet() or doPost().
Note that you can implement both the methods to provide for both types of
input, or simply pass the request object to a central processing method
All of the actual request-by-request traffic in an HTTP Servlets is
handled in the appropriate doOperation() method, including session
management, user authentication, dispatching EJBs and JSPs, and
accessing iAS features.
If you have a Servlets that you intend to also call using a
RequestDispatcher method include() or forward() , be aware that the
request information is no longer sent as HTTP POST, GET, etc.
RequestDispatcher methods always call the service(). In other words, if a
servlet overrides doPost(), it may not process anything if another
servlet calls it, if the calling servlet happens to have received its
data via HTTP GET. For this reason, be sure to implement routines for
all possible types of the input, as explained above.
Note Arbitrary binary data, like uploaded files or images, can
be problematic, since web connector translates incoming data into
name-value pairs by default. You can program web connector to
properly handle this kind of data and package it correctly in the
request object. Accessing Parameters and Storing the Data
Incoming data is encapsulated in the request object. For HTTP servlet,
the request object is of type HttpServletRequest. For generic servlet,
the request object is of type ServletRequest. The request object
contains all the parameters in a request, and you can also set your own
values in the request. The latter is called attribute.
You can access all the parameters in an incoming request by using
getParameter() method.
The following example describe the use of getParameter()
You can also set and retrieve values in the request object using setAttribute() and getAttribute(), respectively.
The following example describe the use of setAttribute()
request.setAttribute("accountNumber", "3284766");
A complete example showing the use of doGet Method in Servlets
Step1:Make the HTML form
Step2:Make the corresponding Servlets Page
Step1:Make the HTML form
Here we are make a HTML form called form.html which is given below:
<html>
<head>
<title>Introductions</title>
</head>
<body>
<form method=GET action="/servlet/name">
If you don't mind me asking, what is your name?
<input type=text name="name"><P>
<input type=submit>
</form>
</body>
</html>
Step2:Make the corresponding Servlets Page
Here we are make a Servlets filr called name.java as we have given in the action attribute path in the form.html as name which is given below: