-
Servlet的MVC模式(策画等差、等比数列的和)
添加时间:2013-5-14 点击量:Web应用供给两个jsp页面,一个负责应用户输入等差数列的首项、公差和乞降项以及等比数列的首项、公比和乞降项数;另一个页面可以显示等差数列和等比数列的乞降成果。Web应用供给一个名字为computerSum的servlet对象,computerSum负责策画等差数列和的呢公比数列的和,然后将有关数据存储到javabean中,Web应用供给的Javabean负责描画等差数列和等比数列,供给简单的获取数据和批改数据。
参加web.xml文件中的字标识表记标帜如下:
1 <servlet>
2 <servlet-name>computerSum</servlet-name>
3 <servlet-class>sun.yourservlet.HandleSum</servlet-class>
4 </servlet>
5 <servlet-mapping>
6 <servlet-name>computerSum</servlet-name>
7 <url-pattern>/lookSum</url-pattern>
8 </servlet-mapping>1>模型(Javabean0
1 package moon.yourbean;
2
3 public class Series {
4 double firstItem; //数列首项
5 double var; //公差或公比
6 int number; //乞降项数
7 double sum; //乞降成果
8 String name = ; //数列类别
9
10 public void setFirstItem(double a) {
11 firstItem = a;
12 }
13
14 public double getFirstItem() {
15 return firstItem;
16 }
17
18 public void setVar(double b) {
19 var = b;
20 }
21
22 public double getVar() {
23 return var;
24 }
25
26 public void setNumber(int n) {
27 number = n;
28 }
29
30 public double getNumber() {
31 return number;
32 }
33
34 public void setSum(double s) {
35 sum = s;
36 }
37
38 public double getSum() {
39 return sum;
40 }
41
42 public void setName(String na) {
43 name = na;
44 }
45
46 public String getName() {
47 return name;
48 }
49 }2>视图(jsp页面)
inputMess.jsp1 <%@ page contentType=text/html;charset=GB2312%>
2 <HTML>
3 <BODY>
4 <Font size=2>
5 <FORM action=lookSum Method=post>等差数列乞降: <BR>
6 输入首项:<Input type=text name=firstItem size=4> 输入公差:<Input type=text name=var size=4> 乞降项数:<Input
7 type=text name=number size=4> <Input type=submit value=提交></FORM>
8 <FORM action=lookSum Method=get>等比数列乞降: <BR>
9 输入首项:<Input type=text name=firstItem size=4> 输入公比:<Input type=text name=var size=4> 乞降项数:<Input
10 type=text name=number size=4> <Input type=submit value=提交></FORM>
11 </Font>
12 </BODY>
13 </HTML>showResult.jsp
1 <%@ page contentType=text/html;charset=GB2312%>
2 <%@ page import=moon.yourbean.%>
3 <jsp:useBean id=lie type=moon.yourbean.Series scope=request />
4 <HTML>
5 <BODY>
6 <Table border=1>
7 <tr>
8 <th>数列的首项</th>
9 <th><jsp:getProperty name=lie property=name /></th>
10 <th>所求项数</th>
11 <th>乞降成果</th>
12 </tr>
13 <td><jsp:getProperty name=lie property=firstItem /></td>
14 <td><jsp:getProperty name=lie property=var /></td>
15 <td><jsp:getProperty name=lie property=number /></td>
16 <td><jsp:getProperty name=lie property=sum /></td>
17 </tr>
18 </Table>
19 </BODY>
20 </HTML>3>把握器(servlet)
1 package sun.yourservlet;
2
3 import moon.yourbean.;
4 import java.io.;
5 import javax.servlet.;
6 import javax.servlet.http.;
7
8 public class HandleSum extends HttpServlet {
9 public void init(ServletConfig config) throws ServletException {
10 super.init(config);
11 }
12
13 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
14 Series shulie = new Series(); //创建Javabean对象
15 request.setAttribute(lie, shulie);//将shulie存储到HttpServletRequest对象中
16 double a = Double.parseDouble(request.getParameter(firstItem));
17 double d = Double.parseDouble(request.getParameter(var));
18 int n = Integer.parseInt(request.getParameter(number));
19 shulie.setFirstItem(a); //将数据存储在shulie中
20 shulie.setVar(d);
21 shulie.setNumber(n);
22 //策画和:
23 double sum = 0, item = a;
24 int i = 1;
25 shulie.setName(等差数列的公差);
26 while (i <= n) //策画等差数列的和
27 {
28 sum = sum + item;
29 i++;
30 item = item + d;
31 }
32 shulie.setSum(sum);
33 RequestDispatcher dispatcher = request.getRequestDispatcher(showResult.jsp);
34 dispatcher.forward(request, response); //恳求showResult.jsp显示shulie中的数据
35 }
36
37 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
38 Series shulie = new Series();
39 request.setAttribute(lie, shulie);
40 double a = Double.parseDouble(request.getParameter(firstItem));
41 double d = Double.parseDouble(request.getParameter(var));
42 int n = Integer.parseInt(request.getParameter(number));
43 shulie.setFirstItem(a);
44 shulie.setVar(d);
45 shulie.setNumber(n);
46 //策画和:
47 double sum = 0, item = a;
48 int i = 1;
49 shulie.setName(等比数列的公比);
50 while (i <= n) //策画等比数列的和
51 {
52 sum = sum + item;
53 i++;
54 item = item d;
55 }
56 shulie.setSum(sum);
57 RequestDispatcher dispatcher = request.getRequestDispatcher(showResult.jsp);
58 dispatcher.forward(request, response);
59 }
60 }
我俩之间有着强烈的吸引力。短短几个小时后,我俩已经明白:我们的心是一个整体的两半,我俩的心灵是孪生兄妹,是知己。她让我感到更有活力,更完美,更幸福。即使她不在我身边,我依然还是感到幸福,因为她总是以这样或者那样的方式出现在我心头。——恩里克·巴里奥斯《爱的文明》