J2EE – Http Session


Very Simple Example。Http Session is stateless, keep track what user is doing.

SessionServlet

public class SersionServlet extends HttpServlet implements Servlet {

HttpSerssions session = request.getSession(false); //takes any two params, false, if user has session, give it to me; true, if user has no session, create one

if(session == null) {

session = reuqest.getSession(true);

session.setAttribute(“count”, hitCount);

}

Integer hitCount = (Integer)session.getAttribte(“count”);

hitCount = new Integer(hitCount.intValue()) + 1;

response.getWriter().print(“Number of times at this page ” + hitCount.intValue() );

}

So, when user hit this servlet, it will be increased by 1 everytime user refreshes the page.

, , ,

  1. No comments yet.
(will not be published)