Thursday, October 18, 2012
Get Client IP Address in JBoss AS 7
Recently I was trying to figure out how to get the ip address of the calling client using JBoss AS 7. I found a few examples that have basically the following code.
import javax.servlet.http.*;
@GET
@Path("/")
@Produces("text/xml")
public String login(@QueryParam("user") String user,
@QueryParam("password") String password,
@Context HttpServletRequest req) {
{
String ipAddress=req.getRemoteAddr();
userLogin(user, password, ipAddress);
}
They key is to use the @Context annotation to get an HttpServletRequest. Then you can just call getRemoteAddr().
Now, I tried this and I kept getting compilation errors. I for some reason assumed that all of the jar's that JBoss used would be added to the classpath of eclipse when I installed the JBoss Tools. Well, I guess they were not. So, all I had to do was go to the project properties, then select Java Build Path, then select the Libraries tab and the jboss-servlet-api_3.0_spec-1.0.0.Final.jar to the project.
This jar is located at <JBOSS-INSTALL>\modules\javax\servlet\api\main, so for me on windows it was at C:\jboss-as-7.1.1.Final\modules\javax\servlet\api\main
That's it!
Subscribe to:
Post Comments (Atom)
Hi i read your blog very nice info about JBoss Development and also your comment for Jboss are welcome.
ReplyDelete