InfoCenter Home >
4: Developing applications >
4.2: Building Web applications >
4.2.1: Developing servlets >
4.2.1.3: Servlet content, examples, and samples >
4.2.1.3.1: Creating HTTP servlets
4.2.1.3.1: Creating HTTP servlets
To create an HTTP servlet, as illustrated in
ServletSample.java:
- Extend the HttpServlet abstract class.
- Override the appropriate methods. The ServletSample overrides the doGet() method.
- Get HTTP request information, if any.
Use the HttpServletRequest object to retrieve data submitted through HTML
forms or as query strings on a URL. The ServletSample example receives an
optional parameter (myname) that can be passed to the servlet as query parameters
on the invoking URL. An example is:
http://your.server.name/application_URI/ServletSample?myname=Ann
The HttpServletRequest object has specific methods to retrieve information provided
by the client:
- getParameterNames()
- getParameter(java.lang.String name)
- getParameterValues(java.lang.String name)
- Generate the HTTP response.
Use the HttpServletResponse object to generate the client response. Its
methods allow you to set the response headers and the response body. The
HttpServletResponse object also has the getWriter() method to obtain a PrintWriter
object for sending data to the client. Use the print() and println() methods of the PrintWriter object
to write the servlet response back to the client.
|
|