JSP的九個隱含對象以及4個域對象

一、JSP中的九個隱含對象
jsp中的九個隱含對象是:request , response ,
pageContext , session , application ,config ,out,page ,exception 這九個隱含變量。也就是說在jsp頁面中使用這些變量是不需要聲明的。

下面是對這九個變量的簡單說明:

  • (1)request :request就是Servlet 中的HtppservletRequest ,代表客戶端的請求,是常用的對象。作用是:取得請求參數。
  • (2)response :response 就是Servlet 中的HtppservletResponse ,代表客戶端的響應。
  • (3)pageContext:
  • (4)session :對象代服務器和客戶端所建立的會話,當需要在不同的jsp頁面中保留客戶信息的情況下使用,。
  • (5)application :application 其實就是ServletContext 的一個對象實例。代表當前的web應用,對象負責提供應用程序在服務器上運行時的一些全局信息。在當前的web應用中有效。
  • (6)config :對象提供一些配置信息。獲取jsp在web.xml中的初始化參數。
  • (7)out :輸出到瀏覽器,out.println(“要輸出的內容”),在jsp中是比較常用的。
  • (8)page :代表了正在運行的由jsp文件產生的對應的servlet對應的servlet對象。
  • (9)exception :此對象不能在一般jsp文件中直接使用,只有在<%@ page isErrorPage=“true”%>的jsp文件中使用。

二、四個域對象及其屬性有關的方法
pageContext ,request ,session ,application 這四個對象稱爲JSP中的四個域對象。

  • (1)setAttribute(name , name):用於設置屬性。
  • (2)getAttribute(name):根據屬性的名字獲取屬性的值。
  • (3)getgetAttributeNames():獲取所有屬性的名字,以枚舉類型返回。
  • (4)removeAttribute() : 移除指定名字的屬性。
  • (5)測試:同一個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>
     <%
         //pageContext
         pageContext.setAttribute("pageContext", "pageContextText");
     
         //request
         request.setAttribute("request", "requestText");
         
         //session
         session.setAttribute("session", "sessionText");
         
         //application
         application.setAttribute("application", "applicationText");
        
     %>
     
     <%= pageContext.getAttribute("pageContext")%><br/><br/>
     <%= request.getAttribute("request")%><br/><br/>
     <%= session.getAttribute("session")%><br/><br/>
     <%= application.getAttribute("application")%><br/><br/>
</body>
</html>

測試結果
在這裏插入圖片描述

  • (6)測試:不同的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>
     <%
         //pageContext
         pageContext.setAttribute("pageContext", "pageContextText");
     
         //request
         request.setAttribute("request", "requestText");
         
         //session
         session.setAttribute("session", "sessionText");
         
         //application
         application.setAttribute("application", "applicationText");
        
     %>
     <a href="index2.jsp">跳轉index2.jsp</a>
</body>
</html>
  • 第二個頁面
<%@ 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>
       <%= pageContext.getAttribute("pageContext")%><br/><br/>
       <%= request.getAttribute("request")%><br/><br/>
       <%= session.getAttribute("session")%><br/><br/>
       <%= application.getAttribute("application")%><br/><br/>
</body>
</html>

測試結果:
在這裏插入圖片描述

  • (7)測試:在servlet中取值
<%@ 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>
     <%
         //pageContext
         pageContext.setAttribute("pageContext", "pageContextText");
     
         //request
         request.setAttribute("request", "requestText");
         
         //session
         session.setAttribute("session", "sessionText");
         
         //application
         application.setAttribute("application", "applicationText");
        
     %>
     <a href="AuthorServlet">跳轉到AuthorServlet</a>
     
</body>
</html>
  • AuthorServlet.java 。 注意: pageContext , 在servlet裏邊不能使用。
package com.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class AuthorServlet
 */
@WebServlet("/AuthorServlet")
public class AuthorServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AuthorServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// pageContext , 在servlet裏邊不能使用
		
		//request
		response.getWriter().println( request.getAttribute("request") );
		
		//session
		response.getWriter().println( request.getSession().getAttribute("session") );
		
		//application
		response.getWriter().println(getServletContext().getAttribute("application") );
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

  • 測試結果:
    在這裏插入圖片描述
  • (8)總結:
    • pageContext : 只能在應用在當前頁面的一次請求中。
    • request : 只要在同一個請求中,不論該請求經過多少個動態資源,只能是轉發。
    • session :只要在同一個會話中,不論該請求經過多少個動態資源,不論轉發還是重定向。
    • application :只要在當前web應用中,只要服務器不關閉,一直存在。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章