InfoCenter Home > 4.2.1: Developing servletsServlets are Java programs that build dynamic client responses, such as Web pages. Servlets receive and respond to requests from Web clients, usually across HTTP, the HyperText Transfer Protocol. Because servlets are written in Java, they can be ported without modification to different operating systems. Servlets are more efficient than CGI programs because, unlike CGI programs, servlets are loaded into memory once, and each request is handled by a Java virtual machine thread, not an operating system process. Moreover, servlets are scalable, providing support for a multi-application server configuration. Servlets also allow you to cache data, access database information, and share data with other servlets, JSP files and (in some environments) enterprise beans. Servlet coding fundamentalsIn order to create an HTTP servlet, you should extend the
For more information on the HttpServlet class and methods, review articles:
The doGet and doPost methods take two arguments: The HttpServletRequest represents a client's requests. This object gives a servlet access to incoming information such as HTML form data, HTTP request headers, and the like.The HttpServletResponse represents the servlet's response. The servlet uses this object to return data to the client such as HTTP errors (200, 404, and others), response headers (Content-Type, Set-Cookie, and others), and output data by writing to the response's output stream or output writer. Since doGet and doPost throw two exceptions (javax.servlet.ServletException and java.io.IOException), you must include them in the declaration. You must also import classes in the following packages:
The beginning of your servlet might look like the following example: import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.*; public class MyServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {After you create your servlet, you must:
Now that you successfully created, compiled, and tested your servlet on your local machine, you must install it in the WebSphere Application Server runtime. View article 6: Administer applications for this information. Servlet lifecycleThe javax.servlet.http.HttpServlet class defines methods to:
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|