没有计算槽html> servlet> bean和bean> jsp

问题描述 投票:0回答:2

我在带有Tomcat Apache的Eclipse中具有DynamicWebApp。

[我的任务:该bean由2个Integer类型的操作数组成,并且算术运算的加法运算符以'+'表示,减法运算符以'-'表示,而乘法运算以'MUL'表示。 Bean返回Integer类型的计算结果。然后实现以下两种情况:

场景1:实现一个合适的Java Servlet,该Java Servlet调用Java Bean。您可以使用自己的HTML页面调用servlet。方案2:实现一个合适的Java服务器页面,该页面使用JSP标准操作重用Java Bean。然后从JSP页面调用您的bean。

html打开,我可以计算。提交后没有打开任何内容,但我可以在浏览器URL中看到我写的内容。并且如果我启动jsp而不是得到:在类型为(bean.CalculatorBean)的bean中找不到有关属性(firstNum)的任何信息。

我的html代码叫做:[[index.html

<!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Hallo Welt</title> </head> <body> <form action="HalloServlet" method="get" style="text-align: center"> <table border="1" width="25%"> <tr style="text-align: center"> <td colspan="2">Rechner</td> <td></td> </tr> <tr> <td>Zahl 1</td> <td><input type="text" name="firstNum"></td> </tr> <tr> <td>Operator</td> <td><select name="operator"> <option value="+">+</option> <option value="-">-</option> <option value="*">MUL</option> </select></td> </tr> <tr> <td>Zahl 2</td> <td><input type="text" name="secondNum"></td> </tr> <tr> <td colspan="2"><input type="submit" value="="></td> </tr> </table> </form> </body> </html>
被称为servlet:

HalloServlet.java

package calculator; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import bean.CalculatorBean; @WebServlet("/HalloServlet") public class HalloServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); // Get Parameter from index.html int operand1 = Integer.parseInt(request.getParameter("firstNum")); int operand2 = Integer.parseInt(request.getParameter("secondNum")); char operator1 = request.getParameter("operator").charAt(0); // Parameter from HalloServlet.java to CalculatorBean CalculatorBean.setFirstNum(operand1); CalculatorBean.setSecondNum(operand2); CalculatorBean.setOperator(operator1); // Parameter from CalculatorBean to HalloServlet.java CalculatorBean.getFirstNum(); CalculatorBean.getSecondNum(); CalculatorBean.getOperator(); int i = Integer.parseInt(request.getParameter("result")); PrintWriter out = response.getWriter(); out.println(i); out.flush(); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); } }
bean称为:

CalculatorBean.java

package bean; public class CalculatorBean { private int firstNum; private int secondNum; private char operator = '+'; private int result; public static void getFirstNum() { } public static void setFirstNum(int firstNum) { } public static void getSecondNum() { } public static void setSecondNum(int secondNum) { } public static void getOperator() { } public static void setOperator(char operator) { } public int getResult() { return result; } public void setResult(int result) { this.result = result; } public void calculate() { switch (this.operator) { case '+': { this.result = this.firstNum + this.secondNum; break; } case '-': { this.result = this.firstNum - this.secondNum; break; } case '*': { this.result = this.firstNum * this.secondNum; break; } } } }
jsp称为:

HalloJSP.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <body> <jsp:useBean id="CalculatorBean" class="bean.CalculatorBean"> </jsp:useBean> <jsp:setProperty name="CalculatorBean" property="*" /> <% CalculatorBean.calculate(); %> <br /> <hr> <br /> Ergebnis: <jsp:getProperty name="CalculatorBean" property="firstNum" /> <jsp:getProperty name="CalculatorBean" property="operator" /> <jsp:getProperty name="CalculatorBean" property="secondNum" /> = <jsp:getProperty name="CalculatorBean" property="result" /> <br /> <hr> <br /> <form action="HalloJSP.jsp" method="post" style="text-align: center"> <table border="1" width="25%"> <tr style="text-align: center"> <td colspan="2">Rechner</td> <td></td> </tr> <tr> <td>Zahl 1</td> <td><input type="text" name="firstNum"></td> </tr> <tr> <td>Operator</td> <td><select name="operator"> <option value="+">+</option> <option value="-">-</option> <option value="*">MUL</option> </select></td> </tr> <tr> <td>Zahl 2</td> <td><input type="text" name="secondNum"></td> </tr> <tr> <td colspan="2"><input type="submit" value="="></td> </tr> </table> </form> </body> </html>

web.xml:

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0"> <display-name>hallowelt</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
我在带有Tomcat Apache的Eclipse中有一个DynamicWebApp。我的任务:bean由2个Integer类型的操作数组成,算术运算的文本形式为'+','-'和...
java eclipse jsp servlets javabeans
2个回答
-1
投票
int i = Integer.parseInt(request.getParameter(“ result”));

-2
投票
您应该注意到您的实现包含空方法?
© www.soinside.com 2019 - 2024. All rights reserved.