首頁  >  文章  >  Java  >  介紹並解釋JSP中的九個內建物件的作用

介紹並解釋JSP中的九個內建物件的作用

PHPz
PHPz原創
2024-01-31 16:03:06947瀏覽

介紹並解釋JSP中的九個內建物件的作用

JSP九大內建物件簡介與作用

1. request

request物件表示客戶端發來的請求,包含客戶端發送的所有訊息,如請求參數、請求頭等。它是一個javax.servlet.http.HttpServletRequest物件。

作用:

  • 取得請求參數:request.getParameter(String name)
  • 取得請求頭:request.getHeader(String name )
  • 取得請求方法:request.getMethod()
  • 取得請求URI:request.getRequestURI()
  • 取得請求URL:request.getRequestURL()
  • 取得請求協定:request.getProtocol()
  • ##取得請求字元編碼:request.getCharacterEncoding()
  • 取得請求內容類型:request.getContentType()
  • 取得請求內容長度:request.getContentLength()
  • ##取得請求的ServletPath:request.getServletPath()
  • 取得請求的ContextPath:request.getContextPath()
  • #取得請求的Session:request.getSession()
  • 取得請求的Cookies:request.getCookies()
  • 取得請求的Locale:request.getLocale()
#2. response

response物件表示伺服器對客戶端的回應,包含所有要傳送給客戶端的數據,如回應頭、回應體等。它是一個javax.servlet.http.HttpServletResponse物件。

作用:

設定回應狀態碼:response.setStatus(int sc)
  • 設定回應頭:response.setHeader(String name, String value)
  • 設定回應字元編碼:response.setCharacterEncoding(String charset)
  • 設定回應內容類型:response.setContentType(String contentType)
  • #取得回應輸出類型:response.setContentType(String contentType)
  • #取得回應輸出流:response.getOutputStream()
  • 取得回應PrintWriter:response.getWriter()

3. session

#session物件表示客戶端與伺服器之間的一次會話,它包含了客戶端與伺服器互動過程中產生的所有信息,如使用者登入資訊、購物車資訊等。它是一個javax.servlet.http.HttpSession物件。

作用:

  • 取得SessionID:session.getId()
  • 取得Session的建立時間:session.getCreationTime()
  • 取得Session的最後存取時間:session.getLastAccessedTime()
  • 取得Session的最大不活動時間:session.getMaxInactiveInterval()
  • 設定Session的最大不活動時間:session .setMaxInactiveInterval(int interval)
  • 取得Session中的屬性:session.getAttribute(String name)
  • 設定Session中的屬性:session.setAttribute(String name, Object value)
  • 移除Session中的屬性:session.removeAttribute(String name)
  • #無效化Session:session.invalidate()

4.application

application物件表示整個Web應用程序,它包含了所有ServletContext物件。它是一個javax.servlet.ServletContext物件。

作用:

  • 取得ServletContext:application.getServletContext()
  • 取得Web應用程式的名稱:application.getServletContext(). getServletContextName()
  • 取得Web應用程式的版本:application.getServletContext().getMajorVersion() "." application.getServletContext().getMinorVersion()
  • WebWeb應用程式的ContextPath: application.getServletContext().getContextPath()
  • 取得Web應用程式的Servlet:application.getServletContext().getServlets()
  • #取得Web應用程式的Filter:application.getServletContext(). getFilters()
  • 取得Web應用程式的Listener:application.getServletContext().getListeners()
  • 取得網路應用程式的資源:application.getServletContext().getResource(String path)
  • 取得Web應用程式的資源路徑:application.getServletContext().getResourcePaths(String path)
  • 取得Web應用程式的MimeType:application.getServletContext().getMimeType(String file)

5. out

out物件表示JSP頁面的輸出流,它是一個java.io.PrintWriter物件。

作用:

  • 向JSP頁面輸出內容:out.print(Object obj)
  • 向JSP頁面輸出換行符號:out .println()

6. page

page物件表示目前的JSP頁面,它是一個javax.servlet.jsp.PageContext物件。

作用:

  • 取得目前JSP頁面的ServletContext:page.getServletContext()
  • 取得目前JSP頁面的Request:page.getRequest()
  • 取得目前JSP頁面的Response:page .getResponse()
  • 取得目前JSP頁面的Session:page.getSession()
  • 取得目前JSP頁面的Application:page.getServletContext().getServletContext()
  • 取得目前JSP頁面的JspWriter:page.getOut()
  • 取得目前JSP頁面的ServletConfig:page.getServletConfig()
  • 取得目前JSP頁面的Config:page.getConfig()
  • 取得目前JSP頁面的ErrorPage:page.getErrorPage()
  • 取得目前JSP頁面的Exception:page.getException()
  • 取得目前JSP頁面的IsErrorPage:page.isErrorPage ()
  • 取得目前JSP頁面的IsPage:page.isPage()
  • 取得目前JSP頁面的IsELIgnored:page.isELIgnored()
  • #取得目前JSP頁面的IsScriptingIgnored :page.isScriptingIgnored()

7. config

config物件表示目前JSP頁面的設定訊息,它是一個javax.servlet.ServletConfig物件。

作用:

  • 取得目前JSP頁面的ServletName:config.getServletName()
  • 取得目前JSP頁面的ContextPath:config. getServletContext().getContextPath()
  • 獲取當前JSP頁面的InitParameter:config.getInitParameter(String name)
  • 獲取當前JSP頁面的InitParameterNames:config.getInitParameterNames()

8. pageContext

pageContext物件表示目前JSP頁面的上下文,它是一個javax.servlet.jsp.PageContext物件。

作用:

    ## 取得目前JSP頁面的ServletContext:pageContext.getServletContext()
  • 取得目前JSP頁面的Request:pageContext. getRequest()
  • 取得目前JSP頁面的Response:pageContext.getResponse()
  • 取得目前JSP頁面的Session:pageContext.getSession()
  • 取得目前JSP頁面的Application:pageContext.getServletContext().getServletContext()
  • 取得目前JSP頁面的JspWriter:pageContext.getOut()
  • #取得目前JSP頁面的ServletConfig:pageContext.getServlet
  • #。
  • 取得目前JSP頁面的Config:pageContext.getConfig()
  • 取得目前JSP頁面的ErrorPage:pageContext.getErrorPage()
  • #取得目前JSP頁面的Exception:pageContext.getException( )
  • 取得目前JSP頁面的IsErrorPage:pageContext.isErrorPage()
  • 取得目前JSP頁面的IsPage:pageContext.isPage()
  • 取得目前JSP頁面的IsELIgnored: pageContext.isELIgnored()
  • 取得目前JSP頁面的IsScriptingIgnored:pageContext.isScriptingIgnored()

9. exception

#exception物件表示目前JSP頁面發生的異常,它是一個java.lang.Throwable物件。

作用:

  • 取得目前JSP頁面發生的例外:exception.getMessage()
  • 取得目前JSP頁面發生的例外的堆疊追蹤:exception.printStackTrace()

以上是介紹並解釋JSP中的九個內建物件的作用的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn