Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to get client Ip address from web service

843833Mar 7 2009 — edited Mar 11 2009
Hello folks,
I am quite new to web service and developing a web service using netbeans6.0 IDE, where i have to know which client called/used the service, After googling i got code snippet like below
inject a WebServiceContext instance into your WebService class, and use it in each WebMethod to get the current request's MessageContext Map. Get the SERVLET_REQUEST item from that, cast as an HttpServletRequest. From that HttpServletRequest, you can invoke getRemoteAddr() to get the client's IP.

@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();

}
But it throws error in my PC like
Service invocation threw an exception with message : null; Refer to the server log for more details


--------------------------------------------------------------------------------

Exceptions details : java.lang.reflect.InvocationTargetException
Is there any way that says client pc ip address using netbeans6.0 Or what went wrong in my code?

Largely Thanks in advance.

SRI.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 8 2009
Added on Mar 7 2009
8 comments
2,601 views