设计一个Web应用。用户可以通过JSP页面输入三角形的三边或梯形的上底、下底和高给一个servlet控制器,控制器负责计算三角形和梯形的面积,并将结果存储到数据模型中,然后请求JSP页面显示数据模型中的数据。(注意用MVC设计模式实现)

环境

Windows11
Tomcat10.0
JDK1.8.0_321
eclipes 2022-03 (4.23.0)

效果

计算梯形
计算梯形

代码

Model:

Compute_Bean.java

package com.example;

public class Compute_Bean {
	private double number1 = 0;
	private double number2 = 0;
	private double number3 = 0;
	private double result;
	private String name = "null";
	private String number1_name = "null";
	private String number2_name = "null";
	private String number3_name = "null";
	
	public void setNumber1(double newNumber1) {
		number1 = newNumber1;
	}
	public double getNumber1() {
		return number1;
	}
	
	
	public void setNumber2(double newNumber2) {
		number2 = newNumber2;
	}
	public double getNumber2() {
		return number2;
	}
	
	
	public void setNumber3(double newNumber3) {
		number3 = newNumber3;
	}
	public double getNumber3() {
		return number3;
	}
	
	
	public void setResult(double newResult) {
		result = newResult;
	}
	public double getResult() {
		return result;
	}
	
	public void setName(String newName) {
		name = newName;
	}
	public String getName() {
		return name;
	}
	
	public void setNumber1_name(String newNumber1_name) {
		number1_name = newNumber1_name;
	}
	public String getNumber1_name() {
		return number1_name;
	}
	
	public void setNumber2_name(String newNumber2_name) {
		number2_name = newNumber2_name;
	}
	public String getNumber2_name() {
		return number2_name;
	}
	
	public void setNumber3_name(String newNumber3_name) {
		number3_name = newNumber3_name;
	}
	public String getNumber3_name() {
		return number3_name;
	}

}

View:

login.jsp

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN" "http://www.w3.org/TR/html4/strict.dtd">
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>求 三角形/梯形 面积</title>
</head>
<body>
	<jsp:useBean id="compute_bean" class="com.example.Compute_Bean" scope="request" />

	<h1>求 三角形/梯形 面积</h1>
	<form action="Compute_Servlet" method="post">
		边1/上底:<input type="text" name="number1" value=<jsp:getProperty name="compute_bean" property="number1" />>	<br> 
		边2/下底:<input type="text" name="number2" value=<jsp:getProperty name="compute_bean" property="number2" />> <br> 
		边3/高:	<input type="text" name="number3" value=<jsp:getProperty name="compute_bean" property="number1" />>	<br> 
				<input type="submit" name="submit" value="求三角形面积"><br>
				<input type="submit" name="submit" value="求梯形面积">	<br>
	</form>
	
	
	<table border="1">
	  	<tr>
		    <th><jsp:getProperty name="compute_bean" property="number1_name" /></th>
		    <th><jsp:getProperty name="compute_bean" property="number2_name" /></th>
		    <th><jsp:getProperty name="compute_bean" property="number3_name" /></th>
		    <th><jsp:getProperty name="compute_bean" property="name" /></th>
	  	</tr>
	  	<tr>
		    <td><jsp:getProperty name="compute_bean" property="number1" />	</td>
		    <td><jsp:getProperty name="compute_bean" property="number2" />	</td>
		    <td><jsp:getProperty name="compute_bean" property="number3" />	</td>
		    <td><jsp:getProperty name="compute_bean" property="result" />	</td>
	  	</tr>
	</table>
	
</body>
</html>

Control:

Compute_Servlet.java

package com.example;

import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;

import org.apache.jasper.tagplugins.jstl.core.Out;

/**
 * Servlet implementation class Compute_Servlet
 */
public class Compute_Servlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Compute_Servlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		Compute_Bean compute_bean = new Compute_Bean();
		request.setAttribute("compute_bean", compute_bean);
		double number1 = Double.parseDouble(request.getParameter("number1"));
		double number2 = Double.parseDouble(request.getParameter("number2"));
		double number3 = Double.parseDouble(request.getParameter("number3"));
		String mess = request.getParameter("submit");
		
		
		compute(number1, number2, number3, mess, compute_bean);
		RequestDispatcher dispatcher = request.getRequestDispatcher("login.jsp");
		dispatcher.forward(request, response);
		System.out.print(compute_bean.getResult());
		
		
		
		
		
		
	}

	
	public void compute(double num1, double num2,double num3, String mess, Compute_Bean bean) {
		boolean flag = mess.contains("三角");
		
		bean.setNumber1(num1);
		bean.setNumber2(num2);
		bean.setNumber3(num3);
		
		if(flag) {
			double p = (num1 + num2 + num3) * 0.5;
			double result = Math.sqrt(p * (p-num1) * (p-num2) * (p-num3));
			bean.setResult(result); 
			bean.setName("三角形面积"); 
			bean.setNumber1_name("边1"); 
			bean.setNumber2_name("边2"); 
			bean.setNumber3_name("边3"); 
		}
		else {
			double result = (num1 + num2) * num3 * 0.5;
			bean.setResult(result);
			bean.setName("梯形面积"); 
			bean.setNumber1_name("上底"); 
			bean.setNumber2_name("下底"); 
			bean.setNumber3_name("高");
		}
		
	}
	
	

	

}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://jakarta.ee/xml/ns/jakartaee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="5.0">
  <display-name>test</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.jsp</welcome-file>
    <welcome-file>default.htm</welcome-file>
  </welcome-file-list>
  <servlet>
    <description></description>
    <display-name>Compute_Servlet</display-name>
    <servlet-name>Compute_Servlet</servlet-name>
    <servlet-class>com.example.Compute_Servlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Compute_Servlet</servlet-name>
    <url-pattern>/Compute_Servlet</url-pattern>
  </servlet-mapping>
</web-app>

目录结构

目录结构

不足之处请做出指正,感谢。

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐