uCosminexus Application Server, Web Service Development Guide

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

24.4.9 javax.servlet.http.HttpServletResponse

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

The following example shows the usage of javax.servlet.http.HttpServletResponse that is injected into the field of a root resource class:

package com.sample.resources;
 
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
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 HttpServletResponse is injected by using the Context annotation
  private @Context HttpServletResponse httpResponse;
 
  //Resource method
  @GET
  public void getValue() throws IOException {
    
    String entity = "Response mentioned using HttpServletResponse";
 
    httpResponse.setHeader("abc","xyz");
    httpResponse.getOutputStream().write(entity.getBytes());
    httpResponse.getOutputStream().flush();
    httpResponse.getOutputStream().close();
    
    return ;
  }
}

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, javax.servlet.http.HttpServletResponse is injected in the HttpResponse field and then the getValue() method that can process the HTTP GET request is called. Therefore, the location where the HttpResponse class is used for building a response, and the getValue() method of the root resource class com.sample.resources.Resource is the same.