• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

get HttpServletRequest from java class

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
--------------------------------------------------------------------------------
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Your buns are mine! But you can have this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic