获取方法:
ServletContext application = this.getServletContext();
ServletContext application1 = request.getServletContext();
作用
当做容器,保存共享信息(版本,访问量)
//放入信息
application.setAttribute( “appname”,“chen” );
application.setAttribute( “version”,“1.0” );
//读取信息
ServletContext app1 = this.getServletContext();
Object appname = app1.getAttribute( “appname” );
Object version = app1.getAttribute( “version” );
获取真实路径
String realPath = application.getRealPath( “/” );
获取服务器的名称
String serverInfo = application.getServerInfo();
获取上下文路径
String contextPath = application.getContextPath();
**获取访问次数**
ServletContext application = request.getServletContext();
Integer count = (Integer) application.getAttribute( "count" );
if (count == null) {
count = 1;
application.setAttribute( "count", count );
} else {
count++;
application.setAttribute( "count", count );
}
PrintWriter out = response.getWriter();
out.write( "servlet共访问次数:" + count );
}