Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Print Client IP

 
Nilu Deshmukh
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am Using Rest Web services. I want to Insert Client IP address into database. I tried.. following



but I am getting Null Pointer Exception..

Can anybody help me regarding this..

Thanks in Advance..

Nilesh
 
Ivan Krizsan
Ranch Hand
Posts: 2198
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
Edit: The code below is for JAX-WS web services, while the question was about RESTful web services.
---
First: If you are using an EJB to implement the web service, then you will not be able to obtain the HTTP request object.
If you are not using an EJB, try this:

Best wishes!
 
Nilu Deshmukh
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ivan,

But how to run this code.. I am new into Rest web services and I only aware of RestEasy Web services.



I only know this kind of web services..

Please send me Complete code or yours and please also tell how to run this..


Thanks Again
 
Ivan Krizsan
Ranch Hand
Posts: 2198
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
Sorry, my mistake!
I missed that you are using REST web services.
The code I provided is for JAX-WS web services.
I will post a solution to your problem as soon as I have some more time.
Best wishes!
 
Ivan Krizsan
Ranch Hand
Posts: 2198
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
I modified one of the examples enclosed with JBoss RESTEasy and here is an example of a method in a REST resource that can access the HttpServletRequest object related to the incoming request:

By annotating a method parameter, you assure yourself that the request object is obtained in connection to processing a request.
I had a case once where injection into an instance field resulted in one single dependency injection, which very soon became stale.
To see what kind of objects you can have injected into a resource class using the @Context annotation, see section 6.1 (if I remember correctly) of the JAX-RS specification.
Best wishes!
 
ralph soika
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot!
providing @Context HttpServletRequest
as a method param works perfect!

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic