今天学习了Servlet的基础知识,学习了一个统计訪问量的小案例,记录一下
package cn.selevet_01; import java.io.IOException; import java.io.InputStream; 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 Servlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletContext sc = this.getServletContext(); Integer ct=(Integer)sc.getAttribute("count"); if(ct==null) { sc.setAttribute("count", 1); } else{ sc.setAttribute("count", ct+1); } System.out.println("訪问量为:"+sc.getAttribute("count")); } }