时间:2023-09-26 03:42:01 | 来源:网站运营
时间:2023-09-26 03:42:01 来源:网站运营
大数据从入门到深入:JavaEE 之 动态网页开发基础 JSP内置对象与对象作用域:第五章 JSP拓展 : JSP内置对象与对象作用域${pageContext.request.contextPath}
通过这个参数获取项目的名称,这样就很方便的可以定位网站元素的位置了; pageContext 除了能够帮助我们获取对象数据之外,还能够保存数据信息;<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>测试作用域</title></head><body><% pageContext.setAttribute("msg", "Hello,PageContext!");%><div> <h1>${msg}</h1></div></body></html>
效果 : <% pageContext.setAttribute("msg", "Hello,PageContext!"); request.setAttribute("msg1", "Hello,Request!");%><div> <h1>${msg}</h1> <h1>${msg1}</h1></div>
但是要是转发到下一个页面就只有request的值能获取到了<% pageContext.setAttribute("msg", "Hello,PageContext!"); request.setAttribute("msg1", "Hello,Request!"); request.getRequestDispatcher("page1.jsp").forward(request, response);%>
page1.jsp:<div> <h1>${msg}</h1> <h1>${msg1}</h1></div>
通过上面的案例我们不难发现,pageContext它的存值范围只有当前页面,所以它的作用域是 非常小的; <% request.getRequestDispatcher("page2.jsp").forward(request, response);%>
page2.jsp<div> <h1>${msg}</h1> <h1>${msg1}</h1></div>
效果:<% // request.getRequestDispatcher("page2.jsp").forward(request, response); response.sendRedirect("page2.jsp");%>
效果 : <% pageContext.setAttribute("msg", "Hello,PageContext!"); request.setAttribute("msg1", "Hello,Request!"); session.setAttribute("msg2", "Hello,Session!"); request.getRequestDispatcher("page1.jsp").forward(request, response);%>
在page2.jsp获取这几个值,注意现在page1.jsp还是以重定向的方式向page2.jsp发起请求 <% // request.getRequestDispatcher("page2.jsp").forward(request, response); response.sendRedirect("page2.jsp");%>
page2.jsp<div> <h1>${msg}</h1> <h1>${msg1}</h1> <h1>${msg2}</h1></div>
效果<% pageContext.setAttribute("msg", "Hello,PageContext!"); request.setAttribute("msg1", "Hello,Request!"); session.setAttribute("msg2", "Hello,Session!"); application.setAttribute("msg3", "Hello,Application!"); request.getRequestDispatcher("page1.jsp").forward(request, response);%>
page1.jsp <% // request.getRequestDispatcher("page2.jsp").forward(request, response); response.sendRedirect("page2.jsp");%>
page2.jsp <div> <h1>${msg}</h1> <h1>${msg1}</h1> <h1>${msg2}</h1> <h1>${msg3}</h1></div>
访问index.jsp 测试结果<% pageContext.setAttribute("msg", "Hello,PageContext!"); request.setAttribute("msg1", "Hello,Request!"); session.setAttribute("msg2", "Hello,Session!"); application.setAttribute("msg3", "Hello,Application!"); Cookie c = new Cookie("msg4","Hello,Cookie!"); c.setMaxAge(3600); response.addCookie(c); request.getRequestDispatcher("page1.jsp").forward(request, response);%>
此处注意设置cookie的有效期,如果不设置的话cookie瞬间就没有了; <% // request.getRequestDispatcher("page2.jsp").forward(request, response); response.sendRedirect("page2.jsp");%>
page2.jsp<div> <% String val = ""; Cookie[] cookies = request.getCookies(); for(Cookie c : cookies){ if(c.getName().equals("msg4")){ val = c.getValue(); } } %> <h1>${msg}</h1> <h1>${msg1}</h1> <h1>${msg2}</h1> <h1>${msg3}</h1> <h1> <%=val %> </h1></div>
访问效果 : 关键词:对象,基础,作用,入门,数据,深入,动态