public Object getAttribute(String name)Returns the object bound with the specified name in this session, or null if no object is bound under the name. public boolean isNew()Returns true if the client does not yet know about the session or if the client chooses not to join the session. For example, if the server used only cookie-based sessions, and the client had disabled the use of cookies, then a session would be new on each request. Returns: true if the server has created a session, but the client has not yet joined 你session.isNew() 多余
会不会是jsp页面有问题?
改称: public void init() throws ServletException {
} public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter(); HttpSession session = request.getSession(true);//利用session中的isFirst判断是否第一次进入此页面。
if (session.isNew() || session.getAttribute("isFirst") == null) {
System.out.println("初始化变量");
session.setAttribute("isFirst", "1");
}
else{
System.out.println("变量已经进行初始化");
}
} }
public boolean isNew()Returns true if the client does not yet know about the session or if the client chooses not to join the session. For example, if the server used only cookie-based sessions, and the client had disabled the use of cookies, then a session would be new on each request. Returns:
true if the server has created a session, but the client has not yet joined 你session.isNew() 多余
说明不是session传递的问题
我估计是楼主程序中其他地方引起
这段代码本身应该没有问题