import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
import java.util.Date;
public class TestServletContext extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=gb2312");
PrintWriter out = response.getWriter();
ServletContext application = this.getServletContext();
Integer accessCount = (Integer) application.getAttribute("accessCount");
if (accessCount == null) {
accessCount = new Integer(0);
} else {
accessCount = new Integer(accessCount.intValue() + 1);
}
// Use setAttribute instead of putValue in version 2.2.
application.setAttribute("accessCount", accessCount);
out.println("<html><head><title>Session追踪</title></head>"
+ "<BODY BGCOLOR=\"#FDF5E6\">\n" + "<H1 ALIGN=\"CENTER\">"
+ accessCount + "\n" + "</TABLE>\n" + "</BODY></HTML>"
+ "</H1>\n");
}
}
servlet application
最新推荐文章于 2022-07-03 23:17:21 发布
本文展示了一个使用Java Servlet API的示例程序,通过ServletContext对象记录页面访问次数。该程序继承了HttpServlet类并覆盖了doGet方法,在每次HTTP GET请求时更新并显示访问计数。
1206

被折叠的 条评论
为什么被折叠?



