pageContext和局部变量的区别?

梦想的初衷 提交于 2020-01-06 20:31:09

4) pageContext:   保存的键值仅在本个页面有效。在未来学习Taglib过程当中,将发挥巨大作用。类变量被所有用户(浏览器)只在这一页时共享(例如例1.1),而pageContext 被某个用户(浏览器)只在这一页时才有。pageContext范围比类变量小,和局部变量是一样的,但局部变量可以在非service的方法中用,而 pageContext只能在service方法中用。 见例子2.4
5)局部变量:转化成servlet后的某个方法中的局部变量。
6)类变量:转化成servlet后的类变量。
例 2.3
<%@ page contentType="text/html; charset=GBK" %>
<html>
<body>
<% 
 request.setAttribute("rName","rmark-to-win");
 application.setAttribute("aName","amark-to-win");
 session.setAttribute("sName","smark-to-win");
 request.getRequestDispatcher("/Cookie/AddCookie").forward(request,response);
/*如用下面的response,request就取不出来了。 结果就变成如下了 null amark-to-win smark-to-win*/ 
//  response.sendRedirect("http://localhost:8080/ServletHello/Cookie/AddCookie");
%>
</body>
</html>
package com;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class AddCookie extends HttpServlet {
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
      String rav=(String)request.getAttribute("rName");
      String scs=(String)this.getServletContext().getAttribute("aName");
      String sa=(String)request.getSession().getAttribute("sName");
      System.out.println(rav+" "+scs+" "+sa);
    }
}

更多请见:http://www.mark-to-win.com/index.html?content=Jsp/jspUrl.html&chapter=Jsp/jsp3_web.html#DifferencepageContextLocalClassVariable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!