Servlet的MVC模式(策画一元二次方程的根)
添加时间:2013-5-14 点击量:
设计一个Web应用,该Web应用供给两个jsp页面,一个页面是用户输入一元二次方程的系数,另一个显示一元二次方程的系和根。Web应用供给一个名字为handle的servlet对象,handle负责策画一元二次方程的根,然后将有关数据存储到JavaBean中;Web应用供给的javaBean负责描画一个一元二次方程,供给简单的获取数据和批改数据的办法。
用户须要为web.xml文件添加的如下字标识表记标帜:
1 <servlet>
2 <servlet-name>handle</servlet-name>
3 <servlet-class>sun.yourservlet.HandleData</servlet-class>
4 </servlet>
5 <servlet-mapping>
6 <servlet-name>handle</servlet-name>
7 <url-pattern>/handleData</url-pattern>
8 </servlet-mapping>
1>模型(javabean)
1 package moon.yourbean;
2
3 public class Equation
4 { double a,b,c;
5 String rootOne,rootTwo;
6 boolean squareEquation;
7 public void setA(double a)
8 { this.a=a;
9 }
10 public double getA()
11 { return a;
12 }
13 public void setB(double b)
14 { this.b=b;
15 }
16 public double getB()
17 { return b;
18 }
19 public void setC(double c)
20 { this.c=c;
21 }
22 public double getC()
23 { return c;
24 }
25 public void setRootOne(String root)
26 { rootOne=root;
27 }
28 public String getRootOne()
29 { return rootOne;
30 }
31 public void setRootTwo(String root)
32 { rootTwo=root;
33 }
34 public String getRootTwo()
35 { return rootTwo;
36 }
37 public void setIsSquareEquation(boolean b)
38 { squareEquation=b;
39 }
40 public boolean getIsSquareEquation()
41 { return squareEquation;
42 }
43 }
2>试图(jsp页面)
input.jsp
1 <%@ page contentType=text/html;charset=GB2312 %>
2 <HTML><BODY ><Font size=2>
3 <FORM action=handleData Method=post >
4 <BR>输入一元二次方程的系数:
5 <BR> 二次项系数a:<Input type=text name=a size=4>
6 一次项系数b:<Input type=text name=b size=4>
7 常数项c:<Input type=text name=c size=4>
8 <Input type=submit value=提交>
9 </FORM>
10 </Font></BODY></HTML>
show.jsp
1 <%@ page contentType=text/html;charset=GB2312 %>
2 <%@ page import=moon.yourbean.%>
3 <jsp:useBean id=equation type=moon.yourbean.Equation scope=request/>
4 <HTML><BODY ><Font size=2>
5 一元二次方程的系数是:
6 <BR> 二次项系数:<jsp:getProperty name=equation property=a/>
7 一次项系数:<jsp:getProperty name=equation property=b/>
8 常数项:<jsp:getProperty name=equation property=c/>
9 <BR>是一元二次方程吗?<jsp:getProperty name=equation property=isSquareEquation/>
10 <BR>方程的两个根是:<jsp:getProperty name=equation property=rootOne/>,
11 <jsp:getProperty name=equation property=rootTwo/>
12 </FONT></BODY></HTML>
3>把握器(servlet)
1 package sun.yourservlet;
2 import moon.yourbean.;
3 import java.io.;
4 import javax.servlet.;
5 import javax.servlet.http.;
6 public class HandleData extends HttpServlet
7 { public void init(ServletConfig config) throws ServletException
8 {super.init(config);
9 }
10 public void doPost(HttpServletRequest request,HttpServletResponse response)
11 throws ServletException,IOException
12 { Equation equ=new Equation(); //创建Javabean对象
13 request.setAttribute(equation,equ);// 将equ存储到HttpServletRequest对象中
14 double a=Double.parseDouble(request.getParameter(a));
15 double b=Double.parseDouble(request.getParameter(b));
16 double c=Double.parseDouble(request.getParameter(c));
17 equ.setA(a); //将数据存储在equ中
18 equ.setB(b);
19 equ.setC(c);
20 if(a!=0)
21 equ.setIsSquareEquation(true);
22 else
23 equ.setIsSquareEquation(true);
24 double disk=bb-4ac;
25 if(disk<0)
26 { equ.setRootOne(无实根);
27 equ.setRootTwo(无实根);
28 }
29 else
30 { double root1=(-b+Math.sqrt(disk))/(2a),
31 root2=(-b-Math.sqrt(disk))/(2a);
32 equ.setRootOne(+root1);
33 equ.setRootTwo(+root2);
34 }
35 RequestDispatcher dispatcher=request.getRequestDispatcher(show.jsp);
36 dispatcher.forward(request,response); //恳求show.jsp显示equ中的数据
37 }
38 public void doGet(HttpServletRequest request,HttpServletResponse response)
39 throws ServletException,IOException
40 { doPost(request,response);
41 }
42 }
所有随风而逝的都属于昨天的,所有历经风雨留下来的才是面向未来的。—— 玛格丽特·米切尔 《飘》
设计一个Web应用,该Web应用供给两个jsp页面,一个页面是用户输入一元二次方程的系数,另一个显示一元二次方程的系和根。Web应用供给一个名字为handle的servlet对象,handle负责策画一元二次方程的根,然后将有关数据存储到JavaBean中;Web应用供给的javaBean负责描画一个一元二次方程,供给简单的获取数据和批改数据的办法。
用户须要为web.xml文件添加的如下字标识表记标帜:
1 <servlet>
2 <servlet-name>handle</servlet-name>
3 <servlet-class>sun.yourservlet.HandleData</servlet-class>
4 </servlet>
5 <servlet-mapping>
6 <servlet-name>handle</servlet-name>
7 <url-pattern>/handleData</url-pattern>
8 </servlet-mapping>
1>模型(javabean)
1 package moon.yourbean;
2
3 public class Equation
4 { double a,b,c;
5 String rootOne,rootTwo;
6 boolean squareEquation;
7 public void setA(double a)
8 { this.a=a;
9 }
10 public double getA()
11 { return a;
12 }
13 public void setB(double b)
14 { this.b=b;
15 }
16 public double getB()
17 { return b;
18 }
19 public void setC(double c)
20 { this.c=c;
21 }
22 public double getC()
23 { return c;
24 }
25 public void setRootOne(String root)
26 { rootOne=root;
27 }
28 public String getRootOne()
29 { return rootOne;
30 }
31 public void setRootTwo(String root)
32 { rootTwo=root;
33 }
34 public String getRootTwo()
35 { return rootTwo;
36 }
37 public void setIsSquareEquation(boolean b)
38 { squareEquation=b;
39 }
40 public boolean getIsSquareEquation()
41 { return squareEquation;
42 }
43 }
2>试图(jsp页面)
input.jsp
1 <%@ page contentType=text/html;charset=GB2312 %>
2 <HTML><BODY ><Font size=2>
3 <FORM action=handleData Method=post >
4 <BR>输入一元二次方程的系数:
5 <BR> 二次项系数a:<Input type=text name=a size=4>
6 一次项系数b:<Input type=text name=b size=4>
7 常数项c:<Input type=text name=c size=4>
8 <Input type=submit value=提交>
9 </FORM>
10 </Font></BODY></HTML>
show.jsp
1 <%@ page contentType=text/html;charset=GB2312 %>
2 <%@ page import=moon.yourbean.%>
3 <jsp:useBean id=equation type=moon.yourbean.Equation scope=request/>
4 <HTML><BODY ><Font size=2>
5 一元二次方程的系数是:
6 <BR> 二次项系数:<jsp:getProperty name=equation property=a/>
7 一次项系数:<jsp:getProperty name=equation property=b/>
8 常数项:<jsp:getProperty name=equation property=c/>
9 <BR>是一元二次方程吗?<jsp:getProperty name=equation property=isSquareEquation/>
10 <BR>方程的两个根是:<jsp:getProperty name=equation property=rootOne/>,
11 <jsp:getProperty name=equation property=rootTwo/>
12 </FONT></BODY></HTML>
3>把握器(servlet)
1 package sun.yourservlet;
2 import moon.yourbean.;
3 import java.io.;
4 import javax.servlet.;
5 import javax.servlet.http.;
6 public class HandleData extends HttpServlet
7 { public void init(ServletConfig config) throws ServletException
8 {super.init(config);
9 }
10 public void doPost(HttpServletRequest request,HttpServletResponse response)
11 throws ServletException,IOException
12 { Equation equ=new Equation(); //创建Javabean对象
13 request.setAttribute(equation,equ);// 将equ存储到HttpServletRequest对象中
14 double a=Double.parseDouble(request.getParameter(a));
15 double b=Double.parseDouble(request.getParameter(b));
16 double c=Double.parseDouble(request.getParameter(c));
17 equ.setA(a); //将数据存储在equ中
18 equ.setB(b);
19 equ.setC(c);
20 if(a!=0)
21 equ.setIsSquareEquation(true);
22 else
23 equ.setIsSquareEquation(true);
24 double disk=bb-4ac;
25 if(disk<0)
26 { equ.setRootOne(无实根);
27 equ.setRootTwo(无实根);
28 }
29 else
30 { double root1=(-b+Math.sqrt(disk))/(2a),
31 root2=(-b-Math.sqrt(disk))/(2a);
32 equ.setRootOne(+root1);
33 equ.setRootTwo(+root2);
34 }
35 RequestDispatcher dispatcher=request.getRequestDispatcher(show.jsp);
36 dispatcher.forward(request,response); //恳求show.jsp显示equ中的数据
37 }
38 public void doGet(HttpServletRequest request,HttpServletResponse response)
39 throws ServletException,IOException
40 { doPost(request,response);
41 }
42 }
所有随风而逝的都属于昨天的,所有历经风雨留下来的才是面向未来的。—— 玛格丽特·米切尔 《飘》