Servlet编程

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

实验报告实验3Servlet编程报告书名称实验3-1重定向和转发姓名指导教师学号日期任务清单实验内容重定向的功能是将用户从当前页面或servlet定向到另一个JSP页面或servlet;转发的功能是将用户对当前JSP页面或servlet对象的请求转发给另一个JSP页面或servlet对象。在servlet开发中,HttpServletResponse类提供了一种重定向方法sendRedirect,RequestDispatcher类提供了一种转发方法forward。请完成一个小实验Redirect,要求如下:用户通过input.jsp页面提供的表单输入姓名和年龄,并提交给名字为verify的servlet对象(由Verify类负责创建)。如果用户输入的数据不完整(没有输入姓名或年龄)或输入的年龄不合法(比如小于1或大于150),那么verify就将用户重定向到input.jsp页面;如果用户输入的数据符合要求,verify就将用户对input.jsp页面的请求转发给名字为showMessage的servlet对象(由ShowMessage类负责创建),该servlet对象显示用户输入的信息。实验步骤1.请参照实验附加材料给出的建立J2EE项目的基本步骤,自己建立一个Servlet项目,参数的名称按照实验的要求设置。2.按照实验要求编写实验代码。3.最后打开浏览器输入相关地址,执行操作检验代码执行结果。实验预估时间50分钟2实验结果1.请将所有的实验结果文件和此实验报告一起打包。2.将实验代码和结果的截图粘贴到下面的空白处:实验代码:一、web.xml代码:?xmlversion=1.0encoding=UTF-8?web-appversion=2.5xmlns=:xsi=:schemaLocation=://java.sun.com/xml/ns/javaee/web-app_2_5.xsdservletdescriptionThisisthedescriptionofmyJ2EEcomponent/descriptiondisplay-nameThisisthedisplaynameofmyJ2EEcomponent/display-nameservlet-nameVerify/servlet-nameservlet-classcom.servlet.Verify/servlet-class/servletservletdescriptionThisisthedescriptionofmyJ2EEcomponent/descriptiondisplay-nameThisisthedisplaynameofmyJ2EEcomponent/display-nameservlet-nameShowMessage/servlet-nameservlet-classcom.servlet.ShowMessage/servlet-class/servletservlet-mappingservlet-nameVerify/servlet-nameurl-pattern/Verify/url-pattern/servlet-mappingservlet-mappingservlet-nameShowMessage/servlet-name3url-pattern/ShowMessage/url-pattern/servlet-mappingwelcome-file-listwelcome-fileinput.jsp/welcome-file/welcome-file-list/web-app二、input.jsp代码:?xmlversion=1.0encoding=UTF-8?web-appversion=2.5xmlns=:xsi=:schemaLocation=://java.sun.com/xml/ns/javaee/web-app_2_5.xsdservletdescriptionThisisthedescriptionofmyJ2EEcomponent/descriptiondisplay-nameThisisthedisplaynameofmyJ2EEcomponent/display-nameservlet-nameVerify/servlet-nameservlet-classgjk.Verify/servlet-class/servletservletdescriptionThisisthedescriptionofmyJ2EEcomponent/descriptiondisplay-nameThisisthedisplaynameofmyJ2EEcomponent/display-nameservlet-nameShowMessage/servlet-nameservlet-classgjk.ShowMessage/servlet-class/servletservlet-mappingservlet-nameVerify/servlet-nameurl-pattern/Verify/url-pattern4/servlet-mappingservlet-mappingservlet-nameShowMessage/servlet-nameurl-pattern/ShowMessage/url-pattern/servlet-mappingwelcome-file-listwelcome-fileinput.jsp/welcome-file/welcome-file-list/web-app三、Verify.java代码:packagegjk;importjava.io.*;importjava.net.*;importjavax.servlet.*;importjavax.servlet.http.*;importjava.io.IOException;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;publicclassVerifyextendsHttpServlet{Stringusername;Stringage;publicVerify(){}protectedvoidprocessRequest(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{response.setContentType(text/html;charset=GB2312);request.setCharacterEncoding(gb2312);username=request.getParameter(username);age=request.getParameter(age);5if(username==null||username==||age==null||age==||Integer.parseInt(age)150||Integer.parseInt(age)1)response.sendRedirect(/Web/input.jsp);else{RequestDispatcherdispatcher=request.getRequestDispatcher(ShowMessage);dispatcher.forward(request,response);}}protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{processRequest(request,response);}protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{processRequest(request,response);}publicStringgetServletInfo(){returnShortdescription;}}三、ShowMessage.java代码:packagegjk;importjava.io.*;importjava.net.*;importjavax.servlet.*;importjavax.servlet.http.*;importjava.io.IOException;importjavax.servlet.ServletException;6importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;publicclassShowMessageextendsHttpServlet{publicShowMessage(){}protectedvoidprocessRequest(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{response.setContentType(text/html;charset=GB2312);request.setCharacterEncoding(gb2312);PrintWriterout=response.getWriter();out.println(HTMLBODY\n+你输入的是:+用户名:+request.getParameter(username)+年龄:+request.getParameter(age)+/BODY/HTML);}protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{processRequest(request,response);}protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{processRequest(request,response);}publicStri

1 / 23
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功