The Java EE 6 Tutorial

Setting Context and Initialization Parameters

The web components in a web module share an object that represents their application context. You can pass initialization parameters to the context or to a web component.

ProcedureTo Add a Context Parameter Using NetBeans IDE

  1. Open the project if you haven’t already.

  2. Expand the project’s node in the Projects pane.

  3. Expand the Web Pages node and then the WEB-INF node.

  4. Double-click web.xml.

  5. Click General at the top of the editor pane.

  6. Expand the Context Parameters node.

  7. Click Add.

    An Add Context Parameter dialog opens.

  8. In the Parameter Name field, type the name that specifies the context object.

  9. In the Parameter Value field, type the parameter to pass to the context object.

  10. Click OK.

ProcedureTo Add an Initialization Parameter Using NetBeans IDE

You can use the @WebServlet annotation to specify web component initialization parameters by using the initParams attribute and the @WebInitParam annotation. For example:


@WebServlet(urlPatterns="/MyPattern", initParams=
  {@WebInitParam(name="ccc", value="333")})

Alternatively, you can add an initialization parameter to the web.xml file. To do this using NetBeans IDE, follow these steps.

  1. Open the project if you haven’t already.

  2. Expand the project’s node in the Projects pane.

  3. Expand the Web Pages node and then the WEB-INF node.

  4. Double-click web.xml.

  5. Click Servlets at the top of the editor pane.

  6. Click the Add button under the Initialization Parameters table.

    An Add Initialization Parameter dialog opens.

  7. In the Parameter Name field, type the name of the parameter.

  8. In the Parameter Value Field, type the parameter’s value.

  9. Click OK.