上一节课我们通过一个案例三种实现,教你理解Jsp、javabean、Servlet(精),今天我们仍然通过同一个案例求圆的面积,使用MVC:jsp+javabean+servlet来实现,深入理解MVC及Jsp、javabean、Servlet的分工和使用。
r5.jsp
<form action="servletCircle" Method="post" > 输入圆的半径:<Input type=text name="r"> <Input type=submit value="提交"> </form>
JavaBean :NewCircle.java
编写求圆面积的JavaBean
package com.bean; public class NewCircle { double r,circleArea; public double getR() { return r; } public void setR(double r) { this.r = r; } public double getCircleArea() { circleArea=Math.PI*r*r; return circleArea ;} }
servlet:servletCircle.java
package com.servlet; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import com.bean.NewCircle; public class servletCircle extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); double r=Double.parseDouble(request.getParameter("r")); NewCircle circle=new NewCircle(); circle.setR(r); out.print(circle.getCircleArea()); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); }
学习更多网页制作、编程开发的知识,请加入网页设计交流群:208047327
125jz网原创文章。发布者:江山如画,转载请注明出处:http://www.125jz.com/1396.html