您的位置:首页 > 运维架构 > Tomcat

统计系统的登录信息 并写入到文件中

2016-08-25 13:34 267 查看
//统计登录系统的信息  这里主要是时间   用户名   ip地址

String ip=ServletActionContext.getRequest().getRemoteAddr();
Date dt=new Date();//如果不需要格式,可直接用dt,dt就是当前系统时间
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置显示格式
String nowTime= df.format(dt);//用DateFormat的format()方法在dt中获取并以yyyy/MM/dd HH:mm:ss格式显示
String sysPath = System.getProperty("user.dir");   //可以获取到tomcat到 /bin目录
String tomcatPath=sysPath.replace("bin","logs");    //修改路径到tomcat下的/logs/
System.out.println(loginUser.getLoginId()+"=="+ip+"=="+nowTime+"=="+sysPath);
String longinMessage=nowTime+"               "+loginUser.getLoginId()+"               "+ip;
FileWriter  fileWrite = null;
try {
 fileWrite=new FileWriter(tomcatPath+File.separator+"loginMessage.log",true);  //true  表示追加日志
 fileWrite.write(longinMessage);
 fileWrite.write("\r\n");    //换行
 fileWrite.flush();    
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fileWrite.close();
} catch (IOException e) {
e.printStackTrace();
}
}
需要主要的是:
 String ip=ServletActionContext.getRequest().getRemoteAddr();   如何得到的结果是 0:0:0:0:0:0:0:1,可以把访问地址:localhost:8080修改为127.0.0.1:8080;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐