A development case using javabean+jsp in ecplise---the step length between two numbers increases by 1 cumulative sum (run code)

1. Three files need to be created in this case:

1. Numinput.jsp file: form form submission data page
2. Numshow.jsp file: display accumulated data results
3. Add.java: arithmetic sum of integers with a step of 1

Two, directly on the code

1、Numinput.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>提交整数页面</title>
</head>
<body>
	<h2>请按照下列各式提交两个整数</h2>
	<br>
	<form action="Numshow.jsp" method="post">
		开始数据:
		<input name="a">
		<br>
		<br>
		结束数据:
		<input name="b">
		<br>
		<br>
		<input type="submit" value="提交">
	</form>
</body>
</html>

The interface is shown in the figure below:
Insert picture description here

2、Numshow.jsp

<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="com.javabean.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>利用Javabean+jsp求两个整数之间的和</title>
</head>
<body>
	<%request.setCharacterEncoding("UTF-8") ; %>
	<jsp:useBean id="he" class="com.javabean.Add" />
	<jsp:setProperty name="he" property="*"  />
	<p><%=he.getA()%>加到<%=he.getB()%>的和是:<%=he.sum()%></p>
	<p>现在的时间是:<%=new Date()%></p>
</body>
</html>

The interface after accumulation and summation is as follows:
Insert picture description here

3、Add.java

package com.javabean;

public class Add {
    
    
	private int a;
	private int b;

	public Add() {
    
    

	}

	public Add(int a, int b) {
    
    
		super();
		this.a = a;
		this.b = b;
	}

	public int getA() {
    
    
		return a;
	}

	public void setA(int a) {
    
    
		this.a = a;
	}

	public int getB() {
    
    
		return b;
	}

	public void setB(int b) {
    
    
		this.b = b;
	}

	public int sum() {
    
    
		int c, s = 0;
		if (a > b) {
    
    
			c = a;
			a = b;
			b = c;
		}
		int x = a;
		while (x <= b) {
    
    
			s += x;
			++x;
		}
		return s;
	}
}

Note:

1. The attribute value <input>in namethe form tag should be the same as that of the Add.java file变量名一致

There are many methods to reference JavaBean, please refer to the link for details: https://blog.csdn.net/a18792721831/article/details/76541292

2. JavaBean design principles:

(1) JavaBean is a公共类

public class Add {
    
    
	
}

(2) JavaBean has a公共的无参的构造方法

public Add() {
    
    

}

(3)JavaBean所有的属性 均为 私有

private int a;
private int b;

(4) In JavaBean, two methods need to be provided for each attribute:setXxx()和getXxx()

    public int getA() {
    
    
		return a;
	}

	public void setA(int a) {
    
    
		this.a = a;
	}

	public int getB() {
    
    
		return b;
	}

	public void setB(int b) {
    
    
		this.b = b;
	}

(5) When defining a JavaBean, usually put it 命名的包next to the
Insert picture description here
white one. I hope to help more friends~~~

Guess you like

Origin blog.csdn.net/weixin_44727274/article/details/110563489