JSP development model and Web Calculator

Source: Internet
Author: User

1, JSP development mode:
1.sun Company introduced JSP Technology, but also recommended two types of Web application development model, one is Jsp+javabean, one is Servlet+jsp+javabean
The 2.jsp+javabean model is suitable for developing Web applications where business logic is less complex, in which the JavaBean is used to encapsulate business data, and the JSP is responsible for processing user requests and displaying data.
The 3.servlet+jsp+javabean (MVC) pattern is suitable for developing complex web applications in which the Servlet is responsible for processing user requests, JSP is responsible for data display, and the JavaBean mode program
The hierarchy between the modules is clear, and web development recommends this model.
2, use mode one to write the calculator

Using Jsp+javabean to write a calculator (Calculator) page, JavaBean has firstnum, secondnum, result, operator properties, and provides a calculate method.

Calculatorbean.java

Package Cn.itcast.domain;import java.math.bigdecimal;//Encapsulate Calculator Data Beanpublic class Calculatorbean {private String firstnum= "0";p rivate char operator= ' + ';p rivate string secondnum= "0";p rivate string Result;public string Getfirstnum () { return firstnum;} public void Setfirstnum (String firstnum) {this.firstnum = Firstnum;} Public Char Getoperator () {return operator;} public void Setoperator (char operator) {this.operator = operator;} Public String Getsecondnum () {return secondnum;} public void Setsecondnum (String secondnum) {this.secondnum = Secondnum;} Public String GetResult () {return result;} public void Setresult (String result) {This.result = result;} public void Calculate () {BigDecimal first=new BigDecimal (this.firstnum); BigDecimal second=new BigDecimal (this.secondnum); switch (this.operator) {case ' + ': {This.result=first.add (second). ToString (); break;} Case '-': {this.result=first.subtract (second). ToString (); Case ' * ': {this.result=first.multiply (second). ToString (); Case '/': {if (Second.doublevAlue () ==0) {throw new RuntimeException ("dividend cannot be 0! ");} This.result=first.divide (SECOND,20,BIGDECIMAL.ROUND_HALF_UP). toString (); break;} Default:throw New RuntimeException ("operator can only be +-*/");}}}

calculatorbean.jsp

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><%string path = Request.getcontextpath () ; String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >

Above

JSP development model and Web Calculator

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.