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

我在带有Tomcat Apache的eclipse中有一个DynamicWebApp。

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

场景1:实现一个合适的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,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>
drakeson 回答:没有计算槽html> servlet> bean和bean> jsp

您应该注意到您的实现包含空方法吗?

public static void setFirstNum (int firstNum) {
}

最好创建新的bean!右键单击“ src”或beanpackage。 然后单击新建->类。给bean命名,然后在界面上单击“添加”。在下面的窗口中输入“ Serializable”并添加紫色的可序列化界面...

创建属性。标记这一点,右键单击->源->生成吸气剂和吸气剂!

实施一个空的构造函数,例如CalcBean () {} 在setResult计算器中实现。其他一切都是多余的

servlet

右键单击您的源文件夹或httpservlet包-> new-> servlet。命名它->单击doGet并制作doPost-> Finito ...必须通过pdf-Reader从打开的脚本“ 2 java servlets.pdf”中复制您的内容!转到第23页。 在doGet方法COMPLETE中标记整个流程。将其放入您的servlet中。将您的bean的getter方法替换为:

o.println ("<p> number 1:" + beans.getFirstNummero () ...

在pdf阅读器中打开脚本,因为然后在“插入”脚本副本时正确插入了格式设置(因此,请勿通过浏览器打开pdf,否则在插入时会急于一行)

让我感到惊讶的是,即使您有更多的代码并且几乎所有内容都已完全指定,并且有关bean的错误每个人都应该在这里注意到,许多人还是回答了很多。

将来,在Eclipse中工作时会变得更加好奇……随处可见,右键单击并查看Eclipse可以做什么。 Via插件的使用范围更广,但与此同时,与Android Studio相比,Eclipse更好。 android studio更适合android代码

本文链接:https://www.f2er.com/3165636.html

大家都在问