如果是在使用servlet,那么可以使用HttpServletRequest这个类的方法:
HttpSession getSession()
Returns the current session associated with this request, or if the request does not have a session, creates one.
HttpSession getSession(boolean create)
Returns the current HttpSession associated with this request or, if there is no current session and create is true, returns a new session.
如果是使用JSP,那么有一个内置对象,就是session.