uCosminexus Application Server, Web Service Development Guide
The javax.servlet.ServletContext class is defined based on the Servlet 3.0 specifications.
The following example shows the usage of javax.servlet.ServletContext that is injected in the field of a root resource class:
package com.sample.resources;
import javax.servlet.ServletContext;
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 ServletContext is injected by using the Context annotation
private @Context ServletContext context;
//Resource method
@GET
public String getValue() {
return this.context.getInitParameter("Hitachi");
}
}
|
The following is an example of web.xml that contains the initialization parameter (context-param element) of the context scope:
<?xml version="1.0" encoding="UTF-8"?>
<web-app ...>
...
<context-param>
<param-name>TestParam</param-name>
<param-value>TestValue</param-value>
</context-param>
</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.ServletContext context is injected in the context 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 of the context scope from the context field, the value TestValue is acquired.
All Rights Reserved. Copyright (C) 2013, Hitachi, Ltd.