您当前的位置: 首页 > 

梁云亮

暂无认证

  • 2浏览

    0关注

    1211博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

JSP中四大域对象

梁云亮 发布时间:2019-12-07 16:19:01 ,浏览量:2

理论 类名域名功能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
    	    
    	
    
    
关注
打赏
1665409997
查看更多评论
立即登录/注册

微信扫码登录

0.0381s