本系列博客汇总在这里:JSP 汇总
目录
JSP 内置对象
-
什么是内置对象(面试常问)
内置对象是在 JSP 页面中无需创建就可以直接使用的变量。在 JSP 中一共有 9 个这样的对象!
它们分别是:
out(JspWriter);
config(ServletConfig);
page(当前JSP的真身类型);
pageContext(PageContext);
exception(Throwable);
request(HttpServletRequest);
response(HttpServletResponse);
application(ServletContext);
Session (HttpSession)。 -
隐藏对象概述
out
:最为常用的方法是 print(),向页面输出!它与 response.getWriter() 基本相同!config
:在页面中基本没有什么用,但如果 JSP 在 web.xml 中存在配置,而且存在初始化参数,那么可以使用 config 来获取;config 对象的功能是:getServletContext()、getServletName()、getInitParameter(),这三个方法在 JSP 中都没什么用。所以config 就没什么用了。JSP 也可以在 web.xml 中配置,但我们没有配置它的必要!page
:基本没用!表示当前 JSP 页面的当前实例!在页面中使用 this 和使用 page 是相同的;也没什么用。request
:与 Servlet 中的 request 一样,没有区别;response
:与 Servlet 中的 response 一样,没有区别;application
:就是 ServletContext 对象;session
:就是 HttpSession 对象;exception
:虽然名字叫 exception,但类型为 Throwable。它只能在错误页中可以使用!后面讲。pageContext
:很有用的东西,下面会讲解。你以后可能不会经常直接使用它,但一定间接使用它。 -
对照 JSP 真身查看内置对象
我们发现,在 JSP 中的内容会出现在“真身”的 _jspService() 方法中,而且在 _jspService() 方法上方还有一些其实代码:public void _jspService(HttpServletRequest request, HttpServletResponse response) throws java.io.IOException, ServletException { PageContext pageContext = null; HttpSession session = null; ServletContext application = null; ServletConfig config = null; JspWriter out = null; Object page = this; JspWriter _jspx_out = null; PageContext _jspx_page_context = null; try { response.setContentType("text/html;charset=UTF-8"); pageContext = _jspxFactory.getPageContext(this, request, response,null, true, 8192, true); _jspx_page_context = pageContext; application = pageContext.getServletContext(); config = pageContext.getServletConfig(); session = pageContext.getSession(); out = pageContext.getOut(); _jspx_out = out; //从这里开始,才是JSP页面的内容 }...
JSP 中的内容都出现在 try 块中,而且在 try 块的正文!上方是对隐藏对象的初始化!!!上面代码只是给出普通页面的 “真身”,而错误页面的 “真身” 你会看到 exception 对象的出现。
-
JSP 四个域对象
域的范围和属性管理①
pageContext 范围: 当前页面之内有效。<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <% pageContext.setAttribute("name", "weiyuxuan"); pageContext.setAttribute("birthday", new Date()); %> <% String name = (String)pageContext.getAttribute("name"); Date birthday = (Date)pageContext.getAttribute("birthday"); %> <h1>name: <%=name %></h1> <h1>birthday: <%=birthday %></h1> </body> </html>
②
request 范围:当前的请求内有效。
index1.jsp<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <% request.setAttribute("name", "魏宇轩"); request.setAttribute("birthday", new Date()); %> <jsp:forward page="/success.jsp"></jsp:forward> </body> </html>
success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <% String name = (String)request.getAttribute("name"); Date birthday = (Date)request.getAttribute("birthday"); %> <h1>name: <%=name %></h1> <h1>birthday: <%=birthday %></h1> </body> </html>
③
session 范围:当前的会话内有效。
index2.jsp<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <% session.setAttribute("name", "wyx"); session.setAttribute("birthday", new Date()); %> <jsp:forward page="/success1.jsp"></jsp:forward> </body> </html>
success1.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <% String name = (String)session.getAttribute("name"); Date birthday = (Date)session.getAttribute("birthday"); %> <h1>name: <%=name %></h1> <h1>birthday: <%=birthday %></h1> </body> </html>
④
application 范围:当前这次服务器生命周期内有效。
index3.jsp<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <% application.setAttribute("name", "liuchengjuan"); application.setAttribute("birthday", new Date()); %> </body> </html>
success2.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <% String name = (String)application.getAttribute("name"); Date birthday = (Date)application.getAttribute("birthday"); %> <h1>name: <%=name %></h1> <h1>birthday: <%=birthday %></h1> </body> </html>
-
域对象的共同特点是都管理域中的属性,他们有着相同的方法。
void setAttribute(String name, Object value); Object getAttrbiute(String name, Object value); void removeAttribute(String name, Object value);
如有错误,欢迎指正!