当前位置:网站首页>request对象、response对象、session对象
request对象、response对象、session对象
2022-06-27 21:36:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
仅仅是一些简单知识点的罗列,当做笔记,想具体学习的话,还是要找专业的书籍看看
request对象
作用:获取用户提供的信息
语法:是实现了ServletRequest接口类的一个实例
使用表单向服务器的某个JSP页面提交信息
常用方法:
设置编码方式:例如(用来防止汉字乱码)
request.setCharacterEncoding(“gb2312”);
同效果:String str = request.getParameter(“message”);
byte[] b = str.getBytes(“ISO-8859-1”);
Str = new String(b);
getProtocol() 获取提交信息使用的协议 getServletPath() 获取请求的JSP页面文件的目录 getContextPath() 获取请求的当前的web服务目录 getContentLength() 获取提交信息的长度 getMethod() 获取提交信息的方法,例如:post、get getHeader(s : String) 获取http头文件中由参数s指定的头名字的值。s可以为:accept,accept-language,content-type, accept-encoding,user-agent,host,content-length,connection,cookie getHeaderNames() 获取头名字的枚举类型 getHeaders(s : String) 获取头文件中指定头名字的全部值的一个枚举 getRemoteAdr() 获取用户的IP地址 getRemoteHost() 获取用户的用户机名 getServletPort() 获取服务器端号 GetParameterNames() 获取提交信息中name参数值的一个枚举
response对象
作用:对用户的请求作出动态响应,向用户端发送数据
例如 :page指令为contentType指定一个值来决定响应MIME类型。若想要动态的改变这个值可以用:
response.setContentType(s : String); s的取值可以是:text/jtml; text/plain; image/gif; image/x-xbitmap; image/jpeg; image/pjpeg; application/x-shockwave-flash;application/vnd.ms-powerpoint; application/vnd-ms-execel; application/msword等
请求和响应都包含一些方法头
使用:response.addHeader(head : String, value : String);
或response.setHeader(head : String, value : String);
动态的添加新的响应和头的值
例如:response.setHeader(“Reflesh”, “5”);表示每5秒刷新一次页面
response重定向
语法:response.setRedirect(url : URL);
response状态行
语法:response.setStatus(n : int);
session对象
说明:由Tomcat负责创建对象,用于存储用户访问服务器页面期间提交的各种信息。每个对象都会被分配一个String类型的ID号(可以使用session.getId();获取),存放在Cookie中,同一个用户在不同的文本服务目录中session互不相同。
实现session对象的唯一性:一般的用户端支持了Cookie,就已经实现了对象的唯一性。
如果没有支持,那就可以通过重写URL来实现session对象的唯一性。调用encodeURL(id : String)或encodeRedirectURL(id : String)方法,向新的URL中添加参数,将session对象的id传递过去。
setAttribute(key : String, obj : Object) : void 存储对象,添加索引
getAttribute(key : String) : Object 使用索引,获取对象
getAttributeNmaes() : Enumeration 产生一个枚举对象,可以使用nextElems() 遍历session 中的各个对象对应的关键字
removeAttribute(key : String) : void 移除关键字对应的对象
getCreationTime() : long 获取session创建的时间(毫秒)
getLastAccessedTime() : long session最后一次被操作的时间(毫秒)
getMaxInactiveInterval() : int 获取最长发呆时间(秒)
setMaxInactiveInterval(interval : int) 设置最长的发呆时间(秒)
isNew() : boolean 判断session是否是最新创建的
invalidate() : void 使session无效
session的生存周期:
1.调用invalidate()方法,使session无效
2.达到设置的最长发呆时间
3.关闭服务器
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/133040.html原文链接:https://javaforall.cn
边栏推荐
- Storage structure of graph
- 虽然TCGA数据库有33种癌症
- Chapter 2 integrated mp
- 【PCL自学:PCLVisualizer】点云可视化工具PCLVisualizer
- 線程池實現:信號量也可以理解成小等待隊列
- Systematic learning + active exploration is the most comfortable way to get started!
- 往前一步是优秀,退后一步是懵懂
- 零基础自学SQL课程 | CASE函数
- Golang uses Mongo driver operation -- Query (array related)
- 线程池实现:信号量也可以理解成小等待队列
猜你喜欢
[sword finger offer] 47 Maximum value of gifts
halcon之区域:多种区域(Region)特征(6)
Using two stacks to implement queues [two first in first out is first in first out]
Eliminate gaps around El image images
An analysis of C language functions
c语言字符指针、字符串初始化问题
[PCL self study: segmentation4] point cloud segmentation based on Min cut
零基础自学SQL课程 | SQL基本函数大全
The file or assembly 'cefsharp.core.runtime.dll' or one of its dependencies could not be loaded. Is not a valid Win32 Application. (exception from hresult:0x800700c1)
【tinyriscv verilator】分支移植到正点原子达芬奇开发板
随机推荐
【AI应用】NVIDIA Tesla V100S-PCIE-32GB的详情参数
吴恩达《机器学习》课程总结(14)_降维
[sword finger offer] 47 Maximum value of gifts
零基础自学SQL课程 | SQL中的日期函数大全
100 questions for an enterprise architect interview
Zero foundation self-study SQL course | complete collection of SQL basic functions
零基础自学SQL课程 | SQL基本函数大全
After a period of silence, I came out again~
Applet referer
ASP. Net warehouse purchase, sales and inventory ERP management system source code ERP applet source code
Zero foundation self-study SQL course | complete collection of date functions in SQL
本地可视化工具连接阿里云centOS服务器的redis
[try to hack] kill evaluation
Excel print settings public header
撰写外文时怎样引用中文文献?
【AI应用】Jetson Xavier NX的详情参数
[AI application] detailed parameters of NVIDIA geforce RTX 3060
获取基因有效长度的N种方法
Sécurité, économie de carburant et protection de l'environnement chameau
Chenyun pytorch learning notes_ Build RESNET with 50 lines of code