初始化的时候把一个变量存到ContextServlet中
@Override
public void init() throws ServletException {
//在ServletContext域中存一个数据count
int count = 0;
this.getServletContext().setAttribute("count", count);;
}
登录方法
if(user!=null) {
//从servletcontext中取出count进行++运算
ServletContext context = this.getServletContext();
Integer count = (Integer)context.getAttribute("count");
count++;
//用户登陆成功
response.getWriter().write(user.toString()+"you are zhe "+count+" one login");
context.setAttribute("count", count);
}else {
//用户登录失败
response.getWriter().write("sorry your username or password is wrong...");
}
整体代码
package cn.aynortechnology.login;
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.