import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.*;
public class ShowSession extends HttpServlet{
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html;charset=gbk");
String title="Http Session Track Example!";
PrintWriter out=resp.getWriter();
HttpSession session=req.getSession(true);
String heading=null;
Integer acessCount =(Integer ) req.getAttribute("acessCount");
if(acessCount == null){
acessCount=new Integer(0);
heading="Welcome NewComer";
}else{
heading="Welcome Back!";
acessCount=new Integer(acessCount.intValue()+1);
}
req.setAttribute("acessCount", acessCount);
out.println("<html><head><title>" +title+
"</title></head>" +
"<body>" +"<h1>"+heading+"</h1>"+
"ID:"+session.getId()+
"<br>"+
session.getCreationTime()+
"<br>"+
session.getLastAccessedTime()+
"<br>"+
"AcessCount"+
acessCount+
"</body></html>");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req,resp);
}
}
这段代码是我用来测试Session的。
而且我已经在Eclipse中导入了servlet包了,第一次Eclipse还会提示类不存在,我关掉Eclipse后重新进入就没有提示了,但是,在浏览器上测试Session 的时候会出现
505的错误,应该是类不存在的原因。我也拷了一个servlet包到我项目的lib目录下,还是没有解决。
请教高手!!