JSP九大内置对象详解

1、Request对象

    客户端的请求信息被封装在Request对象中,经过它才能了解到客户的需求,而后作出响应。它是HttpServletRequest类的实例。html

  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.https及ftp等
  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): 返回一虚拟路径的真实路径

样例1

<%@ 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>

样例2

<%@ 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">  
   密 码:<input type="text" name="userpass">  
   <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>

样例3

<%@ 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++ 
       <input type="checkbox" name="cb" value="ON2">JAVA 
       <input type="checkbox" name="cb" value="ON3">DELPHI 
       <input type="checkbox" name="cb" value="ON4">VB 
       <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类的实例。java

  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类的实例。sql

  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()%>  <%=new Date(session.getCreationTime())%><br><br>
   session的Id号:<%=session.getId()%><br><br>
   客户端最近一次请求时间:<%=session.getLastAccessedTime()%>  <%=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)1970年01月01日0:00:00起到计算当时的毫秒数-->
</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():返回此Object的hash码
  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):返回指定WebApplication的application对象
  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的日志文件

样例1

<%@ 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>

样例2

<%@ 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>

样例3

<%@ 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的某一属性值,他至关于页面中全部功能的集大成者,它的本 类名也叫PageContextsession

  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开始,而后是reques、session以及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对象)。app

  1. ServletContext getServletContext():返回含有服务器相关信息的ServletContext对象
  2. String getInitParameter(String name):返回初始化参数的值
  3. Enumeration getInitParameterNames():返回Servlet初始化所需全部参数的枚举
相关文章
相关标签/搜索