JBoss AS 7,Java EE 6如何获取客户端IP? [英] JBoss AS 7, Java EE 6 how to get clients IP?

查看:155
本文介绍了JBoss AS 7,Java EE 6如何获取客户端IP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的问题,但是我正在寻找更长的时间,但是我总是找到相同的答案,但我真的不知道该如何处理...

I have a simple question, but I'm searching for longer time, but I always found the same answers,which i don't really know how to handle...

当他注册到我的应用程序时,我想获得客户端的IP地址...

i want to get the IP adress of the client, when he registers to my application...

我发现了这样的东西:

    @ManagedBean(name="testController")
    @SessionScoped
    public class TestController implements Serializable {

        private static final long serialVersionUID = -3244711761400747261L;
        protected final HttpServletRequest req;

        public TestController(HttpServletRequest req) {
            this.req = req;
            System.out.println(this.req.getRemoteAddr().toString());
        }
    }

但是我在构造函数中没有HttpServletRequest.... 还是我不知道如何使用它,我得到的只是错误....

but i don't have the HttpServletRequest in the constructor.... or i don't know how to use it, all i get are errors....

推荐答案

请注意,您在最初的尝试中犯了一个主要的概念性错误.您正在尝试将当前HTTP请求分配为会话作用域受管bean的属性. HTTP请求实例将在当前HTTP响应结束时到期,因此不再有效,并且在创建会话范围Bean时,当您尝试在初始请求之后的后续请求中尝试在后续请求中访问其方法时,将抛出所有颜色的异常.

Note that you're making one major conceptual mistake in your initial attempt. You're attempting to assign the current HTTP request as a property of a session scoped managed bean. The HTTP request instance will expire by the end of the current HTTP response and thus not be valid anymore and throw exceptions in all colors when you try to access its methods in the subsequent requests following the initial request when the session scoped bean was been created.

这篇关于JBoss AS 7,Java EE 6如何获取客户端IP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆