您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 人事档案/员工关系 > JavaWeb实验3
13.3实验三HTTP响应对象一.实验目的1.了解和掌握如何从响应对象获得输出流对象。2.掌握如何设置响应类型。3.掌握如何设置响应头和状态码。4.重点掌握如何实现响应重定向。二.实验内容任务1:在Eclipse的helloweb项目中创建下面的Servlet,实现通过设置响应内容类型构建Excel电子表格。packagecom.demo;importjava.io.*;importjava.util.*;importjavax.servlet.*;importjavax.servlet.http.*;importjavax.servlet.annotation.WebServlet;@WebServlet(/ExcelServlet)publicclassExcelServletextendsHttpServlet{publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{response.setHeader(Content-Encoding,gb2312);response.setContentType(application/vnd.ms-excel;charset=gb2312);PrintWriterout=response.getWriter();out.println(学号\t姓名\t性别\t年龄\t所在系);out.println(95001\t李勇\t男\t20\t信息);out.println(95002\t刘晨\t女\t19\t数学);}}该程序的运行结果如下图所示:图3.1生成Excel电子表格修改上述Servlet,将下面一行:response.setContentType(application/vnd.ms-excel;charset=gb2312);改为:2response.setContentType(application/msword;charset=gb2312);重新访问该Servlet,看结果如何?任务2:下面的ShowTimeServlet通过设置refresh响应头,实现每2秒钟刷新一次页面。packagecom.demo;importjava.io.*;importjava.util.*;importjava.text.SimpleDateFormat;importjavax.servlet.*;importjavax.servlet.http.*;publicclassShowTimeServletextendsHttpServlet{publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{response.setContentType(text/html;charset=gb2312);response.setHeader(refresh,2);PrintWriterout=response.getWriter();Datedate=newDate();SimpleDateFormatsdf=newSimpleDateFormat(yyyy-MM-ddaHH:mm:ss);Strings=sdf.format(date);out.println(html);out.println(body);out.println(h3align=\center\每2秒钟刷新一次页面/h3p);out.println(h3align=\center\现在的时间是:+s+/h3p);out.println(/body);out.println(/html);}}任务3:响应重定向步骤1:建立下面的login.html登录页面htmlheadtitleLoginPage/title/headbodyformaction=RedirectServletmethod=posttablewidth=298border=0align=centercellpading=2cellspacing=1trtdalign=right用户名:/tdtdalign=leftinputtype=textname=usernamesize=15/td/trtrtdalign=right密 码:/tdtdinputtype=passwordname=passwordsize=15/td/trtrtdcolspan=2align=centerinputtype=submitvalue=登录 inputtype=resetvalue=取消/td/tr/form/body/html3步骤2:编写一个名为RedirectServlet的Servlet,在其中检索用户名和口令参数值,然后判断其是否正确(若用户名和口令都为admin即认为正确),如果正确将响应重定向到welcome.html页面,否则将响应重定向到error.html页面,这两个页面请你自己编写,存放在应用程序根目录。下面是Servlet的部分代码。packagecom.demo;importjava.io.*;importjavax.servlet.*;importjavax.servlet.http.*;publicclassRedirectServletextendsHttpServlet{publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsIOException,ServletException{//此处添加若干代码,获取用户名和密码值if(){response.sendRedirect();}else{response.sendRedirect();}}}任务4:实现文件上传步骤1:建立upload.html页面,主要代码如下:formmethod=postaction=do_upload.jspENCTYPE=multipart/form-datainputtype=hiddenname=TESTvalue=goodtablewidth=50%border=0align=centertrtddivalign=center1、inputtype=FILEname=FILE1size=30/div/td/trtrtddivalign=center2、inputtype=FILEname=FILE2size=30/div/td/trtrtddivalign=center3、inputtype=FILEname=FILE3size=30/div/td/trtrtddivalign=center4、inputtype=FILEname=FILE4size=30/div/td/trtrtddivalign=centerinputtype=submitname=Submitvalue=上传它!/div/td/tr/table/form步骤2:将jspSmartUpload.jar文件复制到webcontent文件夹中的lib目录中。步骤3:建立do_upload.jsp文件,处理上传的文件。%@pagecontentType=text/html;charset=GB2312%%@pagelanguage=javaimport=com.jspsmart.upload.*%4%com.jspsmart.upload.SmartUploadmySmartUpload=newcom.jspsmart.upload.SmartUpload();intcount=0;mySmartUpload.initialize(pageContext);mySmartUpload.setTotalMaxFileSize(1000000);mySmartUpload.upload();//设置上传路径Stringpath=getServletContext().getRealPath(/);path=path+\\+upload;//out.println(path+br);//设置上传路径try{count=mySmartUpload.save(path);out.println(count+file(s)uploaded.);out.print(br);for(inti=0;icount;i++){FilemyFile=mySmartUpload.getFiles().getFile(i);Stringfn=myFile.getFileName();//out.print(imgsrc=\\upload\\upload\\+myFile.getFileName()+);//out.print(br);//out.print(文件fontcolor=red+fn+/font的地址为:);//out.print();}}catch(Exceptione){out.println(e.toString());}%步骤4:在out.println(path+br);所示的目录中创建upload文件夹,用来存放上传的文件。任务4:实现文件下载步骤1:建立download.html页面,主要代码如下:htmlheadtitle下载/title/headbodyahref=do_download.jsp点击下载/a/body/html步骤2:建立do_download.jsp文件,主要代码如下:%@pagecontentType=text/html;charset=gb2312import=com.jspsmart.upload.*%htmlbody%SmartUploadsu=newSmartUpload();//新建一个SmartUpload对象su.initialize(pageContext);//初始化su.setContentDisposition(null);//设定contentDisposition为null以禁止浏览器自动打开5文件Stringpath=getServletContext().getRealPath(/);path=path+\\+upload;su.downloadFile(path+/123.docx);//下载文件%/body/html三.思考题1.如何理解Servlet的生命周期?2.如何理解请求转发与响应重定向?
本文标题:JavaWeb实验3
链接地址:https://www.777doc.com/doc-2880531 .html