Creating servlets

The servlet wizard helps you create Java servlets by walking you through the creation process and by providing you with output files that you can use "as is" or that you can modify for use with your Web application. The servlets can run on WebSphere Application Server or other J2EE-compliant Web servers.

To create a servlet, complete the following steps:

  1. Open the Servlet wizard. To do this from the Web perspective, expand your project in the Project Explorer view, expand the Deployment Descriptor, and right click on the Servlet icon. Then select New > Servlet from the pop-up menu. The Create Servlet wizard appears.

  2. In the Create a Servlet page, supply the following:

    1. Type the name and description of the servlet. Notice that the name that you type in the name field is used as the default URL mapping.

    2. Optionally add or remove initialization parameters and URL mappings.

    3. Optionally choose an existing sevlet class. To do this, select the appropriate check box, and then select the Browse button. A window appears in which you can choose an existing servlet or a JSP.

    4. Optionally choose whether you want to generate an annotated servlet class.

    5. If you selected an existing servlet class, click Finish; otherwise, select Next.

  3. Supply the following information:

    1. Specify the folder where the servlet class will be placed, the package that the class will belong to (it is added into a default package if you do not specify one), and the class name of the servlet.

    2. Specify a superclass for the servlet class. A servlet created by this wizard can have HttpServlet, or any class that has HttpServlet in its hierarchy as its superclass. Click Browse to choose from the available superclasses.

    3. Click Finish or click Next for more options.

  4. Select a modifier to specify whether your servlet class is public, abstract, or final. (Classes cannot be both abstract and final.)

    1. The javax.servlet.Servlet is provided as the default Interface. You can also add additional interfaces to implement. Click Add to open the Interface Selection dialog.

      In this dialog, as you type the name of the interface that you are interested in adding in the Choose interfaces field, the list of available interfaces listed in the Matching types list box updates dynamically to display only the interfaces that match the pattern. Choose an interface to see the Qualifier, and then click Add. Click OK when you are finished.

    2. Select any appropriate method stubs to be created in the servlet file. The stubs created by using the Inherited abstract methods option must be implemented if you do not intend to create an abstract servlet. This is not true for Constructors from superclass.

    3. Click Finish. The servlet is generated.

 

Related concepts

The Web deployment descriptor editor
Annotation-based programming overview

 

Related tasks

Configuring Web applications using the Web deployment descriptor editor
Adding Web annotation tags