import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class IndexServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public IndexServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext ctx = this.getServletContext();
synchronized(this){
Integer counter = (Integer)ctx.getAttribute("UserNumber");
int tmp = 0;
if(counter==null){
counter = new Integer(1);
}else{
tmp=counter.intValue()+1;
counter = new Integer(tmp);
}
ctx.setAttribute("UserNumber", counter);
}
response.setContentType("
ServletContext示例:使用Servlet上下文保存访问人数
最新推荐文章于 2021-08-24 11:07:20 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)