您好,欢迎访问三七文档
当前位置:首页 > 临时分类 > 实验6-2-计算梯形的面积
实验6-2计算梯形的面积1.相关知识点servlet(BeanClass)负责创建生命周期为session的bean关键代码是:BeanClassbean=newBeanClass();session.setAttribute(“keyWord”,bean);上述代码会把bean存放到Tomcat引擎管理的内置对象pageContext中,该bean被指定的id是”keyWord”,scope(生命周期)是PageContext.session_SCOPE(session)。servlet负责根据模型中数据的变化通知视图(JSP页面)更新。对于scope是session的bean,其手段是使用转发或重定向,让所请求的JSP页面显示bean中的数据2.实验目的本实验的目的是让学生掌握在MVC模式中怎样使用scope是session的bean。3.实验要求视图提供两个JSP页面,一个页面使得用户可以输入梯形的上底、下底和高,并能显示梯形的上底、下底和高以及面积。数据模型提供的Javabean负责刻画梯形,即可以存储梯形的有关数据,提供简单的获取数据和修改数据的方法。控制器负责计算梯形的面积,然后将有关数据存储到Javabean数据模型中,然后用转发或重定向的方式请求视图中负责显示数据的JSP页面显示Javabean数据模型的数据。4.实验代码需要为ch5\We-INF服务目录的WEB—INF下的web.xml文件添加如下的子标记:servletservlet-namelookArea/servlet-nameservlet-classmyservlet.control.HandleArea/servlet-class/servletservlet-mappingservlet-namelookArea/servlet-nameurl-pattern/lookArea/url-pattern/servlet-mapping数据模型代码Area.javapackagemybean.data;publicclassArea{doublea,b,c;Stringmess;doublearea;publicdoublegetA(){returna;}publicvoidsetA(doublea){this.a=a;}publicdoublegetB(){returnb;}publicvoidsetB(doubleb){this.b=b;}publicdoublegetC(){returnc;}publicvoidsetC(doublec){this.c=c;}publicStringgetMess(){returnmess;}publicvoidsetMess(Stringmess){this.mess=mess;}publicvoidsetArea(doubles){area=s;}publicdoublegetArea(){returnarea;}}视图代码:inputData.jsp%@pagelanguage=javacontentType=text/html;charset=utf-8pageEncoding=utf-8%jsp:useBeanid=dataclass=mybean.data.Areascope=session/htmlbodyFontsize=2formaction=lookAreaMethod=get梯形:BR输入上底:inputtype=textname=asize=4输入下底:inputtype=textname=bsize=4输入高:inputtype=textname=csize=4inputtype=submitvalue=提交/formjsp:getPropertyname=dataproperty=mess/:上底jsp:getPropertyname=dataproperty=a/,下底jsp:getPropertyname=dataproperty=b/,高jsp:getPropertyname=dataproperty=c/br的面积是jsp:getPropertyname=dataproperty=area//Font/body/html控制器代码HandleArea.javapackagemyservlet.control;importjava.io.IOException;importjavax.servlet.ServletConfig;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjavax.servlet.http.HttpSession;importmybean.data.Area;publicclassHandleAreaextendsHttpServlet{publicvoidinit(ServletConfigconfig)throwsServletException{super.init(config);}publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{doPost(request,response);}publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{AreadataBean=newArea();HttpSessionsession=request.getSession(true);session.setAttribute(data,dataBean);try{doublea=Double.parseDouble(request.getParameter(a));doubleb=Double.parseDouble(request.getParameter(b));doublec=Double.parseDouble(request.getParameter(c));dataBean.setA(a);dataBean.setB(b);dataBean.setC(c);doubles=-1;s=(a+b)*c/2.0;dataBean.setArea(s);dataBean.setMess(梯形);}catch(Exceptione){dataBean.setArea(-1);dataBean.setMess(+e);}response.sendRedirect(inputData.jsp);}}5.实验结果运行inputData.jsp:输入数据后提交:提交后:Ps:编者语《JSP实用教程(第三版)》耿样义张跃平编著这本书上实验2到实验7,编者都有编辑文档,内容包括实验要求、实验目的、代码甚至必要操作。请记住编者ID:宁十一520
本文标题:实验6-2-计算梯形的面积
链接地址:https://www.777doc.com/doc-4638678 .html