`

JSP中的request对象

    博客分类:
  • jsp
阅读更多

1.request对象

客户端的请求信息被封装在request对象中,通过它才能了解到客户的需求,然后做出响应。它是HttpServletRequest类的实例。

序号
1 object getAttribute(String name)
返回指定属性的属性值

2 Enumeration getAttributeNames()
返回所有可用属性名的枚举

3 String getCharacterEncoding()
返回字符编码方式

4 int getContentLength()
返回请求体的长度(以字节数)

5 String getContentType()
得到请求体的MIME类型

6 ServletInputStream getInputStream()
得到请求体中一行的二进制流

7 String getParameter(String name)
返回name指定参数的参数值

8 Enumeration getParameterNames()
返回可用参数名的枚举

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

10 String getProtocol()
返回请求用的协议类型及版本号

11 String getScheme()
返回请求用的计划名,:http.httpsftp

12 String getServerName()
返回接受请求的服务器主机名

13 int getServerPort()
返回服务器接受此请求所用的端口号

14 BufferedReader getReader()
返回解码过了的请求体

15 String getRemoteAddr()
返回发送此请求的客户端IP地址

16 String getRemoteHost()
返回发送此请求的客户端主机名

17 void setAttribute(String key,Object obj)
设置属性的属性值

18 String getRealPath(String path)
返回一虚拟路径的真实路径

:

<%@ page contentType="text/html;charset=gb2312"%>
<%request.setCharacterEncoding("gb2312");%>
<html>
<head>
<title>request
对象_
1</title>
</head>
<body bgcolor="#FFFFF0">
<form action="" method="post">
<input type="text" name="qwe">
<input type="submit" value="
提交
">
</form>
请求方式:
<%=request.getMethod()%><br>
请求的资源:
<%=request.getRequestURI()%><br>
请求用的协议:
<%=request.getProtocol()%><br>
请求的文件名:
<%=request.getServletPath()%><br>
请求的服务器的IP
<%=request.getServerName()%><br>
请求服务器的端口:
<%=request.getServerPort()%><br>
客户端IP地址:
<%=request.getRemoteAddr()%><br>
客户端主机名:
<%=request.getRemoteHost()%><br>
表单提交来的值:
<%=request.getParameter("qwe")%><br>
</body>
</html>
<%@ page contentType="text/html;charset=gb2312"%>
<%request.setCharacterEncoding("gb2312");%>
<%@ page import="java.util.Enumeration"%>
<html>
<head>
<title>request
对象_
2</title>
</head>
<body bgcolor="#FFFFF0">
<form action="" method="post">
用户名:
<input type="text" name="username">&nbsp;&nbsp;
码:
<input type="text" name="userpass">&nbsp;&nbsp;
<input type="submit" value="
进入
" >
</form>
<%
String str="";
if(request.getParameter("username")!=null && request.getParameter("userpass")!=null){
Enumeration enumt = request.getParameterNames();
while(enumt.hasMoreElements()){
str=enumt.nextElement().toString();
out.println(str+":"+request.getParameter(str)+"<br>");
}
}
%>
</body>
</html>
<%@ page contentType="text/html;charset=gb2312"%>
<%request.setCharacterEncoding("gb2312");%>
<html>
<head>
<title>request
对象_
3</title>
</head>
<body bgcolor="#FFFFF0">
<form action="" method="post">
擅长:
<input type="checkbox" name="cb" value="ON1">VC++&nbsp;
<input type="checkbox" name="cb" value="ON2">JAVA&nbsp;
<input type="checkbox" name="cb" value="ON3">DELPHI&nbsp;
<input type="checkbox" name="cb" value="ON4">VB&nbsp;
<br>
<input type="submit" value="
进入
" name="qwe">
</form>
<%
if(request.getParameter("qwe")!=null ){
for(int i=0;i<request.getParameterValues("cb").length;i++){
out.println("cb"+i+":"+request.getParameterValues("cb")[i]+"<br>");
}
out.println(request.getParameter("qwe"));
}
%>
</body>
</html>
 

 

2.response对象

response对象包含了响应客户请求的有关信息,但在JSP中很少直接用到它。它是HttpServletResponse类的实例。

序号
1 String getCharacterEncoding()
返回响应用的是何种字符编码

2 ServletOutputStream getOutputStream()
返回响应的一个二进制输出流

3 PrintWriter getWriter()
返回可以向客户端输出字符的一个对象

4 void setContentLength(int len)
设置响应头长度

5 void setContentType(String type)
设置响应的MIME类型

6 sendRedirect(java.lang.String location)
重新定向客户端的请求


 

3.session对象

session对象指的是客户端与服务器的一次会话,从客户连到服务器的一个WebApplication开始,直到客户端与服务器断开连接为止。它是HttpSession类的实例.

序号
1 long getCreationTime()
返回SESSION创建时间

2 public String getId()
返回SESSION创建时JSP引擎为它设的惟一ID

3 long getLastAccessedTime()
返回此SESSION里客户端最近一次请求时间

4 int getMaxInactiveInterval()
返回两次请求间隔多长时间此SESSION被取消
(ms)
5 String[] getValueNames()
返回一个包含此SESSION中所有可用属性的数组

6 void invalidate()
取消SESSION,使SESSION不可用

7 boolean isNew()
返回服务器创建的一个SESSION,客户端是否已经加入

8 void removeValue(String name)
删除SESSION中指定的属性

9 void setMaxInactiveInterval()
设置两次请求间隔多长时间此SESSION被取消
(ms)
例:

<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.util.*" %>
<html>
<head><title>session
对象_
1</title><head>
<body><br>
session
的创建时间
:<%=session.getCreationTime()%>&nbsp;&nbsp;<%=new Date(session.getCreationTime())%><br><br>
session
Id
:<%=session.getId()%><br><br>
客户端最近一次请求时间
:<%=session.getLastAccessedTime()%>&nbsp;&nbsp;<%=new java.sql. Time(session.getLastAccessedTime())%><br><br>
两次请求间隔多长时间此SESSION被取消
(ms):<%=session.getMaxInactiveInterval()%><br><br>
是否是新创建的一个SESSION:<%=session.isNew()?"":"
"%><br><br>
<%
session.putValue("name","
霖苑编程
");
session.putValue("nmber","147369");
%>
<%
for(int i=0;i<session.getValueNames().length;i++)
out.println(session.getValueNames()[i]+"="+session.getValue(session.getValueNames()[i]));
%>
<!--
返回的是从格林威治时间(GMT)1970010100000起到计算当时的毫秒数
-->
</body>
</html>
 

 

 

4.out对象

out对象是JspWriter类的实例,是向客户端输出内容常用的对象

序号
1 void clear()
清除缓冲区的内容

2 void clearBuffer()
清除缓冲区的当前内容

3 void flush()
清空流

4 int getBufferSize()
返回缓冲区以字节数的大小,如不设缓冲区则为
0
5 int getRemaining()
返回缓冲区还剩余多少可用

6 boolean isAutoFlush()
返回缓冲区满时,是自动清空还是抛出异常

7 void close()
关闭输出流

例:

<%@page contentType="text/html;charset=gb2312"%>
<html><head><title>out
对象_1:缓存测试</title></head>
<
%@page buffer="1kb"%>
<body>
<%
for(int i=0;i<2000;i++)
out.println(i+"{"+out.getRemaining()+"}");
%><br>
缓存大小:<%=out.getBufferSize()%><br>
剩余缓存大小:
<%=out.getRemaining()%><br>
自动刷新:
<%=out.isAutoFlush()%><br>
<%--out.clearBuffer();--%>
<%--out.clear();--%>
<!--
缺省情况下:服务端要输出到客户端的内容,不直接写到客户端,而是先写到一个输出缓冲区中.只有在下面三中情况下,才会把该缓冲区的内容输出到客户端上:

1.
JSP网页已完成信息的输出

2.
输出缓冲区已满

3.JSP
中调用了out.flush()
response.flushbuffer()
-->
</body>
</html>

5.page对象

page对象就是指向当前JSP页面本身,有点象类中的this指针,它是java.lang.Object类的实例

序号
1 class getClass
返回此Object的类

2 int hashCode()
返回此Objecthash

3 boolean equals(Object obj)
判断此Object是否与指定的Object对象相等

4 void copy(Object obj)
把此Object拷贝到指定的Object对象中

5 Object clone()
克隆此Object对象

6 String toString()
把此Object对象转换成String类的对象

7 void notify()
唤醒一个等待的线程

8 void notifyAll()
唤醒所有等待的线程

9 void wait(int timeout)
使一个线程处于等待直到timeout结束或被唤醒

10 void wait()
使一个线程处于等待直到被唤醒

11 void enterMonitor()
Object加锁

12 void exitMonitor()
Object开锁


 

6.application对象

application对象实现了用户间数据的共享,可存放全局变量。它开始于服务器的启动,直到服务器的关闭,在此期间,此对象将一直存在;这样在用户的前后连接或不同用户之间的连接中,可以对此对象的同一属性进行操作;在任何地方对此对象属性的操作,都将影响到其他用户对此的访问。服务器的启动和关闭决定了application对象的生命。它是ServletContext类的实例。

序号
1 Object getAttribute(String name)
返回给定名的属性值

2 Enumeration getAttributeNames()
返回所有可用属性名的枚举

3 void setAttribute(String name,Object obj)
设定属性的属性值

4 void removeAttribute(String name)
删除一属性及其属性值

5 String getServerInfo()
返回JSP(SERVLET)引擎名及版本号

6 String getRealPath(String path)
返回一虚拟路径的真实路径

7 ServletContext getContext(String uripath)
返回指定WebApplicationapplication对象

8 int getMajorVersion()
返回服务器支持的Servlet API的最大版本号

9 int getMinorVersion()
返回服务器支持的Servlet API的最大版本号

10 String getMimeType(String file)
返回指定文件的MIME类型

11 URL getResource(String path)
返回指定资源(文件及目录)URL路径

12 InputStream getResourceAsStream(String path)
返回指定资源的输入流

13 RequestDispatcher getRequestDispatcher(String uripath)
返回指定资源的RequestDispatcher对象

14 Servlet getServlet(String name)
返回指定名的
Servlet
15 Enumeration getServlets()
返回所有Servlet的枚举

16 Enumeration getServletNames()
返回所有Servlet名的枚举

17 void log(String msg)
把指定消息写入Servlet的日志文件

18 void log(Exception exception,String msg)
把指定异常的栈轨迹及错误消息写入Servlet的日志文件

19 void log(String msg,Throwable throwable)
把栈轨迹及给出的Throwable异常的说明信息 写入Servlet的日志文件

例:

<%@ page contentType="text/html;charset=gb2312"%>
<html>
<head><title>APPLICATION
对象_
1</title><head>
<body><br>
JSP(SERVLET)
引擎名及版本号
:<%=application.getServerInfo()%><br><br>
返回/application1.jsp虚拟路径的真实路径
:<%=application.getRealPath("/application1.jsp")%><br><br>
服务器支持的Servlet API的大版本号
:<%=application.getMajorVersion()%><br><br>
服务器支持的Servlet API的小版本号
:<%=application.getMinorVersion()%><br><br>
指定资源(文件及目录)URL路径:<%=application.getResource("/application1.jsp")%><br><br><!--可以将application1.jsp换成一个目录
-->
<br><br>
<%
application.setAttribute("name","
霖苑计算机编程技术培训学校
");
out.println(application.getAttribute("name"));
application.removeAttribute("name");
out.println(application.getAttribute("name"));
%>
</body>
</html>
<%@ page contentType="text/html;charset=gb2312"%>
<html>
<head><title>APPLICATION
对象_
2</title><head>
<body><br>
<!--
由于application一直存在于服务器端,可以利用此特性对网页记数
-->
<%
if(application.getAttribute("count")==null)
application.setAttribute("count","1");
else
application.setAttribute("count",Integer.toString(Integer.valueOf(application.getAttribute("count").toString()).intValue()+1));
%>
你是第<%=application.getAttribute("count")%>位访问者

</body>
<!--
由于getAttribute()方法得到的是一个Object类型对象,getString()方法转化为String类型-->
<!--
Integer类的valueOf()方法把得到的String转化成Integer的对象,在用intValue()方法得到int,再加1,最后把计算的结果用Integer.toString()方法转化成setAttribute()方法所要求的String类型
-->
</html>
<%@ page contentType="text/html;charset=gb2312"%>
<html>
<head><title>APPLICATION
对象_
3</title><head>
<body><br>
<!--
由于application一直存在于服务器端,可以利用此特性对网页记数
-->
<%
String str=application.getAttribute("count").toString();//getAttribute("count")
返回的是Object类型

int i=0;
if(str==null)
application.setAttribute("count","1");
else
i=Integer.parseInt(str); //out.println(i);
application.setAttribute("count",++i+"");
%>
你是第<%=application.getAttribute("count")%>位访问者
</body>
</html>
 

7.exception对象

exception对象是一个例外对象,当一个页面在运行过程中发生了例外,就产生这个对象。如果一个JSP页面要应用此对象,就必须把isErrorPage设为true,否则无法编译。他实际上是java.lang.Throwable的对象

序号
1 String getMessage()
返回描述异常的消息

2 String toString()
返回关于异常的简短描述消息

3 void printStackTrace()
显示异常及其栈轨迹

4 Throwable FillInStackTrace()
重写异常的执行栈轨迹

 

8.pageContext对象

pageContext对象提供了对JSP页面内所有的对象及名字空间的访问,也就是说他可以访问到本页所在的SESSION,也可以取本页面所在的application的某一属性值,他相当于页面中所有功能的集大成者,它的本 类名也叫pageContext

序号
1 JspWriter getOut()
返回当前客户端响应被使用的JspWriter
(out)
2 HttpSession getSession()
返回当前页中的HttpSession对象
(session)
3 Object getPage()
返回当前页的Object对象
(page)
4 ServletRequest getRequest()
返回当前页的ServletRequest对象
(request)
5 ServletResponse getResponse()
返回当前页的ServletResponse对象
(response)
6 Exception getException()
返回当前页的Exception对象
(exception)
7 ServletConfig getServletConfig()
返回当前页的ServletConfig对象
(config)
8 ServletContext getServletContext()
返回当前页的ServletContext对象
(application)
9 void setAttribute(String name,Object attribute)
设置属性及属性值

10 void setAttribute(String name,Object obj,int scope)
在指定范围内设置属性及属性值

11 public Object getAttribute(String name)
取属性的值

12 Object getAttribute(String name,int scope)
在指定范围内取属性的值

13 public Object findAttribute(String name)
寻找一属性,返回起属性值或
NULL
14 void removeAttribute(String name)
删除某属性

15 void removeAttribute(String name,int scope)
在指定范围删除某属性

16 int getAttributeScope(String name)
返回某属性的作用范围

17 Enumeration getAttributeNamesInScope(int scope)
返回指定范围内可用的属性名枚举

18 void release()
释放pageContext所占用的资源

19 void forward(String relativeUrlPath)
使当前页面重导到另一页面

20 void include(String relativeUrlPath)
在当前位置包含另一文件

例:

<%@page contentType="text/html;charset=gb2312"%>
<html><head><title>pageContext
对象_1</title></head>
<body><br>
<%
request.setAttribute("name","
霖苑编程
");
session.setAttribute("name","
霖苑计算机编程技术培训
");
//session.putValue("name","
计算机编程
");
application.setAttribute("name","
培训
");
%>
request
设定的值:
<%=pageContext.getRequest().getAttribute("name")%><br>
session
设定的值:
<%=pageContext.getSession().getAttribute("name")%><br>
application
设定的值:
<%=pageContext.getServletContext().getAttribute("name")%><br>
范围1内的值:
<%=pageContext.getAttribute("name",1)%><br>
范围2内的值:
<%=pageContext.getAttribute("name",2)%><br>
范围3内的值:
<%=pageContext.getAttribute("name",3)%><br>
范围4内的值:
<%=pageContext.getAttribute("name",4)%><br>
<!--
从最小的范围page开始,然后是requessession以及
application-->
<%pageContext.removeAttribute("name",3);%>
pageContext
修改后的session设定的值:
<%=session.getValue("name")%><br>
<%pageContext.setAttribute("name","
应用技术培训
",4);%>
pageContext
修改后的application设定的值:
<%=pageContext.getServletContext().getAttribute("name")%><br>
值的查找:
<%=pageContext.findAttribute("name")%><br>
属性name的范围:
<%=pageContext.getAttributesScope("name")%><br>
</body></html>
 

 

9.config对象

config对象是在一个Servlet初始化时,JSP引擎向它传递信息用的,此信息包括Servlet初始化时所要用到的参数(通过属性名和属性值构成)以及服务器的有关信息(通过传递一个ServletContext对象)

序号
1 ServletContext getServletContext()
返回含有服务器相关信息的ServletContext对象

2 String getInitParameter(String name)
返回初始化参数的值

3 Enumeration getInitParameterNames()
返回Servlet初始化所需所有参数的枚举

 

 

分享到:
评论

相关推荐

    JSP的Request对象练习源代码

    JSP的Request对象练习源代码,动态演示了request内置对象的各种功能!

    jsp request 对象详解

    jsp request 9大对象详解config pageContext application对象

    JSP 内置对象:request 对象.pptx

    隐式对象 request 对象

    实例详解JSP内置对象--request对象

    在用java做基于Web的开发时,不可避免的需要和request&response这两个对象打交道,因此,对它们的学习和掌握就显得至关重要。

    JSP的request对象实例详解

    客户端的请求信息被封装在request对象中,通过它才能了解客户的需求,然后做出响应。它是HttpServletRequest类的实例。request对象具有请求域,即完成客户端的请求之前,该对象一直有效。  二 request对象方法  ...

    request对象在JSP编程中的应用

    JSP单选题标准化考试页面,利用request对象提取考试页面中的答题信息进行试卷批改,并最终给出成绩

    JSP文本框 request,对象的使用。 文本域

    JSP入门,文本框 文本域 单选框 复选框示范。 可在TOMCAT上运行。

    Jsp九大内置对象,request,session,application

    熟练使用这些内置对象是开发JSP应用程序的基本要求,尤其是对于request、session和application对象更是必须要掌握的。 会使用对象所具有的方法,能熟练使用每一种对象的几种常用方法。希望对大家有用。。。

    4、JAVA培训之JSP基础request对象笔记[归纳].pdf

    4、JAVA培训之JSP基础request对象笔记[归纳].pdf

    JSP request&response;&out;.ppt

    什么是JSP的内置对象 JSP的内置对象有哪些? request对象 与 response对象 session对象 application对象 out对象

    4 JSP内建对象之request

    NULL 博文链接:https://zhiyongliu.iteye.com/blog/1682155

    详细了解JSP中九个隐含对象

    详细了解JSP中九个隐含对象,page 对象,config 对象,request 对象,response 对象,out 对象,session 对象,application对象,pageContext对象,exception对象

    Jsp程序设计试题库

    1、jsp的技术特点?...Request对象提供对Http请求数据的访问,同时还提供用于加入特定请求数据的上下文 Response对象允许直接访问HttpServletResponse对象 Session对象可能是状态管理上下文中使用最多的对话

    JSP内建对象方法大全

    JSP内建对象: page对象 config对象 out对象 request对象 response对象 seesion对象 application对象 pageContext对象 exception对象 的全部方法和具体的解释!

    jsp九大内置对象

    1、Request对象  该对象封装了用户提交的信息,通过调用该对象相应的方法可以获取封装的信息,即使用该对象可以  获取用户提交的信息。  当Request对象获取客户提交的汉字字符时,会出现乱码问题,必须进行...

    Jsp中request的3个基础实践

    本文包含request内置对象的使用、乱码处理的两种方法、使用request.getParamter()方法获取表单提交的数据、采用request对象通过getParameter()方法和getParameterValues()方法获取表单请求数据、使用request内置对象...

    JSP 中request与response的用法详解

    JSP 中request与response的用法详解 概要: 在学习这两个对象之前,我们应该已经有了http协议的基本了解了,如果不清楚http协议的可以看我的关于http协议的介绍。因为其实request和response的使用大部分都是对http...

Global site tag (gtag.js) - Google Analytics