Create and configure Servlets

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 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 using the Servlet wizard, complete the following steps:

  1. From the Web perspective, expand your dynamic project in the Project Explorer view, and expand the Deployment Descriptor.

  2. Right click on the Servlet icon, and select New > Servlet from the pop-up menu. The Create Servlet wizard appears.

    New Servlet

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

    Create a Servlet

    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 servlet class. To do this, select the Use existing Servlet class 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.

  4. Supply the following information:

    Create a Servlet. Specify the folder

    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 javax.servlet.http.HttpServlet, or any class that has javax.servlet.http.HttpServlet in its hierarchy as its superclass. Click Browse to choose from the available superclasses.

    3. Click Finish or click Next for more options.

  5. Select a modifier to specify whether your servlet class is public, abstract, or final.

    NOTE: Classes cannot be both abstract and final.

    Create a Servlet. Select a modifier

    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.

Working in the Servlets page

In the Servlets page of the Web deployment descriptor editor, you can add an existing servlet or JSP file to the deployment descriptor or remove the selected servlet of JSP file from the deployment descriptor.

To work in the Servlets page, complete the following steps:

  1. Open a Web project in the Project Explorer.

  2. Double-click the Web project's Deployment Descriptor file in the Project Explorer. The Web deployment descriptor editor opens.

  3. Click the Servlets tab to open the Servlets page.

  4. You can edit settings in the following sections of the Servlets page:

    Working in the Servlets page

    • Servlets and JSPs - list servlets and JSPs used in this application. You can add or remove servlets and JSPs from the list.

    • Details - lists details about the selected servlet or JSP. Details listed include the servlet class (canonical name), the display name (a short name for display purposes), and a description.

    • URL Mappings - lists URLs mapped to the selected servlet.

    • Initialization - lists the initialization parameters that are configured for the selected servlet or JSP file.

    • Load on Startup - enables you to specify the details of a servlet's load-on-startup element, including load order.

    • Security role references - list the security roles referenced in the Web application and lets you edit the security roles that are authorized to access the servlet. You can add or remove security roles. If you add a security role reference, you specify the role reference name, the role link, and a description.

    • Run as - lists details of the selected servlet's run-as element.

    • Icons - lets you view or change icons (small or large) used to represent the selected servlet or JSP file.

    • WebSphere Extensions - list and lets you change the details of the Servlet Extension for the selected servlet. You can add a new extension by clicking Add. The Add Markup Language entry wizard appears. Also, depending on the Servlet Version of the project you are working with, you can specify global and local transaction settings.

Professional hosting     Belorussian informational portal         Free SCBCD 1.3 Study Guide     Free SCDJWS 1.4 Study Guide     SCDJWS 1.4 Quiz     Free IBM Certified Associate Developer Study Guide     Free SCJP 5.0 (Tiger) Study Guide     Free Mock Exam Engine     Free SCWCD 1.4 Study Guide     IBM Test 000-287. Enterprise Application Development with IBM WebSphere Studio, V5.0 Study Guide