java内置对象三

来源:互联网 发布:涂鸦移动 知乎 编辑:程序博客网 时间:2024/06/10 09:11

一、application对象

1.利用application获取基本信息

<%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Insert title here</title></head><body>服务器信息:<%=application.getServerInfo() %><br>应用名称:<%=application.getServletContextName() %><br>主机名称:<%=application.getVirtualServerName() %><br></body></html>

2.利用application获取页面访问次数

<%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Insert title here</title></head><body><%Object object=application.getAttribute("counter");if(object==null){application.setAttribute("counter", new Integer(1));out.println("该页面被访问了1次<br/>");}else{int counterValue=Integer.parseInt(object.toString());counterValue++;out.println("该页面被访问了"+counterValue+"次");application.setAttribute("counter",counterValue);}%></body></html>

二、config对象

       通过在web.xml文件中配置信息,然后在页面显示出来,例如在web.xml文件中加入如下代码:

 <servlet-name>config</servlet-name>  <jsp-file>/13/config.jsp</jsp-file>  <init-param>  <param-name>username</param-name>  <param-value>djx</param-value>  </init-param>  <init-param>  <param-name>password</param-name>  <param-value>djx993321</param-value>  </init-param>  </servlet>    <servlet-mapping>  <servlet-name>config</servlet-name>  <url-pattern>/13/*</url-pattern>  </servlet-mapping>

然后通过在13文件夹下新建config.jsp文件如下:

<%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>config使用示例</title></head><body>用户名:<%=config.getInitParameter("username") %><br/>密码:<%= config.getInitParameter("password") %></body></html>
  通过config.jsp文件中使用config对象就可以读出xml文件中的部署信息。


三、page对象

<%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Insert title here</title></head><body><%   out.println("page对象的字符串:"+page.toString()+"<br/>");      out.println(page.getClass().toString()+"<br/>");     out.println(page.hashCode());     %></body></html>



0 0
原创粉丝点击