What is HttpSession
2016-07-07 10:50
1271 查看
HttpSession object is used to store entire session with a specific client. We can store, retrieve and remove attribute from HttpSession object. Any servlet can have access to HttpSession object throughout the
of the HttpServletRequest object.
On client's first request, the Web Container generates a unique session ID and gives it back to the client with response. This is a temporary session created by web container.
The client sends back the session ID with each request. Making it easier for the web container to identify where the request is coming from.
The Web Container uses this ID, finds the matching session with the ID and associates the session with the request.
All the files mentioned below are required for the example.
index.html
web.xml
Validate.java
Welcome.java
getSession()method
of the HttpServletRequest object.
How HttpSession works
On client's first request, the Web Container generates a unique session ID and gives it back to the client with response. This is a temporary session created by web container.
The client sends back the session ID with each request. Making it easier for the web container to identify where the request is coming from.
The Web Container uses this ID, finds the matching session with the ID and associates the session with the request.
HttpSession Interface
Some Important Methods of HttpSession
Methods | Description |
---|---|
long getCreationTime() | returns the time when the session was created, measured in milliseconds since midnight January 1, 1970 GMT. |
String getId() | returns a string containing the unique identifier assigned to the session. |
long getLastAccessedTime() | returns the last time the client sent a request associated with the session |
int getMaxInactiveInterval() | returns the maximum time interval, in seconds. |
void invalidate() | destroy the session |
boolean isNew() | returns true if the session is new else false |
voidsetMaxInactiveInterval(int interval) | Specifies the time, in seconds,after servlet container will invalidate the session. |
Complete Example demonstrating usage of HttpSession
All the files mentioned below are required for the example.index.html
<form method="post" action="Validate"> User: <input type="text" name="user" /><br/> Password: <input type="text" name="pass" ><br/> <input type="submit" value="submit"> </form>
web.xml
<web-app..> <servlet> <servlet-name>Validate</servlet-name> <servlet-class>Validate</servlet-class> </servlet> <servlet> <servlet-name>Welcome</servlet-name> <servlet-class>Welcome</servlet-class> </servlet> <servlet-mapping> <servlet-name>Validate</servlet-name> <url-pattern>/Validate</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>Welcome</servlet-name> <url-pattern>/Welcome</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>
Validate.java
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class Validate extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); String name = request.getParameter("user"); String pass = request.getParameter("pass"); if(pass.equals("1234")) { //creating a session HttpSession session = request.getSession(); session.setAttribute("user", name); response.sendRedirect("Welcome"); } } }
Welcome.java
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class Welcome extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); HttpSession session = request.getSession(); String user = (String)session.getAttribute("user"); out.println("Hello "+user); } }
相关文章推荐
- java使用HttpSession实现QQ访问记录
- cookie和session
- 关于httpsession
- 浅谈servlet中的session
- spring websocket 获取httpsession
- session机制
- 深入理解 HTTP Session
- sessio的理解
- Session 初步(二) 之 HTTP Session 浅析
- Java Web监听器-学习笔记
- Java Web基础知识之Listener:监控Servlet的每个动作
- servlet的HttpSession与HibernateSession的区别
- HttpSession会话管理原理
- 理解HTTP session原理及应用
- httpsession的原理及负载均衡
- HTTP Cookie/Session
- (Portal开发读书笔记)PortletSession 和 HttpSession
- Liferay中PortletSession的研究
- 深入理解HTTP Session session
- Session统计系统当前在线用户数