JAVA WEB_JSP的初步(9)

本文介绍了如何利用Application内置对象进行文件操作,包括输入文件名和内容创建文件、查看文件夹信息、以及将文件内容显示在网页上,并展示了制作网页计数器的基本步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.利用application内置对象,进行I/O操作.
(1)利用表单输入文件名及文件内容,通过文件名及文件的内容来创建文件.
(2)查看文件所在文件夹的信息.
(3)将该文件的内容显示在网页上.


以下源码分别是app1.jsp;app2.jsp;app3.jsp;app4.jsp.

<%@page contentType="text/html;charset=GBK"%> <style type="text/css"> <!-- .STYLE1 { font-family: "宋体"; font-size: large; font-weight: bold; } --> </style> <form action="app2.jsp"method="post"> <div align="center" class="STYLE1">输入文件名称: <input type="text" name="filename"> <br> 输入文件内容: <textarea name="content"></textarea> <br> <input type="submit"value="提交"> </div> </form>
<%@page contentType="text/html;charset=GBK"%> <%@page import="java.io.*"%> <h1>文件写入成功</h1> <% request.setCharacterEncoding("GBK"); String fileName=this.getServletContext().getRealPath("/")+"mars"+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(); %>
<%@page contentType="text/html;charset=GBK"%> <%@page import="java.io.*"%> <h1></h1> <% request.setCharacterEncoding("GBK"); String fileName=this.getServletContext().getRealPath("/")+"mars"; File f = new File(fileName); String files[] = f.list(); for(int i=0;i<files.length;i++){ %> <h3><a href="app4.jsp?filename=<%=files[i]%>"><%=files[i]%></a></h3> <% } %>
<%@ page contentType="text/html;charset=GBK"%> <%@ page import="java.io.*"%> <% request.setCharacterEncoding("GBK") ; String fileName = this.getServletContext().getRealPath("/") + "mars" + File.separator + request.getParameter("filename") ; File f = new File(fileName) ; BufferedReader buf = new BufferedReader(new InputStreamReader(new FileInputStream(f))) ; String str = buf.readLine() ; // 读取内容 %> <h3><%=str%></h3> <% buf.close() ; // 关闭 %>


2.制作网页计数器.

<%@page contentType="text/html;charset=GBK"%> <% if(application.getAttribute("count")==null) { application.setAttribute("count","1"); } else { String str=application.getAttribute("count").toString(); int ncount=Integer.valueOf(str).intValue()+1; application.setAttribute("count",""+ncount); } %> 欢迎光临本网站,您是第 <%=application.getAttribute("count")%>位访问者




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值