物联网web应用程序开发基础学院计算机科学与工程学院学号11303010234姓名王韬指导教师唐继强使用教材JSP程序设计编写时间2015.6.17第8章:JavaServlet基础实验内容:(1)第8章8.10实验1:使用servlet读取文件;实验过程(实验1):readFile.jsp%@pagecontentType=text/html;charset=GB2312%HTMLBODYbgcolor=yellowFontsize=2P选择一个文件:FORMaction=helpReadmethod=postSelectname=fileNameOptionvalue=D:/1000/A.javaA.javaOptionvalue=D:/1000/Hello.javaHello.javaOptionvalue=D:/1000/E.javaE.java/SelectInputtype=submitvalue=提交/FORM/FONT/BODY/HTMLReadFile.javapackagemy.servlet;importjava.io.*;importjavax.servlet.*;importjavax.servlet.http.*;publicclassReadFileextendsHttpServlet{publicvoidinit(ServletConfigconfig)throwsServletException{super.init(config);}publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{StringfileName=request.getParameter(fileName);Stringcontent=getReadContent(fileName);response.setContentType(text/html;charset=GB2312);PrintWriterout=response.getWriter();out.println(htmlbody);out.println(TextAreaRows=8Cols=50+content+/TextArea);out.println(/body/html);}publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{doPost(request,response);}privateStringgetReadContent(StringfileName){//读取文件StringreadContent=;try{Filefile=newFile(fileName);FileReaderin=newFileReader(file)BufferedReaderinTwo=newBufferedReader(in);StringBufferstringbuffer=newStringBuffer();Strings=null;while((s=inTwo.readLine())!=null){bytebb[]=s.getBytes();s=newString(bb);stringbuffer.append(\n+s);}readContent=newString(stringbuffer);}catch(IOExceptione){readContent=+e;}returnreadContent;}}总结: