servlet scope objects in three

// scope object in the servlet is used to make traditional values between pages (servlet)

// scope object                                                     lifecycle                                                           scope

All servlet requests once again request through the user-initiated // request
a session after a session of all the servlet // session with the server browser
// application from the server to close all servlet can access the server to start

① request

//设置
request.setAttribute("name", "zhangsan");

// request by forwarding the request to obtain the value set
request.getRequestDispatcher ( "ts02") forward ( request, response).;


② session

//设置
request.getSession().setAttribute("age", 22);

//获取
Object age = request.getSession().getAttribute("age");
System.out.println(age);


③ application/ServletContext

// set
request.getServletContext () setAttribute ( "address" , " Tianhe District").;

//获取
Object address = request.getServletContext().getAttribute("address");
System.out.println(address);

Guess you like

Origin www.cnblogs.com/su-chu-zhi-151/p/11201168.html