InfoCenter Home >
4: Developing applications >
4.2: Building Web applications >
4.2.1: Developing servlets
4.2.1: Developing servlets
Servlets 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 fundamentals
In order to create an HTTP servlet, you should extend the
javax.servlet.HttpServlet class and override any methods that you wish
to implement in the servlet. For example, a servlet would override the doGet
method to handle GET requests from clients.
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:
Package names |
Functions/Objects |
java.io |
PrintWriter |
javax.servlet |
HttpServlet |
javax.servlet.http |
HttpServletRequest and HttpServletResponse |
Note: When creating your servlets, do not use the following
reserved words for the class name:
- Description
- Code
- LoadAtStartup
- UserServlet
- DebugMode
- Enabled
Some reserved words such as UserServlet can be used in the package names but
create problems when used as class names.
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:
- Compile your servlet using the javac command, as for example:
javac MyServlet.java
- Invoke your servlet using one of the methods described in article:
4.2.4.4: Providing ways for clients to invoke applications
You can also compile your servlet using the -classpath option on the
javac compiler. To access the classes that were extended, reference the
servlet.jar file in the
<WAS_root>\lib directory.
Using this method, you issue the following command to compile your servlet:
javac -classpath C:\<WAS_root>\lib\servlet.jar MyServlet.java
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 lifecycle
The javax.servlet.http.HttpServlet
class defines methods to:
- Initialize a servlet
- Service requests
- Remove a servlet from the server
These are known as life-cycle methods and are called in the following sequence:
- The servlet is constructed
- It is initialized with the init method
- Calls from clients to the service method are handled
- The servlet is taken out of service
- It is destroyed with the destroy method
- The servlet is finalized and the garbage is collected.
Review article 4.2.1.1 for more life cycle information.
|
 |
 |
Related topics |
|
| Home (Getting started page) |
|
|
| 0.9: What are servlets? |
|
| 4.2.1.2: Features of the supported Java Servlet API and the IBM extensions to it |
|
| 4.2.1.3: Servlet coding examples |
|
| 6.6.8: Servlet configuration information |
Sub-topics |
|
| 4.2.1.1: Servlet lifecycle |
|
| 4.2.1.2: Servlet support and environment in WebSphere |
|
| 4.2.1.2b: Using servlets in a multi application server environment |
|
| 4.2.1.3: Servlet content, examples, and samples |
|
Peer topics |
|
| 4.2.2: Developing JSP files |
|
| 4.2.3: Incorporating XML |
|
| 4.2.4: Putting it all together (Web applications) |
|
| 4.2.5: Using the Bean Scripting Framework |
|
| 4.2.8: Programming high performance Web applications |
|
| 4.2.9: Setting language encoding in Web applications |
|
| 4.2.10: Converting WAR files to Web applications (wartowebapp script) |
|
InfoCenter |
|
To launch the full documentation set in a separate browser window, click: |
| Display InfoCenter |
| |
PDF library |
|
To browse the PDF library for this product, containing this article and others, click: |
| PDF versions |
| |
Using this documentation |
|
Become an InfoCenter super user! To find out more about navigation, numbering, search, downloads, and more, click: |
| Using this documentation |
| |
|