四大作用域
要先知道四大作用域是什么,怎么運(yùn)作的陵珍。
什么是四大作用域非竿?(這部分知識(shí)在jsp基礎(chǔ)里面有提到過(guò),jsp里面有九個(gè)內(nèi)置對(duì)象酬凳,其中就有四個(gè)域?qū)ο螅?br>
JSP中的四大作用域
page作用域
request作用域
session作用域
application作用域
這四個(gè)域?qū)ο缶褪窃趈sp和servlet類文件之間保存數(shù)據(jù)惠况、獲取數(shù)據(jù)、傳遞數(shù)據(jù)的對(duì)象
這四個(gè)域?qū)ο蟮淖饔梅秶煌校虼朔Q作四個(gè)作用域稠屠。
四個(gè)域?qū)ο笤斀?/h1>
想記清楚作用域,需要明白域?qū)ο蟮脑眙嵘弧6胍宄驅(qū)ο笕ú海梢酝ㄟ^(guò)看jsp實(shí)際在服務(wù)器上編譯成的java文件。
Tips:如何找到j(luò)sp的java文件煎谍?
在部署在服務(wù)器上的項(xiàng)目目錄下攘蔽,例如我的就在我的tomcat服務(wù)器下:
D:\Tomacat\apache-tomcat-8.5.43\work\Catalina\localhost\http_req_resp\org\apache\jsp\
首先先看我的test.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
request.setAttribute("3", 3);
%>
<%request.getRequestDispatcher("d1").forward(request, response); %>
</body>
</html>
它的java文件代碼是這樣的:
/*
* Generated by the Jasper component of Apache Tomcat
* Version: Apache Tomcat/8.5.43
* Generated at: 2019-08-14 08:05:37 UTC
* Note: The last modified time of this file was set to
* the last modified time of the source file after
* generation to assist with modification tracking.
*/
package org.apache.jsp;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
public final class testArea_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent,
org.apache.jasper.runtime.JspSourceImports {
private static final javax.servlet.jsp.JspFactory _jspxFactory =
javax.servlet.jsp.JspFactory.getDefaultFactory();
private static java.util.Map<java.lang.String,java.lang.Long> _jspx_dependants;
private static final java.util.Set<java.lang.String> _jspx_imports_packages;
private static final java.util.Set<java.lang.String> _jspx_imports_classes;
static {
_jspx_imports_packages = new java.util.HashSet<>();
_jspx_imports_packages.add("javax.servlet");
_jspx_imports_packages.add("javax.servlet.http");
_jspx_imports_packages.add("javax.servlet.jsp");
_jspx_imports_classes = null;
}
private volatile javax.el.ExpressionFactory _el_expressionfactory;
private volatile org.apache.tomcat.InstanceManager _jsp_instancemanager;
public java.util.Map<java.lang.String,java.lang.Long> getDependants() {
return _jspx_dependants;
}
public java.util.Set<java.lang.String> getPackageImports() {
return _jspx_imports_packages;
}
public java.util.Set<java.lang.String> getClassImports() {
return _jspx_imports_classes;
}
public javax.el.ExpressionFactory _jsp_getExpressionFactory() {
if (_el_expressionfactory == null) {
synchronized (this) {
if (_el_expressionfactory == null) {
_el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
}
}
}
return _el_expressionfactory;
}
public org.apache.tomcat.InstanceManager _jsp_getInstanceManager() {
if (_jsp_instancemanager == null) {
synchronized (this) {
if (_jsp_instancemanager == null) {
_jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());
}
}
}
return _jsp_instancemanager;
}
public void _jspInit() {
}
public void _jspDestroy() {
}
public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)
throws java.io.IOException, javax.servlet.ServletException {
final java.lang.String _jspx_method = request.getMethod();
if (!"GET".equals(_jspx_method) && !"POST".equals(_jspx_method) && !"HEAD".equals(_jspx_method) && !javax.servlet.DispatcherType.ERROR.equals(request.getDispatcherType())) {
response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "JSPs only permit GET POST or HEAD");
return;
}
final javax.servlet.jsp.PageContext pageContext;//pageContext是一個(gè)Servlet包中的pageContext的實(shí)例
javax.servlet.http.HttpSession session = null;//session是一個(gè)HttpSession實(shí)例
final javax.servlet.ServletContext application;//application是一個(gè)ServletContext實(shí)例
final javax.servlet.ServletConfig config;
javax.servlet.jsp.JspWriter out = null;
final java.lang.Object page = this;
javax.servlet.jsp.JspWriter _jspx_out = null;
javax.servlet.jsp.PageContext _jspx_page_context = null;//這個(gè)是真正的page域?qū)ο?
try {
response.setContentType("text/html; charset=ISO-8859-1");
pageContext = _jspxFactory.getPageContext(this, request, response,
null, true, 8192, true);//把頁(yè)面的信息用pageContext來(lái)引用
_jspx_page_context = pageContext;//真正的page域?qū)ο螅。? application = pageContext.getServletContext();//把原本服務(wù)器中的servletContext實(shí)例用application來(lái)引用
config = pageContext.getServletConfig();
session = pageContext.getSession();//把會(huì)話中new的session對(duì)象用session來(lái)引用
out = pageContext.getOut();
_jspx_out = out;
out.write("\r\n");
out.write("<!DOCTYPE html>\r\n");
out.write("<html>\r\n");
out.write("<head>\r\n");
out.write("<meta charset=\"ISO-8859-1\">\r\n");
out.write("<title>Insert title here</title>\r\n");
out.write("</head>\r\n");
out.write("<body>\r\n");
out.write("<h1>ddddd</h1>\r\n");
request.setAttribute("3", 3);
out.write('\r');
out.write('\n');
request.getRequestDispatcher("d1").forward(request, response);
out.write("\r\n");
out.write("\r\n");
out.write("</body>\r\n");
out.write("</html>");
} catch (java.lang.Throwable t) {
if (!(t instanceof javax.servlet.jsp.SkipPageException)){
out = _jspx_out;
if (out != null && out.getBufferSize() != 0)
try {
if (response.isCommitted()) {
out.flush();
} else {
out.clearBuffer();
}
} catch (java.io.IOException e) {}
if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
else throw new ServletException(t);
}
} finally {
_jspxFactory.releasePageContext(_jspx_page_context);//釋放掉page域?qū)ο蟆? }
}
}
可從上看出:
- application只是單純的引用了系統(tǒng)中已經(jīng)存在的servletContext對(duì)象呐粘;session只是單純的引用了每次會(huì)話中都只創(chuàng)建一次的session對(duì)象满俗;page域?qū)ο笠仓皇且粋€(gè)單純的引用,但是因?yàn)樗詈蟊会尫帕俗麽灰虼怂麄兊淖饔梅秶@而易見
- pageContext對(duì)象是jsp的上下文唆垃,可以獲取到servletContext、session等已經(jīng)存在的對(duì)象痘儡,這樣就能實(shí)現(xiàn)他們的作用范圍
- request對(duì)象和response對(duì)象是用戶的請(qǐng)求和應(yīng)答降盹,可以被轉(zhuǎn)發(fā),轉(zhuǎn)發(fā)還是那個(gè)對(duì)象;但是如果是重定向了蓄坏,那就是傳回客戶端再次請(qǐng)求的過(guò)程价捧,就不是同一個(gè)request了。
常說(shuō)的jsp往servlet三種傳值方式
- Form表單傳值涡戳,
- url傳值
3.session傳值
實(shí)際上application也就是servletContext也可以作為傳值结蟋,但是不太好;而request域?qū)ο笕绻獋髦悼梢允强梢杂嬲茫钱?dāng)你在jsp頁(yè)面上轉(zhuǎn)發(fā)到servlet的時(shí)候這個(gè)jsp界面所有東西都會(huì)一下子閃過(guò)嵌屎,內(nèi)容不會(huì)停留在瀏覽器上,這樣這個(gè)jsp就沒(méi)有存在的必要恍涂。
servlet給jsp傳值
重定向
這種方式要傳值出去的話宝惰,只能在url中帶parameter或者放在session中,無(wú)法使用request.setAttribute來(lái)傳遞再沧。轉(zhuǎn)發(fā)
使用這種方式跳轉(zhuǎn)尼夺,傳值可以使用三種方法:使用url中帶parameter或使用session或使用request.setAttribute
總結(jié)
- 其實(shí)通過(guò)四個(gè)域?qū)ο髠鲄⒌脑砗芎?jiǎn)單:
同一個(gè)對(duì)象數(shù)據(jù)就不會(huì)丟,不同的對(duì)象就不能傳參啦炒瘸! - 平常開發(fā)就使用常用的幾種傳值方式就好