JAVA WEB_JSP的初步(9)

来源:互联网 发布:淘宝店铺女装简介 编辑:程序博客网 时间:2024/04/29 07:34
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")%>位访问者