Servlet wizard

This is a feature of JBuilder Professional and Enterprise.

To open the Servlet wizard, choose File|New, select the Web tab of the object gallery, then double-click the Servlet icon.

In JBuilder Professional and Enterprise, you can use the Servlet wizard to create a Java file that extends javax.servlet.http.HttpServlet. The JBuilder Servlet wizard creates servlets that generate the following content types:

With the Servlet wizard, you can also create standard servlets, filter servlets, or listener servlets (if your web server supports the JavaServer Pages v1.2/Java Servlets v2.3 specifications). The steps displayed in the wizard depend on the type of servlet you are creating.



Choose servlet name and type

This is a feature of JBuilder Professional and Enterprise.

Package and Class

On the first step of the Servlet wizard, you can choose the package the servlet will belong to. You can also enter the name of the servlet in the Class field.

Generate Header Comments

If you want to replicate the header comments added to other classes in the project (see the Project wizard online Help topic), choose the Generate Header Comments option.

Single Thread Model

The Single Thread Model option implements the SingleThreadModel interface. Choosing this option may make your servlet a little less efficient, as the web server will queue requests and start another instance of the servlet to service the demand.

WebApp

Select the name of the WebApp you want to run this servlet under from the WebApp drop-down list. Any web content files, such as a servlet's SHTML file, will be placed in the WebApp directory.

Servlet Type

The options at the bottom of the dialog allow you to choose the type of servlet you're creating.

Note: These options are only available if your web server supports the JavaServer Pages v1.2/Java Servlets v2.3 specifications. (Tomcat 4.0 is the reference implementation of these specifications.) Otherwise a standard servlet is created by default.

Standard Servlet

A servlet that is not a filter or a listener. The Standard Servlet option is always available whether you select the <Default WebApp> or a named WebApp. When this option is selected for the default WebApp, the servlet name (see Enter webapp details page) defaults to the simple class name of the servlet, in lowercase. If the WebApp is a named WebApp, the servlet URL on the Enter webapp details page defaults to the URL pattern: /servletname (all lowercase).

Filter Servlet

A servlet that acts as a filter for another servlet or for the WebApp. In addition to the name, you must choose a URL pattern for the other servlet (which must have a name). This option is available only if a named WebApp is selected. If this option is selected, the Enter webapp details page, where you name the servlet and specify the filter mapping, is the only other available Servlet wizard page.

Listener Servlet

A servlet that is added to the WebApp's list of listeners. This option is available only if a named WebApp is selected. If this option is selected, the Enter listener servlet details page is the only other available Servlet wizard page.



Edit standard servlet details

This is a feature of JBuilder Professional and Enterprise.

If you select Standard Servlet as the servlet type on the Choose servlet name and type page of the Servlet wizard, the Enter standard servlet details page is Step 2 of the wizard. This page allows you to select the servlet's content type, the methods to implement, and the SHTML file to generate.

Generate Content Type

You use the Generate Content Type drop-down list to choose the content type for the servlet. The options include:

Implement Methods

This area of the Servlet wizard provides options for overriding the standard servlet methods. HttpServlet provides an abstract class that you can subclass to create an HTTP servlet, which receives requests from and sends responses to a web client. When you subclass HttpServlet, you must override at least one method, usually one of these. For more information on the methods, refer to the Servlet documentation at http://java.sun.com/products/servlet/index.html.

The following methods can automatically be generated in your servlet:

SHTML File Details options

The SHTML file details options are displayed if you select either HTML or XHTML from the Generate Content Type drop-down list. If you want to call the servlet from an HTML page, as described in "Calling a servlet from an HTML page," select the Generate SHTML File option. An SHTML file, with the same name as the servlet, is added to the project. If you select a WebApp on the Servlet wizard's Choose servlet name and type page, the SHTML file will be placed in this directory.

If you want to run the servlet directly, as described in "Invoking a servlet from a browser window," do not select the Generate SHTML File option.

Choose the SHTML file's background color from the Background Color drop-down list. To connect to the servlet from the SHTML file, you can either use a <servlet> tag or an <a href> link tag.



Enter webapp details

This is a feature of JBuilder Professional and Enterprise.

This page allows you to quickly define basic WebApp mappings. (The selected WebApp is used to run or debug the servlet when you select Web Run or Web Debug.) The Servlet wizard automatically adds the servlet mappings to the Servlet or Filters sections of the web.xml deployment descriptor file. For more information, see "Deploying servlets" or "Filters page" in "Deploying your web application."

For more information on mapping servlet names and URL patterns, see "How URLs run servlets" in "Working with web applications in JBuilder."

Name

The Name field applies to both standard and filter servlets. This field is displayed for standard servlets running in both the <Default WebApp> and named WebApp and for filter servlets.

In this field, enter the servlet's name. For a standard servlet, this is the name that is used to run the servlet. By convention, this is always lowercase.

The Name represents a short-hand way of running a servlet. For example, instead of pointing your browser to the servlet's fully qualified class name running on the web server, you could simply enter: inputform. This could map to the servlet named formservlet, which in turn could be mapped to the servlet class http://localhost:8080/servlet/FormServlet running locally on the Tomcat web server from the JBuilder IDE.

URL Pattern

For both standard and filter servlets, the URL Pattern field is where you choose the URL pattern that will be used to run the servlet. The URL pattern is always lowercase and preceded by a forward slash. This is useful when you're invoking the servlet directly, from a browser. (See "Invoking a servlet from a browser window" for more information.)

A default URL pattern is suggested. For a standard servlet, the default pattern consists of the servlet's class name in lower case preceded by a forward slash: /formservlet. This would map to the servlet's fully qualified class name. If the servlet is a filter servlet, the URL Pattern field defaults to /*, which means that the filter is applied to all requests.

Note: This field is not available if you did not select a WebApp on the Servlet wizard's Choose servlet name and type page.

Mapped Servlet

For filter servlets, you can choose how the servlet is mapped - it can be mapped with either the URL Pattern or the Mapped Servlet option.

The Mapped Servlet drop-down list allows you to choose another servlet in your project that will be filtered by this servlet. (This field defaults to the lowercase name of the first servlet in your project, preceded by a forward slash. For example, if your project already contains Servlet1.java and Servlet2.java which are both standard servlets, the Mapped Servlet field would default to /servlet1.) The drop-down list shows all other servlets in the project that have been previously mapped. If there are none, the option is disabled.



Enter servlet request parameters

This is a feature of JBuilder Professional and Enterprise.

The Enter servlet request parameters page of the Servlet wizard is where you enter servlet parameters. Parameters are values passed to the servlet. The values might be simple input values. However, they could also affect the runtime logic of the servlet. For example, the user-entered value could determine what database table gets displayed or updated. Alternatively, a user-entered value could determine the servlet's background color.

The grid on this page is used to enter the information that defines the parameters for your servlet. This information will generate PARAM tags within the FORM tag of the new HTML file, and parameter-handling code in the new Servletx.java file. Fields marked with an asterisk (*) are required fields.

Fill in one row of the grid for each parameter.

Name

A name for the parameter. This will be used to provide the NAME attribute in the FORM tag in the HTML file, and to provide the name parameter of the corresponding getParameter() call in the Java source.

Type

A drop-down choice list from which you select the type of variable that will be inserted into the Java source code of your servlet for holding the value of the parameter coming in from the HTML page.

Desc

A brief description of the parameter. This will be used to generate comments and to label the form field in the generated HTML file.

Variable

The name of the variable that will be inserted into the Java source code of your servlet for holding the value of the parameter coming in from the HTML page.

Default

The default value for the parameter. This is the value that the Java source code in this servlet will use if some future HTML file that uses this servlet has no PARAM tag for this parameter. For an HTML file to provide this parameter, the NAME attribute in the PARAM tag must exactly match what you enter in the Name column in this row of the grid. Note that this matching is case sensitive.



Enter listener servlet details

This is a feature of JBuilder Professional and Enterprise.

This page is available only if you select the servlet type of Listener Servlet on the Choose servlet name and type page of the Servlet wizard. It is Step 2, and the final Servlet wizard step for listener servlets. You use this page to implement one or more servlet listener interfaces. The corresponding methods are added to the servlet.

The Servlet wizard automatically adds the selected listeners to the Listeners section of the web.xml deployment descriptor file. For more information, see "Listeners page" in "Deploying your web application."