Java Reference
In-Depth Information
A post request includes form data as a header sent separately from the URL. This is
generally preferred, and it's required when confidential information is being collected on
the form. Also, some web servers and browsers do not support URLs longer than 255
characters, which limits the amount of information that can be sent in a get request.
Java servlets handle both of these requests through methods inherited from the
HttpServlet class: doGet( HttpServletRequest , HttpServletResponse ) and
doPost( HttpServletRequest , HttpServletResponse ) . These methods throw two kinds
of exceptions: ServletException , which is part of the javax.servlet package, and
IOException , an exception in the standard java.io package that involves input and out-
put streams.
The doGet() and doPost() methods have two arguments: an HttpServletRequest
object and an HttpServletResponse object. One is called when a get request is used to
execute the servlet, and the other is called with post . A common technique in Java
servlet programming is to use one method to call the other, as in the following example:
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
The request and response objects belong to classes in the javax.servlet.http package.
A servlet receives information about how it was run by calling methods of the
HttpServletRequest class. For example, when a web form is submitted to a servlet,
each field on the form is stored as a string by the HttpServletRequest class.
You can retrieve these fields in a servlet by calling the getParameter( String ) method
with the name of the field as an argument. This method returns null if no field of that
name exists.
A servlet communicates with the user by sending back an HTML document, a graphics
file, or another type of information supported by a web browser. It sends this information
by calling the methods of the HttpServletResponse class.
The first thing you must do when preparing a response is to define the kind of content
the servlet is sending to a browser. Call the setContentType( String ) method with the
content type as an argument.
21
The most common form for a response is HTML, which is set by calling
setContentType(“text/html”) . You also can send a response as text (“text/plain”),
graphics files (“image/gif”, “image/jpeg”), and application-specific formats such as
“application/msword”.
Search WWH ::




Custom Search