理论
类名域名功能ServletContextcontext域 在整个服务器上保存,所有用户都可使用。 重启服务器后无效JSP内置对象HttpSessionsession域再一次会话中有效。服务器跳转、客户端跳转都有效。 网页关闭重新打开无效HttpServletRequetrequest域只在一次请求中有效,服务器跳转之后有效。 客户端跳无效PageContextpage域只在一个页面中保存属性。 跳转之后无效。
域对象的作用:保存数据,获取数据,共享数据.
可以往这4个属性范围对象内部放置东西,然后在别的地方取出来使用。使用的时候谁的范围小谁优先。
示例1:
示例2:
login
示例3:
示例4:
-
Servlet代码:
@WebServlet("/demo") public class DemoServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //this指当前类的对象 //this.getServletContext这个方法的返回值对象就是application对象 ServletContext application = this.getServletContext(); application.setAttribute("k","application"); //获取Session对象 HttpSession session = request.getSession(); session.setAttribute("k","session"); request.setAttribute("k","request"); request.getRequestDispatcher("demo.jsp").forward(request,response); } }
-
JSP页面代码
Title fdsfdsfsd