科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道web开发:详细讲解jsp的内置对象

web开发:详细讲解jsp的内置对象

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

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

作者:中国IT实验室 来源:中国IT实验室 2007年9月3日

关键字:

  • 评论
  • 分享微博
  • 分享邮件

在本页阅读全文(共3页)

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的日志文件
20    
      

<%@ 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() 重写异常的执行栈轨迹
5     

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) 在当前位置包含另一文件
21    
     
       

<%@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对象)

序号 方 法 说 明
1  ServletContext getServletContext() 返回含有服务器相关信息的ServletContext对象
2  String getInitParameter(String name) 返回初始化参数的值
3  Enumeration getInitParameterNames() 返回Servlet初始化所需所有参数的枚举

查看本文来源

    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章