4锛?pageContext锛? 淇濆瓨鐨勯敭鍊間粎鍦ㄦ湰涓〉闈㈡湁鏁堛€傚湪鏈潵瀛︿範Taglib杩囩▼褰撲腑锛屽皢鍙戞尌宸ㄥぇ浣滅敤銆傜被鍙橀噺琚墍鏈夌敤鎴?娴忚鍣?鍙湪杩欎竴椤墊椂鍏變韓锛堜緥濡備緥1.1锛夛紝鑰宲ageContext 琚煇涓敤鎴鳳紙娴忚鍣級鍙湪杩欎竴椤墊椂鎵嶆湁銆俻ageContext鑼冨洿姣旂被鍙橀噺灏忥紝鍜屽眬閮ㄥ彉閲忔槸涓€鏍風殑锛屼絾灞€閮ㄥ彉閲忓彲浠ュ湪闈瀞ervice鐨勬柟娉曚腑鐢紝鑰?pageContext鍙兘鍦╯ervice鏂規硶涓敤銆?瑙佷緥瀛?.4
5锛夊眬閮ㄥ彉閲忥細杞寲鎴恠ervlet鍚庣殑鏌愪釜鏂規硶涓殑灞€閮ㄥ彉閲忋€?
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);
/*濡傜敤涓嬮潰鐨剅esponse锛宺equest灏卞彇涓嶅嚭鏉ヤ簡銆?缁撴灉灏卞彉鎴愬涓嬩簡 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);
}
}
姣旇緝pageContext锛岀被鍙橀噺鍜屽眬閮ㄥ彉閲忥紝涓嬮潰缁欏嚭涓€涓緥瀛愩€?
渚?2.4
<%@ page contentType="text/html; charset=GBK"%>
<html>
<body bgcolor="#ffffff">
<%!double called() {
int c=9;//灞€閮ㄥ彉閲忓彲浠ユ斁鍦╯ervice澶栫殑鏂規硶閲屻€?
/*涓嬩竴鍙ラ敊璇紝鍥犱負pageContext鍙兘鏀懼湪service閲岄潰锛屽闈an not be resolved.*/
// pageContext.getAttribute("abc");
return Math.random();
}%>
<%!int a = 3;%>
<%
int b=4;
if (pageContext.getAttribute("abc") != null) {
pageContext.setAttribute("abc", "xyz1");
} else {
pageContext.setAttribute("abc", "xyz");
}
%>
<%
if(b==4) System.out.println(b+" is still 4");
b++;
System.out.println(a++);
/*you can not write System.out.println(abc); it will report error by drawing the red line*/
System.out.println(pageContext.getAttribute("abc"));
%>
<h1>JBuilder Generated JSP</h1>
</body>
</html>
鏇村璇風湅锛歨ttps://blog.csdn.net/qq_44594371/article/details/103167781