JSP程序设计习题解答[1]..

}

public double getB() { return b; }

public void setC(double c) { this.c=c; }

public double getC() { return c; }

public void setRootOne(String root) { rootOne=root; }

public String getRootOne() { return rootOne; }

public void setRootTwo(String root) { rootTwo=root; }

public String getRootTwo() { return rootTwo; }

public void setIsSquareEquation(boolean b) { squareEquation=b; }

public boolean getIsSquareEquation() { return squareEquation; } }

2) 视图

input.jsp:

<%@ page contentType=\

输入一元二次方程的系数:


二次项系数a:

show.jsp:

<%@ page contentType=\<%@ page import=\

一元二次方程的系数是:


二次项系数:


是一元二次方程吗?方程的两个根是:

(3) 控制器

HandleData.java:

package sun.yourservlet; import moon.yourbean.*; import java.io.*; import javax.servlet.*; import javax.servlet.http.*;

public class HandleData extends HttpServlet

{ public void init(ServletConfig config) throws ServletException {super.init(config); }

public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { Equation equ=new Equation(); //创建Javabean对象

request.setAttribute(\将equ存储到HttpServletRequest对象中 double a=Double.parseDouble(request.getParameter(\ double b=Double.parseDouble(request.getParameter(\ double c=Double.parseDouble(request.getParameter(\ equ.setA(a); //将数据存储在equ中 equ.setB(b); equ.setC(c);

22

if(a!=0)

equ.setIsSquareEquation(true); else

equ.setIsSquareEquation(true); double disk=b*b-4*a*c; if(disk<0)

{ equ.setRootOne(\无实根\ equ.setRootTwo(\无实根\ } else

{ double root1=(-b+Math.sqrt(disk))/(2*a), root2=(-b-Math.sqrt(disk))/(2*a); equ.setRootOne(\ equ.setRootTwo(\ }

RequestDispatcher dispatcher=request.getRequestDispatcher(\

dispatcher.forward(request,response); //请求show.jsp显示equ中的数据 }

public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { doPost(request,response); } }

23

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4