请用Servlet加jsp文件完整实现复数加减乘除四则运算

时间: 2024-02-23 13:00:51 浏览: 19
好的,以下是一个简单的实现: 1. 编写一个Servlet来处理复数运算: ```java @WebServlet("/complex") public class ComplexServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String op = request.getParameter("op"); Complex c1 = new Complex(Double.parseDouble(request.getParameter("real1")), Double.parseDouble(request.getParameter("imag1"))); Complex c2 = new Complex(Double.parseDouble(request.getParameter("real2")), Double.parseDouble(request.getParameter("imag2"))); Complex result = null; switch (op) { case "add": result = Complex.add(c1, c2); break; case "subtract": result = Complex.subtract(c1, c2); break; case "multiply": result = Complex.multiply(c1, c2); break; case "divide": result = Complex.divide(c1, c2); break; default: break; } request.setAttribute("result", result); request.getRequestDispatcher("/complex.jsp").forward(request, response); } } ``` 2. 编写一个Complex类来表示复数: ```java public class Complex { private double real; private double imag; public Complex(double real, double imag) { this.real = real; this.imag = imag; } public static Complex add(Complex c1, Complex c2) { return new Complex(c1.real + c2.real, c1.imag + c2.imag); } public static Complex subtract(Complex c1, Complex c2) { return new Complex(c1.real - c2.real, c1.imag - c2.imag); } public static Complex multiply(Complex c1, Complex c2) { double real = c1.real * c2.real - c1.imag * c2.imag; double imag = c1.real * c2.imag + c1.imag * c2.real; return new Complex(real, imag); } public static Complex divide(Complex c1, Complex c2) { double denominator = c2.real * c2.real + c2.imag * c2.imag; double real = (c1.real * c2.real + c1.imag * c2.imag) / denominator; double imag = (c1.imag * c2.real - c1.real * c2.imag) / denominator; return new Complex(real, imag); } @Override public String toString() { return real + (imag >= 0 ? "+" : "-") + Math.abs(imag) + "i"; } } ``` 3. 创建一个jsp页面来展示结果: ```jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Complex Calculator</title> </head> <body> <h1>Complex Calculator</h1> <form action="${pageContext.request.contextPath}/complex" method="post"> <input type="hidden" name="op" value="${param.op}"> <label>Complex Number 1:</label> <input type="text" name="real1" value="${param.real1}"> <input type="text" name="imag1" value="${param.imag1}">i <br> <label>Complex Number 2:</label> <input type="text" name="real2" value="${param.real2}"> <input type="text" name="imag2" value="${param.imag2}">i <br> <button type="submit" name="op" value="add">+</button> <button type="submit" name="op" value="subtract">-</button> <button type="submit" name="op" value="multiply">*</button> <button type="submit" name="op" value="divide">/</button> </form> <br> <h2>Result:</h2> <% if (request.getAttribute("result") != null) { %> <p>${request.getAttribute("result")}</p> <% } %> </body> </html> ``` 现在,您可以在浏览器中访问该Servlet并进行复数运算了。

相关推荐

最新推荐

recommend-type

servlet+jsp实现过滤器 防止用户未登录访问

一种是在jsp页面进行session的判断,如果不存在该用户的session,就跳转到登录页面,否则执行jsp页面代码,但是你会发现这样做逻辑也简单,但是非常麻烦,如果有很多个jsp,那么就要写多个判断。 另一种则是利用过滤...
recommend-type

jsp+servlet实现文件下载

jsp+servlet实现文件下载。文档中有demo代码,通过配置web.xml,编写servlet类和前台jsp代码,实现下载图片等功能
recommend-type

如何将JSP/Servlet项目转换为Spring Boot项目

主要介绍了如何将JSP/Servlet项目转换为Spring Boot项目,帮助大家更好的利用springboot进行网络编程,感兴趣的朋友可以了解下
recommend-type

mysql分页 jsp实现 servlet+javabean+jsp三级架构

自己根据所作的一个项目对mysql分页的总结,菜鸟还有理解不到位的地方还请老鸟指教,因为自己纯手工制作所以分值高点犒劳一下自己。网上也有很多类似的东西,我想应该和我的不同
recommend-type

使用JSP+SERVLET+JDBC实现对数据库的增删改查

使用JSP+SERVLET+JDBC实现对数据库的增删改查使用JSP+SERVLET+JDBC实现对数据库的增删改查
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。