ITeye博客

JSP application用上下文getServletContext()

chokee · 2014-05-23 09:33 · request/servlet · struts · 编程语言 · Markdown

JSP application用上下文getServletContext() JSP application对象的使用_ application 对象 1 什么是 application 对象 ? (1) 当 Web 服务器启动时,Web 服务器会自动创建一个 application 对象。 服务器启动时, 服务器会自动创建一个 对象。 application 对象一旦创建,它将一直存在,直到 Web 服务器关闭。 对象一旦创建,它将一直存在, 服务器关闭。 一个 Web 服务器通常有多个 Web 服务目录 (网站),当 Web 服务器启动时,它自动为每个 Web 服务目录都创建一个 application 对象,这些 application 对象 各自独立,而且和 Web 服务目录一一对应。对象, 因此, application 对象 访问同一个网站的客户都共享一个 application 对象, 可以实现多客户间的数据共享。访问不同网站的客户, 对象不同。 访问不同网站的客户,对应的 application 对象不同。 (2) application 对象的生命周期: Web 服务器启动,对象的生命周期 从 的生命周期: 直到 Web 服务器关闭。 注意: 应用程序启动后, 对象, 注意:一个 Web 应用程序启动后,将会自动创建一个 application 对象,而 对象, 且在整个应用程序的运行过程中只有一个 application 对象,也即所有访问该网 只有一个 对象。站的客户都共享一个 application 对象。 (3) application 对象的作用范围: 对象的作用范围 application 对象是一个应用程序级的对象, 的作用范围: 对象是一个应用程序级的对象, 它作用于当前 Web 应用程序,也即作用于当前网站,所有访问当前网站的客户作用于当前网站, 作用于当前网站 对象。 都共享一个 application 对象。 具体来说: 具体来说:不管哪个客户来访问网站 A,也不管客户访问网站 A 下哪个页 ,面文件,都可以对网站 对象进行操作, 面文件,都可以对网站 A 的 application 对象进行操作,因为,所有访问网站 A 的客户都共用一个 application 对象。 因此,当在 application 对象中存储数据后,所有访问网站 A 的客户都能够 对其进行访问,实现了多客户之间的数据共享。实现了多客户之间的数据共享。 实现了多客户之间的数据共享 (4) application 对象的基类是:javax.servlet.ServletContext 类。 对象的基类是: 注 意 : 有 些 Web 服 务 器 不 直 接 支 持 使 用 application 对 象 , 必 须 用 ServletContext 类来声明 application 对象,再调用 getServletContext()方法来获取 调用 方法来获取 对象。 当前页面的 application 对象。 (5) ServletContext 类:用于表示应用程序的上下文。 用于表示应用程序的上下文。 一个 ServletContext 类的对象表示一个 Web 应用程序的上下文。 应用程序的上下文。 具体来说: 服务器中, 应用程序的运行时环境, 具体来说:在 Web 服务器中,提供了一个 Web 应用程序的运行时环境,专 门 负 责 Web 应 用 程 序 的 部 署 、 编 译 、 运 行 以 及 生 命 周 期 的 管 理, 通 过 ServletContext 类,可以获取 Web 应用程序的运行时环境信息。 应用程序的运行时环境信息。 常识:JSP 引擎、Servlet 引擎、JSP 容器和 Servlet 容器,都是指 Web 服务 常识: 器,Web 服务器用于管理所有 Web 应用程序。 (6) application 对象的常用方法,如下所示: 对象的常用方法,如下所示: 方法名 String getAttribute(String name) Enumeration getAttributeNames() void object) void removeAttribute(String name) ServletContext getContext(String uripath) String getContextPath() setAttribute(String name, Object 描述 根据属性名称获取属性值。 获取所有的属性名称。设置属性,指定属性名称和属性值。 根据属性名称删除对应的属性。 获取指定 URL 的 ServletContext 对象。 获取当前 Web 应用程序的根目录。 String getInitParameter(String name) int getMajorVersion() int getMinorVersion() String getMimeType(String file) String getServletInfo() String getServletContextName() void log(String message) 根据初始化参数名称,获取初始化参数值。 获取 Servlet API 的主版本号。获取 Servlet API 的次版本号。 获取指定文件的 MIME 类型。 获取当前 Web 服务器的版本信息。 获取当前 Web 应用程序的名称。将信息写入日志文件中。 2


application 是javax.servlet.ServletContext接口的对象
2.在此对象之中提供了大量的方法,但是这些方法中最重要的应用就只有两组:
3.1。属性的操作
4. setAttribute(),getAttribute(),removeAttribute()
5. 6.2。取得虚拟目录对应的真实路径:
7.<import="java.io."%>
8.<%=application.getRealPath("/")%>
9.就是得到的以下的docBase内容
10. ----server.xml中
11. 12.3。getServletContext()方法
13. 一般application表示的是上下文,但是在实际的开发中,往往很少直接使用application,而使用getServletContext()方法来表示application.
14.范例:
15. <%=getServletContext().getRealPath()%>
16.这个方法表示由容器调用,如果非要加上对象的话,直接加上this关键字
17. 18.既然已经知道了真实路径,那么就可以进行
19. 20.<%
21. request.setCharacterEncoding("GBK");
22. String fileName = this.getServletContext().getRealPath("/")+"Notes"+File.separator+request.getParameter("filename");
23. String content = request.getParameter("content");
24. PrintStream ps = new PrimtStream(new FileOutputStream(new File(fileName)));
25. ps.println(content);
26. ps.close();
27.%>
28. 29.范例:列出一个文件夹下的文件名称
30.<%
31.String fileName = this.getServletContext().getRealPath("/")+"note";
32.File f = new File(fileName);
33.String files = f.list();
34.for(int i=0;i<files.length;i++){
35.%>
36. <%=files
%>
37.<%
38.}
39.%>
40.--------------------------------------------处理换行的问题
41.<%
42. request.setCharacterEncoding("GBK");
43. String fileName = this.getServletContext().getRealPath("/") +"notes"+File.separator + request.getParameter("filename");
44. String content = request.getParameter("content").replaceAll("\r\n","");
45. PrintStream ps = new PrintStream(new FileOutputStream(new File(fileName)));
46. ps.println(content);
47. ps.close();
48. %>
49.或者在读出来后直接 replaceAll("\r\n","");
50. 51. 52.4.可以使用application完成一个网站的计数器操作
53.每次当有新的用户来的时候,就要求记录的数字加1,在用户第一次来的时候进行计算
54.<%
55. if(session.isNew()){
56. save(++count,path); //这个方法是把计数器记录到一个文件当中
57. }
58.%>
59.<%
60. String path = getServletContext().getRealPath("/")+"count.txt";
61. int count = load(path); //这个方法是从文件中读取记录
62.%>
63.您是第<%=count%>位访问者
64. 65.--------------------------------------------------------------
66.简单的聊天室程序
67.main.jsp
68.
69.
70.
71.
72.input.jsp
73.<contentType="text/html;charset=GBK"%>
74.<import="java.util."%>
75. ----注意,这是传给自己,也就是传给自己后面的代码,为什么要传给自己,原因是我们不希望点击提交后变成别的页面,所以传给自己,或者使用js哦
76. 请输入内容:
77.
78.
79.<% //这里所做的事情就是得到当前说话的内容,然后放入application的list中
80. request.setCharacterEncoding("GBK");
81. if(request.getParameter("content") == null)return; //如果没有说话,那么不需更新刷新
82. List all = null;
83. all = (List)getServletContext().getAttribute("notes");
84. if(all == null){
85. all = new ArrayList();
86. }
87. all.add(request.getParameter("content"));
88. getServletContext().setAttribute("notes",all);
89.%>
90. 91.content.jsp //这个页面的任务就是每隔一段时间刷新一次,然后打印所有的留言
92. 93.<contentType="text/html;charset=GBK"%>
94.<import="java.util.
"%>
95.<%
96. response.setHeader("refresh","2");
97. request.setCharacterEncoding("GBK");
98. List all = (List)getServletContext().getAttribute("notes");
99. if(all == null){
100.%>
101. 没有留言
102.<%
103. }else{
104. for(Iterator it = all.iterator();it.hasNext();){
105.%>
106. <%=it.next()%>
107.<%
108. }
109. }
110.%> application 是javax.servlet.ServletContext接口的对象 在此对象之中提供了大量的方法,但是这些方法中最重要的应用就只有两组: 1。属性的操作 setAttribute(),getAttribute(),removeAttribute()

2。取得虚拟目录对应的真实路径: <import="java.io.*"%>

3。getServletContext()方法 一般application表示的是上下文,但是在实际的开发中,往往很少直接使用application,而使用getServletContext()方法来表示application. 范例:

既然已经知道了真实路径,那么就可以进行

<% request.setCharacterEncoding("GBK"); String fileName = this.getServletContext().getRealPath("/")+"Notes"+File.separator+request.getParameter("filename"); String content = request.getParameter("content"); PrintStream ps = new PrimtStream(new FileOutputStream(new File(fileName))); ps.println(content); ps.close(); %>

范例:列出一个文件夹下的文件名称 <% String fileName = this.getServletContext().getRealPath("/")+"note"; File f = new File(fileName); String files = f.list(); for(int i=0;i<files.length;i++){ %>

4.可以使用application完成一个网站的计数器操作 每次当有新的用户来的时候,就要求记录的数字加1,在用户第一次来的时候进行计算 <% if(session.isNew()){ save(++count,path); //这个方法是把计数器记录到一个文件当中 } %> <% String path = getServletContext().getRealPath("/")+"count.txt"; int count = load(path); //这个方法是从文件中读取记录 %>


简单的聊天室程序 main.jsp

content.jsp //这个页面的任务就是每隔一段时间刷新一次,然后打印所有的留言

<contentType="text/html;charset=GBK"%> <import="java.util.*"%> <% response.setHeader("refresh","2"); request.setCharacterEncoding("GBK"); List all = (List)getServletContext().getAttribute("notes"); if(all == null){ %>


给你一断代码,这个代码是防止用户重复登陆的,你修改下就可以了 package com.china.util;

import java.text.SimpleDateFormat; import java.util.Date; import java.util.Hashtable; import javax.servlet.ServletContext; import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSessionBindingEvent; import javax.servlet.http.HttpSessionBindingListener;

import com.china.pojo.CitiesAndEmpPojo;

public class OnlineOperatorListener implements HttpSessionBindingListener {

// 当前被监听的操作员Pojo实例 private CitiesAndEmpPojo operatorPojo; private HttpSession session = null; // 容器上下文对象 private ServletContext application = null; // 在线操作员列表 private static Hashtable<String, CitiesAndEmpPojo> operatorTable = null;

public OnlineOperatorListener(CitiesAndEmpPojo operatorPojo) {

this.operatorPojo = operatorPojo;

}

public synchronized boolean validateOperatorOnline(String operatorName) { return operatorTable.containsKey(operatorName);

}

public void valueBound(HttpSessionBindingEvent event) {

// 创建Http会话对象 this.session = event.getSession(); // 创建容器上下文对象 this.application = this.session.getServletContext(); // 将用户保存到在线列表 operatorTable.put(this.operatorPojo.getEmpcode(), this.operatorPojo); // 保存更改 this.application.setAttribute("OperatorTable", operatorTable);

}

public void valueUnbound(HttpSessionBindingEvent event) {

// 创建Http会话对象 this.session = event.getSession(); // 创建容器上下文对象 this.application = this.session.getServletContext(); // 将用户移出在线列表 operatorTable.remove(this.operatorPojo.getEmpcode()); // 保存更改 this.application.setAttribute("OperatorTable", operatorTable); }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。