本人初学者,刚学到Servlet,作练习时,准备统计网站的在线人数(Session的数量),当服务器关闭或重启的时候,将当前Session的数量序列化到文件中,问题就出在这个路径上,如下面代码(在ServletContextListener中的contextDestroyed方法中,设置一个Attribute来保存当前系统所有Session的数量)所示,我只写的一个文件名,其后又打印出文件路径,结果居然是/home/Jinghwm/tools/apache-tomcat-6.0.14/bin/count.dat===============,高手帮忙分析下。
public void contextDestroyed(ServletContextEvent sce) {
ServletContext scx = sce.getServletContext();
if((Integer)scx.getAttribute("count") > 0){
File f = new File("count.dat");
DataOutputStream dos = null;
try {
if (!f.exists()) {
f.createNewFile();
}
System.out.println(f.getAbsolutePath()+"===============");
dos = new DataOutputStream(new FileOutputStream(f));
dos.writeInt((Integer) scx.getAttribute("count"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (dos != null)
try {
dos.close();
} catch (Exception e) {}
}
}
}
当Tomcat关闭时,要将Session的数量序列化到硬盘时,结果居然将文件保存到Tomcat的bin目录下,这个岂不很不安全吗?
当Tomcat关闭时,具体输出如下:
2008-11-26 17:59:09 org.apache.coyote.http11.Http11Protocol pause
信息: Pausing Coyote HTTP/1.1 on http-8080
2008-11-26 17:59:10 org.apache.catalina.core.StandardService stop
信息: Stopping service Catalina
2008-11-26 17:59:10 org.apache.catalina.core.ApplicationContext log
信息: SessionListener: contextDestroyed()
2008-11-26 17:59:10 org.apache.catalina.core.ApplicationContext log
信息: ContextListener: contextDestroyed()
/home/Jinghwm/tools/apache-tomcat-6.0.14/bin/count.dat===============,
2008-11-26 17:59:10 org.apache.coyote.http11.Http11Protocol destroy
信息: Stopping Coyote HTTP/1.1 on http-8080高手帮忙看下?
public void contextDestroyed(ServletContextEvent sce) {
ServletContext scx = sce.getServletContext();
if((Integer)scx.getAttribute("count") > 0){
File f = new File("count.dat");
DataOutputStream dos = null;
try {
if (!f.exists()) {
f.createNewFile();
}
System.out.println(f.getAbsolutePath()+"===============");
dos = new DataOutputStream(new FileOutputStream(f));
dos.writeInt((Integer) scx.getAttribute("count"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (dos != null)
try {
dos.close();
} catch (Exception e) {}
}
}
}
当Tomcat关闭时,要将Session的数量序列化到硬盘时,结果居然将文件保存到Tomcat的bin目录下,这个岂不很不安全吗?
当Tomcat关闭时,具体输出如下:
2008-11-26 17:59:09 org.apache.coyote.http11.Http11Protocol pause
信息: Pausing Coyote HTTP/1.1 on http-8080
2008-11-26 17:59:10 org.apache.catalina.core.StandardService stop
信息: Stopping service Catalina
2008-11-26 17:59:10 org.apache.catalina.core.ApplicationContext log
信息: SessionListener: contextDestroyed()
2008-11-26 17:59:10 org.apache.catalina.core.ApplicationContext log
信息: ContextListener: contextDestroyed()
/home/Jinghwm/tools/apache-tomcat-6.0.14/bin/count.dat===============,
2008-11-26 17:59:10 org.apache.coyote.http11.Http11Protocol destroy
信息: Stopping Coyote HTTP/1.1 on http-8080高手帮忙看下?
可以通过 System.getProperty("user.dir"); 获得。
这样说我一个应用就可以良方其它的WEB主目录了,这样是不是不太安全呀,万一我租用的JSP主机,照这个原理来讲,别人可以访问的我WEB主目录,会不会对我的数据造成威胁?