Client IP Address

4
I know its currently not possible to get the browser client headers from a java action. I am writing a reCaptcha widget, and need the IP address of the client submitting the captcha form. Is there a simple workaround, getting the IP address of a client?
asked
3 answers
2

Google tells me it is not possible from the client side. You might write a servlet that returns the IP of the connecting client and call this servlet from within your widget's javascript. A bit complex I would say.

answered
2

Good idea Bart. Create a javaaction, start in After Startup. Java action code:

import com.mendix.core.Core;
import com.mendix.externalinterface.connector.RequestHandler;
import com.mendix.m2ee.api.IMxRuntimeRequest;
import com.mendix.m2ee.api.IMxRuntimeResponse;
import com.mendix.m2ee.log.ILogNode;
import com.mendix.systemwideinterfaces.core.UserAction;

public class AddClientIPHandler extends UserAction<Boolean>

{
    public AddClientIPHandler()
    {
        super();
    }
@Override
public Boolean executeAction() throws Exception
{
    // BEGIN USER CODE
    Core.addRequestHandler("client/", new ClientIPHandler());
    return true;
    // END USER CODE
}

/**
 * Returns a string representation of this action
 */
@Override
public String toString()
{
    return "AddClientIPHandler";
}

// BEGIN EXTRA CODE
class ClientIPHandler extends RequestHandler {
    private static final String HTML_CONTENT = "text/html";
    private static final String ENCODING = "UTF-8";
    @Override
    public void processRequest(IMxRuntimeRequest request,
            IMxRuntimeResponse response, String arg2) throws Exception 
    {
        try {
            response.setContentType(HTML_CONTENT);
            response.setCharacterEncoding(ENCODING);
            response.getWriter().append(request.getRemoteAddr());
        }
        catch (Exception e)
        {
            response.setStatus(500); //internal server error
            log.error("Error while serving clientip: ", e);
        }               
    }

}
protected static ILogNode log = Core.getLogger("ClientIP");

// END EXTRA CODE
}

Call with localhost:8080/client/

answered
0

HI,

 

Found this post, is there a way to redirect or forward to the next page?

 

Thanks

Shannon

answered