01_9_ServletContext
1. 例子
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
ServletContext application = this.getServletContext();
Integer accessCount = (Integer) application.getAttribute("accessCount");
if ( accessCount == null) {
accessCount = new Integer(0);
} else {
accessCount = new Integer(accessCount.intValue() + 1 );
}
application.setAttribute("accessCount", accessCount);
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">");
out.println("<HTML>");
out.println("<HEAD><TITLE>Servelet Context</TITLE></HEAD>");
out.println("<BODY><H1 align="CENTER">" + accessCount);
out.println("</H1></BODY>");
out.println("</HTML>");
out.flush();
out.close();
}