JSP four hidden objects and domain objects

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/qq_42267300/article/details/87636053

JSP hidden objects

JSP has nine hidden objects, which are commonly used to hide objects are: request, response, session, application.

out output stream object

Javax.servlet.jsp.JspWriter hidden object is an instance of the class out. Character class content server to the client output can be output through the out object. API out of the following objects:

Method name Outline
void append(CharSequence cs) Extended character class out to the output buffer. When the cache is full or execution out.flush () operation, the content will be output to the client browser
void clear() To empty the contents of output
void flush() The contents of the cache flush to the client browser
void println() Output content to the client
boolean isAutoFlush() Whether to automatically flush when returning buffer is full. If false, it will throw an exception when the cache is full
int getBufferSize() Returns the cache size. Unit KB
int getRemaining() Back buffer remaining size, in KB

demo2.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>out输出流对象</title>
</head>
<body>
    <h1>out输出流对象</h1>
    <form action="/xjm/demo2.jsp" method="get">
        <input type="submit" value="clear" name="clear"/>
    </form>

    <%
        out.println("out对象打印:123<br/>");
        out.println("out缓存大小:" + out.getBufferSize() + "kb<br/>");
        out.append("append对象:123asd<br/>");
        out.println("out缓存占用大小:" + out.getRemaining() + "kb<br/>");
        out.println("out是否自动flush:" + out.isAutoFlush() + "<br/>");
        if(request.getParameter("clear") != null) {
            out.clear();
            out.println("clear over");
        }
    %>
</body>
</html>

Operating results
Here Insert Picture Description
after clicking clear
Here Insert Picture Description

request

Javax.servlet.ServletRequest hidden object is an instance of the class out, on behalf of the requesting client. request comprising information of the client and the information request. Each time a client requests a request will become instance. Specific usage similar to the Servlet in the request. API request object as follows:

Method name Outline
void setAttribute(String name, Object value) Save an object in the request domain. Page after the present page or z forwarding (Forward) may obtain the object by getAttribute (String name) Method
Object getAttribute(String name) Return property
String getMethod() Mode return request, usually GET or POST
String getParameter(String key) Return parameters submitted
String[] getParameterValues(String key) Back to submit parameter values ​​of the same name. Returns an array
Enumeration getParameterNames() Returns all parameter names submitted
Cookie[] getCookie() Back to all Cookie
String getContextPath() Returns the application path
String getRequestURI() Returns the requested URI path
void setCharacterEncoding(String encoding) Set the encoding of the request
String getHeader(String name) Get request headers
Enumeration getHeaderNames() Return all request header name
Dispacher getRequestDispacher() Return Dispatcher object. Dispatcher object can perform forwarding (Forward) Operation
HttpSession getSession() Returns the built-in object session

response

Javax.servlet.ServletResponse hidden object is an instance of the class out, representing the response to the client. Any output server should be sent to the client browser through the object. Each time the server will think it should be a response object. Specific usage similar to the Servlet in response. API response object is as follows:

Method name Outline
void clear() Clear temporarily stored in the output buffer
void addCookie(Cookie cookie) Cookie settings
OutputStream getOutputStream() The server returns the output stream, the information stream may be a binary output
void sendRedirect(String url) 使页面重定向(redirect)到另一个页面
void setContentType(String contentType) 设置文档类型
PrintWriter getOut() 返回内置对象out
void setHeader(String name, String value) 设置response头信息
void setStatus(int status) 设置response状态码

config

隐藏对象out是javax.servlet.ServletConfig类的实例。ServletConfig封装了配置在web.xml中初始化的JSP参数。JSP通过config获取这些参数,一个JSP文件共用一个config对象。config对象的API如下:

方法名 概述
String getInitParameter(String name) 返回配置在web.xml中的初始化参数
Enumeration getInitParameterNames() 返回所有的初始化参数名称
ServletContext getServletContext() 返回application内置对象
String getServletName() 返回Servlet的名称

session

隐藏对象out是javax.servlet.http.HttpSession类的实例。session与cookie是记录客户访问的两种机制,session用于在服务器端保存用户信息,cookie用于在客户端保存用户信息。在Servlet中需要通过request.getSession()方法来获取session对象,而JSP中可以直接使用。如果<%@ page session=“false” %>,则隐藏对象session不可用。每个用户对应一个session对象。session对象的API如下:

方法名 概述
String getId() 返回session的id
Object getAttribute(String name) 返回属性
Enumeration getAttributeNames() 返回session中所有属性名
long getCreateTime() 返回该sessoin创建的时间
long getLastAccessedTime() 返回该session最后一次访问时间
int getMaxInactiveInterval() 返回seesoin的最大允许的间隔时间。单位为秒
void setAttribute(String name, Object value) 设置session
void setMaxInactiveInterval(long second) 设置最大允许时间间隔

application

隐藏对象out是javax.servlet.ServletContext类的实例。application代表了Servlet的上下文。application封装了JSP所在Web程序的信息。例如web.xml中配置的全局的初始化信息。Servlet中application对象需要通过ServletConfig.getServletContext()获取。整个Web应用对应一个application对象。application对象的API如下:

方法名 概述
Object getAttribute(String name) 返回属性
Enumeration getAttributeNames() 返回application中所有属性名
void setAttribute(String name, Object value) 设置application属性
void removeAttribute(String name) 移除application属性
String getInitParameter(String name) 返回全局初始化参数
Enumeration getInitParameterNames() 返回所有的全局初始化参数名称
String getMimeType(String filename) 返回文件的文档类型
String getRealPath(String relativePath) 返回相对网址的绝对路径

page

隐藏对象out是javax.servlet.jsp.HttpJspPage类的实例。page对象代替了当前JSP页面,是当前JSP编译后的Servlet类的对象,类同于Java类中的this关键字。代表了JSP编译后的java文件。


pageContext

隐藏对象out是javax.servlet.jsp.PageContext类的实例。与application类似,pageContext代表JSP当前页面的上下文,也就是JSP界面编译后的内容。通过该对象可以获取JSP中的资源。可以通过xxxAttribute方法来操作当前页面的域对象,还可以通过xxxAttribute(…,int scope)方法来操作其他三个域对象,pageContext对象的API如下:

方法名 概述
Object findAttribute(String name) 依次从pageContext,request,session,applicaton中查找指定的属性,若找到则立即返回且结束该次查找,若找不到返回null
Object getAttribute(String name) 返回属性
Object getAttribute(String name, int scope) 返回指定范围内的属性。范围:PAGE_SCOPE、REQUEST_SCOPE、SESSION_SCOPE、APPLICATION_SCOPE
Object setAttribute(String name, Object value) 设置属性
Object setAttribute(String name, Object value, int scope) 在指定范围内的属性
JspWriter getOut() 返回内置对象out
Object getPage() 返回内置对象page
ServletRequest getRequest() Returns the built-in object request
ServletResponse getResponse() Returns the built-in object response
ServletSession getSession() Returns the built-in object session

scope int type values ​​are static variables:

  • PAGE_SCOPE:1
  • REQUEST_SCOPE:2
  • SESSION_SCOPE:3
  • APPLICATION_SCOPE: 4
    when used, can be used directly int the corresponding values can be obtained by pageContext built-in objects.
    eg pageContext.PAGE_SCOPE

exception

Javax.lang.Exception hidden object is an instance of the class out. It encapsulates the exception information JSP thrown.
To use the exception object, first set <% @ page isErrorPage = "true "%>, exception processing error interface is typically used.


JSP four domain object

JSP 4 domain objects, in accordance with the descending scope are: application, session, request, pageContext.

application in the current project can be used. session in a session may be used, there may be multiple requests a reply. Request may be used in the next request. pageContext only valid in the current page.

Guess you like

Origin blog.csdn.net/qq_42267300/article/details/87636053