Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

get HttpServletRequest from java class  RSS feed

 
Farhan Jan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
code:
--------------------------------------------------------------------------------
I'm trying to get " HttpServletRequest" object and then "remoteAddr()" method.
How can I get access to my HttpServletRequest, form within a normal java class, not in servlet class itself.


Here is the code I tried to use,



RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
if (requestAttributes instanceof ServletRequestAttributes) {
HttpServletRequest request = ((ServletRequestAttributes)requestAttributes).getRequest();

String rhost = request.getRemoteHost();
String raddr = request.getRemoteAddr();
int rport = request.getRemotePort();

String lhost = request.getLocalName();
String laddr = request.getLocalAddr();
int lport = request.getLocalPort();
}


I am using

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;





but here the


ServletRequestAttributes.getRequest();

is protected!


I need some guidance here!
thanks!
--------------------------------------------------------------------------------
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pass a reference from the servlet to the Java class.
 
Farhan Jan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pass a reference from the servlet to the Java class.


I cant do so,
Actually request is in jsp page, which is a "login page" from there control goes to a security class, whose implementation i cnat see or change.

after passing through th security mechanism, control comes to my java class, where i need to get IP of caller.

so i need to to somehow get request object here!

Please help me here!
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you describe how control is being passed from your JSP to the security class and then from the security class to your Java object?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!