Issue
I'm using Java 6, Tomcat 6, and Metro. I use WebService and WebMethod annotations to expose my web service. I would like to obtain information about the request. I tried the following code, but wsCtxt is always null. What step must I take to not get null for the WebServiceContext.
In other words: how can I execute the following line to get a non-null value for wsCtxt?
MessageContext msgCtxt = wsCtxt.getMessageContext();
@WebService
public class MyService{
@Resource
WebServiceContext wsCtxt;
@WebMethod
public void myWebMethod(){
MessageContext msgCtxt = wsCtxt.getMessageContext();
HttpServletRequest req = (HttpServletRequest)msgCtxt.get(MessageContext.SERVLET_REQUEST);
String clientIP = req.getRemoteAddr();
}
Solution
I recommend you either rename your variable from wsCtxt to wsContext or assign the name attribute to the @Resource annotation. The J2ee tutorial on @Resource indicates that the name of the variable is used as part of the lookup. I've encountered this same problem using resource injection in Glassfish injecting a different type of resource.
Though your correct name may not be wsContext. I'm following this java tip. If you like the variable name wsCtxt, then use the name attribute in the variable declaration:
@Resource(name="wsContext") WebServiceContext wsCtxt;
Answered By - James A Wilson
Answer Checked By - Dawn Plyler (JavaFixing Volunteer)