首页 > 解决方案 > Validating Null and Empty String in Servlet

问题描述

I am making a JavaServlet calculator application that computes basic operation "+","-","/","*". I finished making it but I don't want to have an error page when the inputs are empty or null.

Code:

JSP:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Calculator</title>
</head>
<body>
<h2>Calculator</h2> <br>
<form action="/index" method="post">
    Input number1: <input type="text" name="input1" value="${calcCompute.input1}"> <br>
    Input number2: <input type="text" name="input2" value="${calcCompute.input2}"> <br>
    <input type="radio"  name="operand" value="Sum"> Sum <br>
    <input type="radio"  name="operand" value="Difference"> Difference <br>
    <input type="radio"  name="operand" value="Product"> Product <br>
    <input type="radio"  name="operand" value="Quotient"> Quotient<br>
    <input type="submit" name="submit" value="Calculate"> <br>
    Answer:${calcCompute.result} ${error}
</form>
</body>
</html>

servlet:

import javax.servlet.RequestDispatcher;
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 java.io.IOException;

@WebServlet(name = "CalculatorServlet",urlPatterns = "/index")
public class CalculatorServlet extends HttpServlet {
    public CalculatorServlet()
    {
        super();
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {

        String input1 = request.getParameter("input1");
        String input2 = request.getParameter("input2");
        String operand = request.getParameter("operand");

        StringBuilder error = new StringBuilder();
        if (input1 == null)
        {
            error.append("input 1 was not provided");
        }
        else if (input2 == null)
        {
            error.append("input 2 was not provided");
        }

        if (error.length() > 0)
        {
            request.setAttribute("error",error.toString());
        }
        else {
            double input_1 = Double.parseDouble(input1);
            double input_2 = Double.parseDouble(input2);
            calcCompute calcCompute = new calcCompute(operand, input_1, input_2);
            request.setAttribute("calcCompute", calcCompute);
        }


        getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);

    }

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

    }
}

JavaBeans:

import java.io.Serializable;



 public class calcCompute implements Serializable
    {
    
        private Double input1;
        private Double input2;
        private Double result;
    
        public calcCompute(String operand, Double input1, Double input2) {
            this.input1 = input1;
            this.input2 = input2;
            switch (operand)
            {
                case "Sum":
                    this.result = input1 + input2;
                    break;
                case "Difference":
                    this.result = input1 - input2;
                    break;
                case "Product":
                    this.result = input1 * input2;
                    break;
                case "Quotient":
                    this.result = input1 / input2;
                    break;
    
            }
        }
        public Double getInput1() {
            return input1;
        }
    
        public void setInput1(Double input1) {
            this.input1 = input1;
        }
    
        public Double getInput2() {
            return input2;
        }
    
        public void setInput2(Double input2) {
            this.input2 = input2;
        }
    
        public Double getResult()
        {
            return result;
        }
    
        public void setResult(Double result) {
            this.result = result;
        }
    }

Error Page: enter image description here I tried using this code snippet as a fix but the error page is still showing up

Fix code:

StringBuilder error = new StringBuilder();
        if (input1 == null)
        {
            error.append("input 1 was not provided");
        }
        else if (input2 == null)
        {
            error.append("input 2 was not provided");
        }

        if (error.length() > 0)
        {
            request.setAttribute("error",error.toString());
        }
        else
            {
            double input_1 = Double.parseDouble(input1);
            double input_2 = Double.parseDouble(input2);
            calcCompute calcCompute = new calcCompute(operand, input_1, input_2);
            request.setAttribute("calcCompute", calcCompute);
        }

标签: jspservletsjavabeans

解决方案


推荐阅读