jsp----application

[code]
application 是javax.servlet.ServletContext接口的对象
在此对象之中提供了大量的方法,但是这些方法中最重要的应用就只有两组:
1。属性的操作
setAttribute(),getAttribute(),removeAttribute()

2。取得虚拟目录对应的真实路径:
<%@page import="java.io.*"%>
<h1><%=application.getRealPath("/")%>
就是得到的以下的docBase内容
<Context path="/demo" docBase="D:\webdemo" /> ----server.xml中

3。getServletContext()方法
一般application表示的是上下文,但是在实际的开发中,往往很少直接使用application,而使用getServletContext()方法来表示application.
范例:
<h1><%=getServletContext().getRealPath()%></h1>
这个方法表示由容器调用,如果非要加上对象的话,直接加上this关键字

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

<%
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++){
%>
<h3><%=files[i]%></h3>
<%
}
%>
--------------------------------------------处理换行的问题
<%
request.setCharacterEncoding("GBK");
String fileName = this.getServletContext().getRealPath("/") +"notes"+File.separator + request.getParameter("filename");
String content = request.getParameter("content").replaceAll("\r\n","<br>");
PrintStream ps = new PrintStream(new FileOutputStream(new File(fileName)));
ps.println(content);
ps.close();
%>
或者在读出来后直接 replaceAll("\r\n","<br>");


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

--------------------------------------------------------------
简单的聊天室程序
main.jsp
<frameset rows="80%,20%">
<frame name="top" src="content.jsp" />
<frame name="bottom" src="input.jsp"/>
</frameset>
input.jsp
<%@page contentType="text/html;charset=GBK"%>
<%@page import="java.util.*"%>
<form action="input.jsp" method="post"> ----注意,这是传给自己,也就是传给自己后面的代码,为什么要传给自己,原因是我们不希望点击提交后变成别的页面,所以传给自己,或者使用js哦
请输入内容:<input type="text" name="content"/>
<input type="submit" value="说话" />
</form>
<% //这里所做的事情就是 得到当前说话的内容,然后放入application的list中
request.setCharacterEncoding("GBK");
if(request.getParameter("content") == null)return; //如果没有说话,那么不需更新刷新
List all = null;
all = (List)getServletContext().getAttribute("notes");
if(all == null){
all = new ArrayList();
}
all.add(request.getParameter("content"));
getServletContext().setAttribute("notes",all);
%>

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

<%@page contentType="text/html;charset=GBK"%>
<%@page import="java.util.*"%>
<%
response.setHeader("refresh","2");
request.setCharacterEncoding("GBK");
List all = (List)getServletContext().getAttribute("notes");
if(all == null){
%>
<h2>没有留言</h2>
<%
}else{
for(Iterator it = all.iterator();it.hasNext();){
%>
<h3><%=it.next()%></h3>
<%
}
}
%>


[/code]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值