web.xml Visual Editor: Servlets

Use the Servlet section of the web.xml Visual Editor to add, remove, and view the deployment information of your web application's servlets and, optionally, JSP files.

Servlets

Servlets are Java classes that can be loaded dynamically into a server and executed by the server to extend its feature set. Servlets are useful for tasks that involve processing an HTTP request. Servlets are also helpful for tasks that do not generate an HTTP response at all or that generate a relatively simple HTTP response. Servlets use the javax.servlet API.

To deploy a servlet, you must register it in the web.xml file. Doing this provides information needed by the server to deploy the servlet. For example, you tell the server which class should be loaded to deploy the servlet.

JSP Files

JSP technology was designed to simplify the process of creating servlets. In fact, the main function of servlets is to generate HTML output in cases where dynamic portions can be encapsulated. Servlets are generated by JSP pages when compiled. In many applications, the response sent to the client is a combination of template data and dynamically-generated data. In this situation, it is often easier to work with JSP pages than to do everything with servlets.

Unlike servlets, the server automatically deploys JSP files, therefore you do not have to register a JSP files in the web.xml file in order to deploy it. However, if you register a JSP file in the web.xml file, you can use servlet elements to set initialization parameters and mappings for the JSP file, just as if it were a servlet.

Before registering a servlet or JSP file in the web.xml file, you can create the servlet or create the JSP file in the New File wizard, or import it from your existing sources. You can use the Configure Servlet Deployment panel of the New File wizard to register a servlet. If you have not done so, or if you want to modify a registered servlet's deployment information, use the Servlets view of the web.xml Visual Editor. Note that you cannot use the New File wizard to register JSP files.

In the Visual Editor, click Servlets at the top of the editor to open the Servlets view.

First, click Add Servlet Element to define the servlet element with the following properties:

Element Description

Servlet Name

Specifies the display name of the servlet or JSP file, such as MyVeryUsefulComponent.
Description Specifies a description, such as Initialization settings for my JSP file.

    Servlet Class

Specifies the servlet's class, such as MyVeryUsefulServlet.

    JSP File

Specifies the JSP file, such as MyVeryUseFulPage.

URL Pattern(s)

Specifies a mapping between the servlet definition or JSP file definition and a URL pattern, such as /bookstore/*, *.jsp. All requests to the specified URL patterns are handled by the servlet or JSP file associated with the servlet or JSP file definition.

When you click OK, the values you entered are displayed in the Servlets section. Optionally, you can now specify the startup order of the servlets in the application. In the Startup Order edit box, type an integer indicating the order in which the servlets should be loaded.

Initialization Parameters

Next, click Add within the servlet element and define its initialization parameters:

Element Description

Param Name

Specifies the parameter's display name, such as encoding.

Param Value

Specifies the parameter's value, such as UTF-8.

Description

Specifies a description, such as Acceptable encoding formats.

Security Role References

Optionally, click Add under Security Role References for the servlet to add role references:

Element Description

Role Ref Name

Specifies the security role reference's display name.

Role Ref Link

Specifies the security role defined in the Security Roles table in the Security section of the visual editor.

Description

Specifies a description.

Run As

Specifies the run-as element for the security roles.

Note that when you click Remove at the top of the Servlets section, you remove the definitions for the servlet or JSP file from the web.xml file, but you do not remove the servlet or JSP file itself. To delete the servlet or JSP file, right-click it in the Projects window and choose Delete.


Related Topics

Developing Applications with NetBeans IDE,

Developing Applications with NetBeans IDE,

Developing Applications with NetBeans IDE,

Copyright © 2016, Oracle and/or its affiliates. All rights reserved.