zoukankan      html  css  js  c++  java
  • JSP内置对象

    Jsp内置对象

    jsp具有9大内置对象:

       
    内置对象 类型 作用域
    request javax.servlet.http.HttpServletRequest request
    response javax.servlet.http.HttpServletResponse response
    pageContext javax.servlet.jsp.PageContext page
    session javax.servlet.http.HttpSession session
    application javax.servlet.jsp.ServletContext application
    out javax.servlet.jsp.jspWriter page
    config javax.servlet.ServletConfig page
    page java.lang.Object page
    exception java.lang.Throwable page

     

    jsp有4种属性的保存范围:page,request,session,application

    保存范围介绍
    page 所设置的属性在当前页面有效
    request 指属性在一次请求范围内有效
    session 指客户端与服务器一次会话范围内
    application 指整个服务器范围,直至服务器停止以后才失效

     

    对于属性操作的方法:

    方法作用
    setAttribute("name","key") 在某个范围内设置属性
    getAttribute("name") 获取某个范围内属性名为name的属性值
    removeAttribute("name") 移除某个属性范围内名为name的属性

     

    1.request对象:

    request对象不但可以用来设置和取得request范围变量,还可以用来获得客户端请求参数,请求的来源,表头,cookies等

    request获取请求参数的方法:

    方法返回值方法说明
    getParameter(String name) String 获得参数名为name的参数值
    getParamterName() Enumeration 获得所有参数的名称
    getParameterValues(String name) String[] 获得参数名为name的所有参数
    getParameterMap() Map 获得所有参数封装的Map实例

    若出现乱码:则设置request.setCharacterEncoding("gb2312");

    request获得表头及其他信息的方法

    方法返回值方法说明
    getHeader(String name) String 获得指定标题名称为name的标头
    getHeaderName() Enumeration 获得所有标头的名称
    getIntHeader(String name) int 获得标题名称为name的标头,内容以整数类型返回
    getDateHeader(String name) long 获得标题名称为name的标头,内容以日期类型返回
    getCookies() Cookie 获得相关cookies
    getContextPath() String 获得Context的路径
    getMethod() String 获得客户端的提交方式
    getProtocol() String 获得使用的HTTP协议
    getQueryString() String 获得请求的字符串
    getRequestedSessionId() String 获得客户端的SessionID
    getRequestURI String 获得请求URL
    getRemoteAddr() String 获得客户端IP地址
    getRemoteHost() String 获得客户机的主机名称
    getRmotePort() int 获得客户机的主机端口号
    getRemoteUer() String 获得客户机名称

    2.response

    response用来给客户端传送信息

    方法返回值方法说明
    addCookie(Cookie cookie) void 添加cookie
    addDateHeader(String name,long date) void 添加一个标题名称为name标头,其值为日期类型
    addHeader(String name,String value) void 添加一个标题名称为name标头,其值为字符串类型
    addIntHeader(String name,int value) void 添加一个标题名称为name标头,其值为整数类型
    setDateHeader(String name,long date) void 设置一个标题名称为name标头,其值为日期类型
    setHeader(String name,String value) void 设置一个标题名称为name标头,其值为字符串类型
    setIntHeader(String name,int value) void 设置一个标题名称为name标头,其值为整数类型
    sendError(int sc) void 传送状态码
    sendError(int sc,String msg) void 传送状态码和错误信息
    setStatus(int sc) void 传送状态码
    sendRedirect(URL) void 页面重定向,用来实现页面跳转

    给addIntHeader()方法添加一个标题为Refresh的标头,并指定刷新时间,从而实现页面刷新。例如:response.addIntHeader("Refresh",10);

    response.addHeader("Refresh","10;URL=http://www.baidu.com");实现页面自动跳转

    3.out对象

    out对象用来向网页输出信息

    方法返回值方法说明
    clear() void 清除网页上的输出内容
    clearBuffer() void 清除缓冲区内容
    close() void 关闭缓冲区,清除所有内容
    getBufferSize() int 取得缓冲区大小
    getRemaining() int 取得缓冲区剩余大小
    inAutoFlush() boolean 获得缓冲区是否进行自动清除的信息
    print(String str) void 进行页面输出
    println(String str) void 进行页面输出并换行

    4.session对象

    session对象用来表示用户的会话状况,一般用于保存用户的各种信息,直至生命周期超时或者被认为释放掉为止

    方法返回值方法说明
    getId() String 获得session的ID
    getCreationTime() long 获得session生成的时间
    getLashAccessedTime() long 获得用户最后通过session发送请求时间
    getMaxInactiveInterval long 取得session生命周期,如果超过这个之间则失效
    invalidate() void 清空session内容
    isNew boolean 判断session是否为“新”的
    setMaxInactiveInterval() void 设置session的生命周期,超过这个时间则失效

    5.application

    application对象用来取得和设置Servlet的相关信息application对象的生命周期是从服务器产生到服务器关闭位置

    方法返回值方法说明
    getMajorVersion() int 获得主要的Servlet API版本
    getMinorVersion int 获得次要的Servlet版本
    getServerInfo String 获得服务器版本
    getMimeType() String 获得指定文件的MIME类型
    getContext() ServletContext 获得指定Local的Application context
    getRealPath String 获得指定path的绝对路径

    6.pageContext

    pageContext对象不但可以用来设置page范围的属性,同样也可以设置其他属性范围的属性,不过要指定范围的参数

    方法返回值方法说明
    getException() Ecxeption 获得当前的exception内置对象
    getOut() JspWriter 获得当前的out内置对象
    getPage() Object 获得当前的page内置对象
    getRequest() ServletRequest 获得当前的request内置对象
    getResponse() ServletResponse 获得当前的response内置对象
    getServletConfig() ServletConfig 获得当前的config内置对象
    getServletContext() ServletContext 获得当前application内置对象
    getSession() HttpSession 获得当前的session内置对象
    getAttribute(String name,int scope) Object 获得指定范围的name属性
    getAttributeNamesInScope(int scope) Enumeration 获得指定范围的所有属性名称
    getAttributesScope(String name) int 获得属性名称为name的属性范围
    removeAttribute(String name) void 移除属性名为name的属性
    removeAttribute(String name,int scope) void 移除指定范围的属性名称为name的属性
    setAttribute(String name,Object value,int scope) void 设置指定范围的name属性
    findAttribute(String name) Object 寻找所有范围属性名称为name的属性

    7.page

    page对象代表JSP转译后的Servlet,通过page对象可以非常方便的调用Servlet类中定义的方法

    8.Config

    config对象可以用来获得Servlet的配置信息

    方法返回值方法说明
    getInitParameter(name) String 获得Servlet初始化参数
    getInitParameterNames() Enumeration 获得Servlet所有初始化参数名称
    getServletContext() ServletContext 获得当前Application context
    getServletName() String 获得Servlet名称

    9.exception

    exception对象用来处理错误异常,如果要使用exception对象,必须指定page中的isErrorPage属性值为“True".

  • 相关阅读:
    解决Chrome关联HTML文件,图标不显示的问题。
    Tomcat启动超时问题Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds
    matlalb 的后台运行方式
    新转移注意(caffe):ImportError: libcudart.so.7.0: cannot open shared object file: No such file or directory
    查看nvidia显卡命令
    train validation test
    lmdb数据格式
    github
    2016 hosts
    opencv3.0 imread问题
  • 原文地址:https://www.cnblogs.com/xr210/p/10722459.html
Copyright © 2011-2022 走看看