時(shí)間:2023-09-26 03:42:01 | 來(lái)源:網(wǎng)站運(yùn)營(yíng)
時(shí)間:2023-09-26 03:42:01 來(lái)源:網(wǎng)站運(yùn)營(yíng)
大數(shù)據(jù)從入門(mén)到深入:JavaEE 之 動(dòng)態(tài)網(wǎng)頁(yè)開(kāi)發(fā)基礎(chǔ) JSP內(nèi)置對(duì)象與對(duì)象作用域:第五章 JSP拓展 : JSP內(nèi)置對(duì)象與對(duì)象作用域${pageContext.request.contextPath}
通過(guò)這個(gè)參數(shù)獲取項(xiàng)目的名稱,這樣就很方便的可以定位網(wǎng)站元素的位置了? pageContext 除了能夠幫助我們獲取對(duì)象數(shù)據(jù)之外,還能夠保存數(shù)據(jù)信息?<%@ 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>測(cè)試作用域</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>
但是要是轉(zhuǎn)發(fā)到下一個(gè)頁(yè)面就只有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>
通過(guò)上面的案例我們不難發(fā)現(xiàn),pageContext它的存值范圍只有當(dāng)前頁(yè)面,所以它的作用域是 非常小的? <% 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獲取這幾個(gè)值,注意現(xiàn)在page1.jsp還是以重定向的方式向page2.jsp發(fā)起請(qǐng)求 <% // 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>
訪問(wèn)index.jsp 測(cè)試結(jié)果<% 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);%>
此處注意設(shè)置cookie的有效期,如果不設(shè)置的話cookie瞬間就沒(méi)有了? <% // 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>
訪問(wèn)效果 : 關(guān)鍵詞:對(duì)象,基礎(chǔ),作用,入門(mén),數(shù)據(jù),深入,動(dòng)態(tài)
客戶&案例
營(yíng)銷資訊
關(guān)于我們
客戶&案例
營(yíng)銷資訊
關(guān)于我們
微信公眾號(hào)
版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。