版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
实验五Servlet的常用功能一、实验目的1、掌握Servlet的作用域和存取数据。2、掌握Servlet实现请求转发和重定向。二、实验环境PC系列微机:CPU2G以上,内存2G以上,硬盘可以空间:5G以上。操作系统:Windows10。三、实验步骤具体内容参考教材《JavaWeb应用开发基础教程》第3章。1、在Eclipse中创建JavaWeb项目;在JavaWeb项目中创建RequestScopeServlet并使用注解配置RequestScopeServlet;在RequestScopeServlet中,创建User对象,将User对象保存到Request作用域中;使用请求转发跳转到另一个ServletReceiveServlet,在ReceiveServlet中获取RequestScopeServlet中保存到Request作用域的User对象;部署JavaWeb项目到Tomcat,测试访问RequestScopeServlet。2、修改1中的RequestScopeServlet,使用Servlet重定向跳转到另一个ServletReceiveServlet,在ReceiveServlet中尝试获取RequestScopeServlet中保存到Request作用域的User对象;部署JavaWeb项目到Tomcat,测试访问RequestScopeServlet。3、编写SessionScopeServlet实现将创建的User对象保存到Session作用域、跳转到SessionReceiveServlet中并在SessionReceiveServlet中获取保存的User对象。4、编写ApplicationScopeServlet实现将创建的User对象保存到Application作用域、跳转到ApplicationReceiveServlet中并在ApplicationReceiveServlet中获取保存的User对象。四、实验报告1、请写出实验步骤1的RequestScopeServlet和ReceiveServlet的完整代码。①RequestScopeServlet的完整代码:packageexp5;importjava.io.IOException;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebServlet;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;@WebServlet(name="RequestScopeServlet",urlPatterns={"/RequestScope"})publicclassRequestScopeServletextendsHttpServlet{ publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{ doPost(request,response); } publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{ Useruser=newUser(); user.setName("Mike"); user.setPassword("123"); user.setAge(22); request.setAttribute("user",user); //请求转发 request.getRequestDispatcher("Receive").forward(request,response); }②ReceiveServlet的完整代码:packageexp5;importjava.io.IOException;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebServlet;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;@WebServlet(name="ReceiveServlet",urlPatterns={"/Receive"})//ReceiveServlet此处要和上一页转发里面填写的地址一样,不然会出错的,找了好久的错误publicclassReceiveServletextendsHttpServlet{ publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{ doPost(request,response); } publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{ //设置请求接收信息的编码格式 request.setCharacterEncoding("UTF-8"); //接收传过来的对象 Useru=(User)request.getAttribute("user"); //在控制台打印输出接收到的信息 System.out.println("name="+u.getName()+";password="+u.getPassword()+";age="+u.getAge()); }}请写出实验步骤2的RequestScopeServlet和ReceiveServlet的完整代码。①RequestScopeServlet的完整代码:packageexp5;importjava.io.IOException;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebServlet;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;@WebServlet(name="RequestScopeServlet",urlPatterns={"/RequestScope"})publicclassRequestScopeServletextendsHttpServlet{ publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{ doPost(request,response); } publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{ Useruser=newUser(); user.setName("Mike"); user.setPassword("123"); user.setAge(22); request.setAttribute("user",user); //请求转发 request.getRequestDispatcher("Receive").forward(request,response);//ReceiveServlet //重定向 response.sendRedirect("Receive"); }}②ReceiveServlet的完整代码:packageexp5;importjava.io.IOException;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebServlet;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;@WebServlet(name="ReceiveServlet",urlPatterns={"/Receive"})publicclassReceiveServletextendsHttpServlet{ publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{ doPost(request,response); } publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{ //设置请求接收信息的编码格式 request.setCharacterEncoding("UTF-8"); Useru=(User)request.getAttribute("user"); //在控制台打印输出接收到的信息 System.out.println("name="+u.getName()+";password="+u.getPassword()+";age="+u.getAge()); }}请说明实验步骤1的运行结果和实验步骤2的运行结果,并说明实验步骤1与2的运行结果差异的原因。步骤一结果是控制台输出打印结果;步骤二空。原因:重定向会丢失原来的请求信息。请写出SessionScopeServlet和SessionReceiveServlet的完整代码。①SessionScopeServlet的完整代码:packageexp5;importjava.io.IOException;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebServlet;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjavax.servlet.http.HttpSession;@WebServlet("/SessionScopeServlet")publicclassSessionScopeServletextendsHttpServlet{ publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{ doPost(request,response); } publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{ Useruser=newUser(); user.setName("Mike"); user.setPassword("123"); user.setAge(22); HttpSessionsession=request.getSession(); session.setAttribute("user",user); //转发 request.getRequestDispatcher("SessionReceiveServlet").forward(request,response); }}②SessionReceiveServlet的完整代码:packageexp5;importjava.io.IOException;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebServlet;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjavax.servlet.http.HttpSession;@WebServlet("/SessionReceiveServlet")publicclassSessionReceiveServletextendsHttpServlet{ publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{ doPost(request,response); } publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{ //设置请求接收信息的编码格式 request.setCharacterEncoding("UTF-8"); //接收传过来的信息 HttpSessionsession=request.getSession(); Useru=(User)session.getAttribute("user"); //在控制台打印输出接收到的信息 System.out.println("name="+u.getName()+";password="+u.getPassword()+";age="+u.getAge()); }}请写出ApplicationScopeServlet和ApplicationReceiveServlet的完整代码。①ApplicationScopeServlet的完整代码:packageexp5;importjava.io.IOException;importjavax.servlet.ServletContext;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebServlet;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;@WebServlet("/ApplicationScopeServlet")publicclassApplicationScopeServletextendsHttpServlet{ publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{ doPost(request,response); } publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{ Useruser=newUser(); user.setName("Mike"); user.setPassword("123"); user.setAge(22); ServletContextapplication=this.getServletContext(); application.setAttribute("user",user); //转发 request.getRequestDispatcher("ApplicationReceiveServlet").forward(request,response); }}②ApplicationReceiveServlet的完整代码:packageexp5;importjava.io.IOException;importjavax.servlet.ServletContext;import
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 农业用排灌机产业链招商引资的调研报告
- 企业票据贴现服务行业市场调研分析报告
- 2024年创新型人才培训合同
- 2023电商平台反诈骗用户保护方案
- 2024年冷库工程改造施工合同范本
- 2024年优化版工程设备租赁协议
- 医用口气清新剂产品供应链分析
- 在线学习平台作业管理方案
- 2024年商务接送合同范本
- 健身俱乐部会员管理与运营方案
- 亚马逊账户安全培训试题
- 《饭店服务心理学》课程教案
- 政务短视频运营方案书
- 电厂消防安全知识讲座
- 陕西2016-2020五年中考英语分题型考点统计
- 护士团队的协作和领导力培养培训课件
- 国有资产委托经营管理协议
- 双方合作责任协议
- 2024年陕煤集团招聘笔试参考题库含答案解析
- 冬季防溺水安全课件
- vsd负压吸引术护理查房
评论
0/150
提交评论