A developer wants to use WebServiceContext in the web service endpoint. Which of the following is the correct way to get WebServiceContext object ? (Choose one)
A.
@WebService
public class MyService {
@WebServiceContext
WebServiceContext ctxt;
public String echo(String str) {
…
}
B.
@WebService
public class MyService {
WebServiceContext ctxt;
public String echo(String str) {
ctxt = jndi.lookup(“java:com/env/WebServiceContext”); }
C.
@WebService
public class MyService {
@Inject
WebServiceContext ctxt;
public String echo(String str) {
…
}
D.
@WebService
public class MyService {
@Resource
WebServiceContext ctxt;
public String echo(String str) {
…
}
Answer: D.
@WebService
public class MyService {
@Resource
WebServiceContext ctxt;
public String echo(String str) {
…
}
D
The servlet context is made available by JAX-WS via the message context, which can be retrieved using the web service context. Inserting the following member will cause JAX-WS to inject a reference to the web service context into your web service:
import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.xml.ws.WebServiceContext;
import javax.xml.ws.handler.MessageContext;
…
@Resource
private WebServiceContext context;