1、applicationhtml
String getContextPath():獲取虛擬路徑
String getRealPath():獲取虛擬路徑對應的絕對路徑java
實例瀏覽器
application.jsp服務器
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%="當前目錄的虛擬路徑:"+application.getContextPath()+"<br/>" %>
<%="當前虛擬路徑"+application.getContextPath()+"對應的絕對路徑:"+"<br/>"+application.getRealPath(application.getContextPath()) %>
</body>
</html>
下面是測試截圖session
/Myjsp爲默認的路徑,能夠本身修改app
進行添加虛擬路徑便可jsp
2、四大範圍對象
pageContext:JSP頁面容器(page對象) ,當前頁面有效(跳轉頁面無效)
request:請求對象,同一次請求有效(請求轉發有效,重定向無效)
session:會話對象,同一次會話有效(換瀏覽器無效)
application:全局對象,全局有效(整個項目運行期間有效,切換瀏覽器依然有效,重啓Tomcat就無效)
以上四個對象共有的方法
Object getAttribute(String name):根據屬性名,或者屬性值。
void setAttribute(String name,Object obj):設置屬性值(新增,修改)。
//若是name對象以前不存在,則建立一個name對象,並將obj的值賦給name。
//若是name對象以前存在,則將name的值改成obj。
void removeAttribute(String name):根據屬性名,刪除對象 測試
一、pageContext實例this
pageContext.jspspa
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <% //建立name對象 pageContext.setAttribute("name", "zzw"); //在控制檯打印name的值 System.out.println(pageContext.getAttribute("name")); request.getRequestDispatcher("pc1.jsp").forward(request, response); %> </body> </html>
pc1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <% //在控制檯打印頁面跳轉後的name值 System.out.println(pageContext.getAttribute("name")); %> </body> </html>
在跳轉後name值爲null,說明pageContext旨在當前頁面有效
二、request實例
request.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <% //建立name對象 request.setAttribute("name", "zzw"); //在控制檯打印name的值 System.out.println(request.getAttribute("name")); //請求轉發語句 //request.getRequestDispatcher("rq1.jsp").forward(request, response); //重定向語句 response.sendRedirect("rq1.jsp"); %> </body> </html>
rq1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <% //在控制檯打印頁面跳轉後的name值 System.out.println(request.getAttribute("name")); %> </body> </html>
使用請求轉發語句進行頁面跳轉
使用重定向進行頁面跳轉
經過上述測試能夠得出結果,同一次請求有效(請求轉發有效,重定向無效)
三、session實例
session.jsp
ss1.jsp
在同一瀏覽器的不一樣頁面打開都有效
換用Chrome後,name值爲null,說明失效
session在同一次會話中有效。
四、application實例
application.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <% //建立name對象 application.setAttribute("name", "zzw"); //在瀏覽器打印name的值 out.print(application.getAttribute("name")); //請求轉發語句 //request.getRequestDispatcher("al1.jsp").forward(request, response); //重定向語句 response.sendRedirect("al1.jsp"); %> </body> </html>
al1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> al1.jsp<br/> <% //在瀏覽器打印頁面跳轉後的name值 out.print(application.getAttribute("name")); %> </body> </html>
在Firefox有效
換用Chrome依然有效,說明application是全局有效。(整個項目運行期間有效,切換瀏覽器依然有效)
重啓Tomcat就無效。
以上的四個對象
一、經過setAttribute()來保存,經過getAttribute()得到
二、上述範圍對象,儘可能使用最小的範圍 ,由於對象的範圍越大,形成的損耗越大
3、其他jsp內置對象
一、config:配置對象(服務器配置信息) 二、 pageContext:JSP頁面容器 三、page:當前JSP頁面對象(至關於java中的this) 4 、exception:異常對象