简体   繁体   English

无法获取远程计算机的IP地址

[英]can not get the ip address of a remote computer

i have a requirement that only one user will be allowed to log in using one login id and password that means not two users will be allowed to login using the same username and id.so i thought to make a field in my Db to store the ipaddress.Suppose A has logged in then his ip address will be stored in Db and now if B wants to login then he will get alert that this user has already logged in and the ip address is A's ipaddress.and for that purpose i am using 我要求只允许一个用户使用一个登录ID和密码登录,这意味着不允许两个用户使用相同的用户名和ID登录。所以我想在我的Db中创建一个字段来存储ipaddress。假设A已经登录,那么他的IP地址将存储在Db中,现在,如果B要登录,那么他将收到该用户已经登录且该IP地址是A的ipaddress的警报。为此,我正在使用

    <%@ page import="java.net.*" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%

out.println("remote address  "+request.getRemoteAddr());
 out.println("remote host  "+request.getRemoteHost());
 out.println("remote port "+request.getRemotePort());
 out.println("remote user  "+request.getRemoteUser());
//out.println(InetAddress.getLocalHost().getHostAddress());
out.println(request.getRemoteAddr());
//out.println(InetAddress.getLocalHost().getHostAddress().toString());
%>
</body>
</html>

but i am not getting the exact ipaddress.please tell me how to get the ipaddress.if you think this way of storing ipadddress in Db and alerting to duplicate user is not wise then yours suggestions are also welcome 但是我没有得到确切的ipaddress.please告诉我如何获取ipaddress。如果您认为这种将ipadddress存储在Db中并警告重复用户的方法不明智,那么也欢迎您提出建议

remote address 0:0:0:0:0:0:0:1 remote host 0:0:0:0:0:0:0:1 remote port 38059 remote user null 0:0:0:0:0:0:0:1

For some reason, request.getRemoteAddr() did not work for you. 由于某些原因, request.getRemoteAddr()对您不起作用。 It is quite impossible to say why without knowing the company's network you are working in. 很难说为什么不知道您正在使用的公司网络。

However, you do not even need the IP-address for that. 但是,您甚至不需要IP地址。 Just keep a flag if the user is currently logged in: 如果用户当前已登录,只需保留一个标志:

User | logged in?
-----+------------
 A   |    Yes
 B   |    No
 C   |    No

Now, if a user tries to login, check if he/she is already logged in. If so, display an error message and avoid login. 现在,如果用户尝试登录,请检查他/她是否已经登录。如果已登录,则显示错误消息并避免登录。

You need to reset this flag 您需要重置此标志

  • When the user logs out (clicking the "logout" button) 用户注销时(单击“注销”按钮)
  • After the session times out (you can use a HttpSessionListener for that), in case that the user did not click "logout". 在会话超时后(可以使用HttpSessionListener ),以防用户未单击“注销”。 Note: If you have a high session timeout (like 30 minutes), and the user forgets to click "logout", he/she will not be able to login for the rest of the session. 注意:如果会话超时较高(例如30分钟),并且用户忘记单击“注销”,则他/她将无法在其余会话中登录。
  • After the application is redeployed, if all sessions are destroyed then. 重新部署应用程序后,如果所有会话都被销毁,那么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM