【发布时间】:2019-11-04 15:24:14
【问题描述】:
我在 Eclipse 中有一个 DynamicWebApp 和 Tomcat Apache。
我的任务:该 bean 由 2 个整数类型的操作数和作为文本的算术运算“+”加法、“-”减法和“MUL”乘法运算组成。 bean返回Integer类型的计算结果。然后实现以下两个场景:
场景 1:实现一个调用 Java Bean 的合适的 Java Servlet。您使用自己的 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>
【问题讨论】:
-
Apache Tomcat 的哪个版本?你试过什么了?是否包括打开 Web 浏览器控制台并检查网络面板是否有错误代码和来自服务器的响应(应该有)?
-
我在 apache 中有 9.0 版。实际上,jsp 之前调用了 bean 并完成了我的账单。但我认为在编写 servlet 后问题就开始了。我不确定我是否编码错误或有连接问题。我只得到:服务于:/hallowelt 但不是结果。我只有 500 次失败
标签: java eclipse jsp servlets javabeans