显示方法的内容作为对 doPost 的响应

Display contents of method as response to doPost

美好的一天。我目前正在 servlet 中创建一个简单的工资计算器程序,它将显示用户的 GrossPay、WithholdingTax 和 NetPay。计算器的计算是正确的。我的问题是,在用户输入所需数据后,用户的工资明细显示在 Eclipse 的控制台中,而不是在 Web 浏览器中。我刚开始学习 servlet,非常感谢您的帮助。代码如下。

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    response.setContentType("text/html");

    PrintWriter out = response.getWriter();

    double hrsWorked1 = 0;
    double ratePHour1 = 0;


    if ((request.getParameter("hrsWorked") != null)||(request.getParameter("ratePHour") != null)) {
        hrsWorked1 = Double.parseDouble(request.getParameter("hrsWorked"));
        ratePHour1 = Double.parseDouble(request.getParameter("ratePHour"));

        if((hrsWorked1 < 0)||(ratePHour1 < 0)){
            out.print("<h1>Invalid amount - must be positive.</h1>");
            out.print("<h3>Click <a href='index.html'>here</a> to try again.</h3>");
        }
    }
    computeSalary(hrsWorked1, ratePHour1);
    out.close();     
}

private void computeSalary(double hrsWorked , double ratePHour) {
        double grosspay = hrsWorked * ratePHour;
        double withholdingTax; 
        double sss = 1500.00;
        double hdmf = 1000.00;
        double deductions; 
        double netpay; 

    if(grosspay <= 10000){
        withholdingTax = grosspay * .08;
        deductions = withholdingTax + hdmf + sss;
        netpay = grosspay - deductions;
        System.out.println("<h4>Gross Pay (PHP): </h4>" + grosspay );
        System.out.println("<h4>Withholding (PHP): </h4>" + withholdingTax );
        System.out.println("<h4>SSS (PHP): </h4>" + sss );
        System.out.println("<h4>HDMF (PHP): </h4>" + hdmf );
        System.out.println("<h4>Net Pay (PHP): </h4>" + netpay );
    }
    else if((grosspay >= 10001)&&(grosspay <= 15000)){
        withholdingTax = grosspay * .10;
        deductions = withholdingTax + hdmf + sss;
        netpay = grosspay - deductions;
        System.out.println("<h4>Gross Pay (PHP): </h4>" + grosspay );
        System.out.println("<h4>Withholding (PHP): </h4>" + withholdingTax );
        System.out.println("<h4>SSS (PHP): </h4>" + sss );
        System.out.println("<h4>HDMF (PHP): </h4>" + hdmf );
        System.out.println("<h4>Net Pay (PHP): </h4>" + netpay );
    }
    else if((grosspay >= 15001)&&(grosspay <= 25000)){
        withholdingTax = grosspay * .14;
        deductions = withholdingTax + hdmf + sss;
        netpay = grosspay - deductions;
        System.out.println("<h4>Gross Pay (PHP): </h4>" + grosspay );
        System.out.println("<h4>Withholding (PHP): </h4>" + withholdingTax );
        System.out.println("<h4>SSS (PHP): </h4>" + sss );
        System.out.println("<h4>HDMF (PHP): </h4>" + hdmf );
        System.out.println("<h4>Net Pay (PHP): </h4>" + netpay );
    }
    else if((grosspay >= 25001)&&(grosspay < 35000)){
        withholdingTax = grosspay * .18;
        deductions = withholdingTax + hdmf + sss;
        netpay = grosspay - deductions;
        System.out.println("<h4>Gross Pay (PHP): </h4>" + grosspay );
        System.out.println("<h4>Withholding (PHP): </h4>" + withholdingTax );
        System.out.println("<h4>SSS (PHP): </h4>" + sss );
        System.out.println("<h4>HDMF (PHP): </h4>" + hdmf );
        System.out.println("<h4>Net Pay (PHP): </h4>" + netpay );
    }
    else{
        withholdingTax = grosspay * .25;
        deductions = withholdingTax + hdmf + sss;
        netpay = grosspay - deductions;
        System.out.println("<h4>Gross Pay (PHP): </h4>" + grosspay );
        System.out.println("<h4>Withholding (PHP): </h4>" + withholdingTax );
        System.out.println("<h4>SSS (PHP): </h4>" + sss );
        System.out.println("<h4>HDMF (PHP): </h4>" + hdmf );
        System.out.println("<h4>Net Pay (PHP): </h4>" + netpay );
    }
}


}

问题是您的 computeSalary() 方法正在写入 System.out 而不是您从响应中获得的变量 out 。

您需要更改 computeSalary 方法以像这样接收您的响应 PrintWriter:

private void computeSalary(double hrsWorked , double ratePHour, PrintWriter out)

然后你需要像这样使用它而不是 System.out:

out.println("<h4>Gross Pay (PHP): </h4>" + grosspay );