<%@ page language="java" contentType="text/html; charset=UTF-8"%> pageContext

pageContext.set|get|findAttribute 设置、提取、查找属性


<% //获取request对象 HttpServletRequest req = (HttpServletRequest) pageContext .getRequest(); // pageContext包含Request和Response // HttpServletResponse resp = (HttpServletResponse) pageContext.getResponse(); //设置page范围内属性 pageContext.setAttribute("书名", "《Java程序设计》",pageContext.PAGE_SCOPE); //设置request范围内属性 req.setAttribute("书名", "《Java Web程序设计》"); pageContext.setAttribute("书名2", "《Java Web程序设计》(第2版)",pageContext.REQUEST_SCOPE); //设置session范围内属性 pageContext.setAttribute("图书名", "《Java Sesion详解》", pageContext.SESSION_SCOPE); //获得的page范围属性 String str1 = (String)pageContext.getAttribute("书名", pageContext.PAGE_SCOPE); //获得的request范围属性 String str2 = (String)pageContext.getAttribute("书名", pageContext.REQUEST_SCOPE); String str3 = (String)pageContext.getAttribute("书名2", pageContext.REQUEST_SCOPE); //获得的request范围属性 String str4 = (String)pageContext.getAttribute("图书名", pageContext.SESSION_SCOPE); //获得的默认范围属性 String str5 = (String)pageContext.getAttribute("书名"); String str6 = (String)pageContext.findAttribute("书名"); %> <%="page范围 getAttribute(\"书名\"):"+str1 %>
<%="request范围 getAttribute(\"书名\"):"+str2 %>
<%="request范围 getAttribute(\"书名2\"):"+str3 %>
<%="session范围 getAttribute(\"图书名\"):"+str4 %>
<%="pageContext.getAttribute(\"书名\"):"+str5 %>
<%="pageContext.findAttribute(\"书名\"):"+str6 %>