2

I am using Jodd Madvoc MVC Framework. I want to get IP address of web application client. How can I get HttpServletRequest object in action class?

@MadvocAction(value = "login")
public class LoginAction extends BaseAction {

    Logger logger = LoggerFactory.getLogger(getClass());

    @PetiteInject
    UserService userService;

    @Action
    public void view() {
        if (logger.isInfoEnabled())
            logger.info("LoginAction.view()");
       // Code for getting ip Address
        forwardTo("/login.jsp");
    }
}
Kirtan Patel
  • 158
  • 1
  • 11
  • Make a field for a HttpServletRequest and then pass it to the Login class with setter method. – itwasntme Aug 16 '15 at 09:03
  • 1
    Hello mastah, I am using Jodd Madvoc MVC Framework in which I haven't to initialize the action class. So setting private field of HttpServletRequest is not possible. – Kirtan Patel Aug 16 '15 at 15:16

1 Answers1

1

After reading http://jodd.org/doc/madvoc/injection.html like HttpServletResponse injection I have done the same for HttpServletRequest.

@MadvocAction(value = "login")
public class LoginAction extends BaseAction {

    Logger logger = LoggerFactory.getLogger(getClass());

    @PetiteInject
    UserService userService;

    @In(scope = ScopeType.SERVLET)
    protected HttpServletResponse servletResponse;

    @In(scope = ScopeType.SERVLET)
    protected HttpServletRequest servletRequest;

    @Action
    public void view() {
        if (logger.isInfoEnabled())
            logger.info("LoginAction.view()");
       // Code for getting ip Address
        forwardTo("/login.jsp");
    }
}

And problem is solved. :-)

Kirtan Patel
  • 158
  • 1
  • 11
  • This is correct answer :) However, try not to use servlet class - Madvoc purpose is so users may use simple java beans, and not servlet classes. In your case, do the forwarding using the results - just keep reading the docs ;))) – igr Aug 17 '15 at 06:36
  • Yes I am trying not to use HttpServletRequest but I want to get Ip Address from request so want to use that. – Kirtan Patel Aug 17 '15 at 07:15
  • Got it. Notice that you can reach servlet properties (like getRequestAddress) using special fields names... from the docs: "...adapter map for servlet objects: when @In property is named as: requestMap, sessionMap or contextMap, corresponding map adapter will be injected in the property. Of course, it is expected that property type is Map...." – igr Aug 17 '15 at 07:39