uCosminexus Application Server, Web Service Development Guide

[Contents][Glossary][Index][Back][Next]

24.4.6 javax.servlet.ServletConfig

The javax.servlet.ServletConfig class is defined based on the Servlet 3.0 specifications.

The following example shows the usage of javax.servlet.ServletConfig that is injected to the field of a root resource class:

package com.sample.resources;
 
import javax.servlet.ServletConfig;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
 
//Root resource class
@Path("/root")
public class Resource {
  //A field in which the ServletConfig is injected by using the Context annotation
  private @Context ServletConfig config;
 
  //Resource method
  @GET
  public String getValue() {
    return this.config.getInitParameter("TestParam");    
  }
}

The following is an example of web.xml that contains an additional initialization parameter (init-param element):

<?xml version="1.0" encoding="UTF-8"?>
<web-app ...>
  <servlet>
    <servlet-name>CosminexusJaxrsServlet</servlet-name>
    <servlet-class>com.cosminexus.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
      <param-name>com.cosminexus.jersey.config.property.packages</param-name>
      <param-value>com.sample.resources</param-value>
    </init-param>
    <init-param>
      <param-name>TestParam</param-name>
      <param-value>TestValue</param-value>
    </init-param> 
  </servlet>
  <servlet-mapping>
    <servlet-name>CosminexusJaxrsServlet</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>
</web-app>

Consider the context root of the Web application (WAR file) containing the root resource class com.sample.resources.Resource to be example, and that the Web application is published on a host named sample.com. In the above example, with the HTTP GET request corresponding to the URL http://sample.com/example/root, first, the javax.servlet.ServletConfig context is injected in the config field and then the getValue() method that can process the HTTP GET request is called. Therefore, if you use the getValue() method and acquire the initialization parameter TestParam from the config field, the value TestValue is acquired.