Đối tượng session trong JSP

Đối tượng session trong JSP

Đối tượng session trong JSP

Learn more »

Đối tượng session trong JSP



Đối tượng session trong JSP là một đối tượng ẩn của javax.servlet.http.HttpSession. Đối tượng session được sử dụng để theo dõi phiên của các request của client.

Lập trình viên Java có thể sử dụng đối tượng này để set, get hoặc remove thuộc tính hoặc để lấy thông tin về phiên làm việc.


Ví dụ về đối tượng session trong JSP

File: index.jsp

 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Doi tuong session trong JSP</title> </head> <body> <form action="welcome.jsp">   <input type="text" name="username">    <input type="submit" value="Submit"> </form> </body> </html> 

File: welcome.jsp

 <%@ page language="java" contentType="text/html; charset=UTF-8"   pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Welcome page</title> </head> <body>   <%       String name = request.getParameter("username");       out.print("Welcome " + name);        session.setAttribute("user", name);   %>   <a href="second.jsp">second jsp page</a> </body> </html> 

File: second.jsp

 <%@ page language="java" contentType="text/html; charset=UTF-8"   pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Second page</title> </head> <body>   <%       String name = (String) session.getAttribute("user");       out.print("Hello " + name);   %> </body> </html> 

Run ứng dụng trên chúng ta có kết quả như sau:

Đối tượng session trong JSP

Click Submit.

Đối tượng session trong JSP

Click Link.

Đối tượng session trong JSP


Lượt xem : 291

Integrations
Users

Share Profile

Anyone at KeenThemes can view
Anyone with link can edit

Give Award

Anyone at KeenThemes can view
Anyone with link can edit

Report User

Let us know why you’re reporing this person
Don't worry, your report is completely anonymous; the person you're
reporting will not be informed that you've submitted it