写点什么

JavaWeb 快速入门 --JSP(2)

  • 2022 年 4 月 25 日
  • 本文字数:2008 字

    阅读完需:约 7 分钟

这行内置对象大致可以四类:输入输出对象(request、response、out)、作用域通信对象(session、application、pageContext)、Servlet 对象(page、config)和异常对象(exception)

[](()输入输出对象

request 对象


request 对象封装了客户端的请求信息,包括 HTTP 头信息、系统信息、请求方式和请求参数等。通过调用相关方法可以获取封装信息。其中常用方法包括:


| 方法 | 说明 |


| --- | --- |


| String getParameter(String name) | 获取 JSP 页面中传递过来的参数 |


| Enumeration getParameterNames() | 返回一个枚举类型,包含当前页面所有参数名称的集合 |


| String[] getParameterValues(String name) | 返回包含参数 name 的所有值的数组 |


| void setAttribtue(String name,Object object) | 将方法保存到 request 范围内的变量中 |


| Object getAttribute(String name) | 获取 request 范围内名字为 name 的变量,若没有则为空 |


| void removeAttribute(String name) | 删除名字为 name 的 request 请求 |


| void setCharacterEncoding(String enc) | 指定请求编码的格式,一般用于解决中文乱码 |


response 对象


当客户端向 Web 服务器发送请求后,服务器会接收请求,并作出响应响应。response 代表的就是对客户端的响应,主要是将 JSP 容器处理过的对象传回到客户端。其中常用方法包括:


| 方法 | 说明 |


| --- | --- |


| void setContentType(String type) | 设置 MIME(响应的内容)类型 |


| void sendRedirect() | 重定向客户的请求到指定的页面 |


| void setHeader(String s1,String s2) | 设置 HTTP 首部信息,如:s1=“REfresh”,s2="1"时页面每一秒刷新一次 |


out 对象


out 对象可以在客户端浏览器输出信息,并且管理应用服务器上的输出缓冲区。在使用 out 对象输出数据时,可以对数据缓冲区进行操作,及时清除缓冲区中的残余数据,为其他的输出让出缓冲空间。待数据输出完毕后,要及时关闭输出流。其中的常用方法有:


| 方法 | 说明 |


| --- | --- |


| void print(DateType) | 在 JSP 页面输入数据,但不结束当前行 |


| void println(DateType | 在 JSP 页面输入数据,并结束当前行 |


| void clear() | 清空缓冲区中数据,但不把数据写入客户端 |


| void close() | 关闭输出流 |

[](()作用域通信对象

session 对象


从客户打开服务器开始,到关闭浏览器离开结束,这个过程被称为一次会话(session),session 对象在 JSP 页面打开时自动创建,当我们在不同页面之间进行跳转时,我们就需要 session 对象来辨别用户了。session 对象内部使用 Map 类(Key/value)来保存数据,通过 session 对象,我们可以对客户的会话进行控制,其常用方法如下:


| 方法 | 说明 |


| --- | --- |


| String getId() | 返回用户的 session 的 id |


| void setAttribute(String name,Object obj) | 设置名称为 name 的属性值,并存入到 session 中 |


| Object getAttribute(String name) | 返回 session 中属性名为 name 的属性值 |


| void removeAttribute(String name) | 除去 session 中属性名为 name 的属性 |


| Boolean isNew() | 判断是否为一个新用户 |


application 对象


application 对象保存所有应用程序中的公共数据到服务器中,只要服务器不关闭,application 就一直存在,所有用户都可以共享 application 对象。其中的常用方法有:


| 方法 | 说明 |


| --- | --- |


| void setAttribute(String name,Object obj) | 设置名称为 name 的属性值,并存入到 application 中 |


| Object getAttribute(String name) | 返回 application 中属性名为 name 的属性值 |


| void removeAttribute(String name) | 除去 application 中属性名为 name 的属性 |


pageContext 对象


pageContext 表示当前页面的所有属性和对象,它的创建和初始化都是由容器来完成的,我们可以在 JSP 页面中可以直接使用 pageContext 对象。通过它可以获取 JSP 页面的 out、request、reponse、session、application 等对象。其常用方法如下:


| 方法 | 说明 |


| --- | 《一线大厂 Java 面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》无偿开源 威信搜索公众号【编程进阶路】 --- |


| void forward(String url) | 将当前页面重定向到另一个页面或 servlet 对象 |


| Object getAttribute(String name) | 获取页面范围或特定对象范围内的属性 name |


| void setAttribute(String name,Object obj) | 设置默认页面范围或特定对象范围的属性 name |


| void removeAttribute(String name,Object obj) | 设置默认页面范围或特定对象范围的属性 name |


| ServletRequest getRequest() | 返回当前页面中的 request 对象 |


| ServletResponse getResponse() | 返回当前页面中的 response 对象 |


| ServletSession getSession() | 返回当前页面中的 session 对象 |


| ServletException getException() | 返回当前页面中的 exception 对象 |


| ServletOut getOut() | 返回当前页面中的 out 对象 |


| ServletApplication getApplication() | 返回当前页面中的 application 对象 |

[](()Servlet 对象

page 对象

用户头像

还未添加个人签名 2022.04.13 加入

还未添加个人简介

评论

发布
暂无评论
JavaWeb快速入门--JSP(2)_Java_爱好编程进阶_InfoQ写作社区