06920170909

1、ServletContext类:web站点的唯一全局对象

1)、是servlet与servlet容器之间的通信接口;

2)、每个servlet应用都会创建一个ServletContext对象与之对应;

3)、ServletContext对象被web应用下的所有servlet共享;

4)、访问web应用资源。


2、ServletContext的使用步骤:


1)、User user = new User("username","passwrod"); request.getServletContext().setAttribute("user",user);


2)、可以在该站点下的任意一个servlet中获取刚才存放在User中的值。request.getServletContext().getAttribute("user",user);


3、代码如下:


package com.tiger.countAccess;

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.*;

/**
* 06920170909
* 统计浏览访问次数
* @author tiger
* @time 2017年9月9日
*/
@WebServlet("/accessTimesCount")
public class AccessTimesCount extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext application = request.getServletContext();
//获取远程客户端ip
String ip = request.getRemoteAddr();
//将ip存入一个集合中
List

ips = (List

) application.getAttribute("ips");
if (ips == null) {
//如果为空,则创建
ips = new ArrayList<>();
}
System.out.println("ip = "+ip);
if (!ips.contains(ip)) {
if (application.getAttribute("count") == null) {
application.setAttribute("count", 1);
}else {
Integer count = (Integer) application.getAttribute("count");
count++;
application.setAttribute("count", count);
}
ips.add(ip);
application.setAttribute("ips", ips);
}
response.getWriter().append("count = ").
append(String.valueOf(application.getAttribute("count")));
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
package com.tiger.listener;

import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.WebListener;
/**
*
* @author tiger
* @time 2017年9月9日
*/
@WebListener
public class AccessTimesCountListener implements ServletContextListener {

public AccessTimesCountListener() { }

/**
* 在站点[web应用]解构之前,将浏览访问次数存入到一个文本中
*/
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("AccessTimesCountListener.contextDestroyed()");
//取得ServletContext操作对象
ServletContext application = sce.getServletContext();
//取得数据
Integer count = (Integer) application.getAttribute("count");
//创建数据保存路径
String folder = application.getRealPath(File.separator+"count") + File.separator ;
File file = new File(folder);
//如果文件不存在则创建
if (!file.exists()) {
file.mkdirs();
}
//将数据输出存入本应用下的一个文件夹下的文件中
try {
PrintStream ps = new PrintStream(new FileOutputStream(folder +"countAccess.txt"));
ps.print(count);
ps.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @see ServletContextListener#contextInitialized(ServletContextEvent)
*/
public void contextInitialized(ServletContextEvent sce) {
System.out.println("AccessTimesCountListener.contextInitialized()");
ServletContext application = sce.getServletContext();
String folder = application.getRealPath(File.separator+"count")+ File.separator ;
File file = new File(folder);
if(file.exists()) {
try {
BufferedReader reader = new BufferedReader(new FileReader(folder + "countAccess.txt"));
String countstr = reader.readLine();
System.out.println("contextInitialized--count = "+countstr);
Integer count = Integer.parseInt(countstr);
application.setAttribute("count", count);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}