【问题标题】:Using a Servlet doPost method使用 Servlet doPost 方法
【发布时间】:2013-08-25 19:33:39
【问题描述】:

我是 Java 新手,正在尝试一些我用于学习的财务分析 java 文件。当我在 bean 本身中创建一个 main 方法时,我让它工作了。但是,当我尝试通过 jsp 表单传递所需的值并使用 servlet doPost 方法通过从 bean 实例化方法来进行计算时,没有任何反应,我得到了 NullPointerException。我假设 BlackScholes.java 应该像 bean 一样工作,并且问题出在所提供的代码中。非常感谢您的帮助。

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

    String action = request.getParameter("action");

    if (action != null) {
        try {
            String spotPrice = request.getParameter("sprice");
            String strikePrice = request.getParameter("strike");
            String interestRate = request.getParameter("rate");
            String timeTo = request.getParameter("time");
            String vol = request.getParameter("volatility");

            double sprice = Double.parseDouble(spotPrice);
            double strike = Double.parseDouble(strikePrice);
            double rate = Double.parseDouble(interestRate);
            double time = Double.parseDouble(timeTo);
            double volatility = Double.parseDouble(vol);
            double carryrate = rate;

            request.setAttribute("sprice", sprice);
            request.setAttribute("strike", strike);
            request.setAttribute("rate", rate);
            request.setAttribute("time", time);
            request.setAttribute("volatility", volatility);

            BlackScholes BS = new BlackScholes(carryrate);
            BS.bscholEprice(sprice, strike, volatility, time, rate);
            double currentpriceC = BS.getCalle();
            request.setAttribute("validation1", currentpriceC);
        } catch (NullPointerException e) {
            return;
        }
    } else {
        request.getRequestDispatcher("/index.jsp").forward(request,
                response);
    }
  }
}

用于创建表单数据的jsp文件:

<form action="${pageContext.request.contextPath}/OptionsController"
    method="post">
    <input type="hidden" name="action" value="docalculate" /> 

    Spot Price: <input type="text" name="sprice" /> <br />
    <p />
    Strike Price: <input type="text" name="strike" /> <br />
    <p />
    Risk Free Rate: <input type="text" name="rate" /> <br />
    <p />
    Months To Expire: <input type="text" name="time" /> <br />
    <p />
    Volatility: <input type="text" name="volatility" /> <br />
    <p />

    <input type="submit" value="Submit" /><br />
    <p />
    One call options costs: <input type="text" name="validation1"
        readonly="readonly"
        value="<%if (request.getAttribute("validation1") != null) {
            out.print(request.getAttribute("validation1"));
        }%>">
</form>

【问题讨论】:

  • 在 catch 块中使用 e.printStackTrace() 并将异常详细信息放在这里..
  • 正如@Prabhaker 所说,向我们展示异常详细信息..
  • 通过捕获 NullPointerException 并在 catch 块中返回,就好像什么都没发生一样,你是在踢自己的脚:你隐藏了错误,并且无法找出它发生在哪里。不要隐藏错误。修复它们。修复它的第一件事就是删除这个catch块,让异常发生,然后读取异常堆栈跟踪。

标签: java jsp servlets


【解决方案1】:

您正在使用 getParameters 读取的操作参数之一可能未填充。这可能会在您正在解析的后续调用中导致空指针异常。

【讨论】:

    猜你喜欢
    • 2011-08-18
    • 2012-02-27
    • 2011-02-01
    • 2018-11-19
    • 2013-05-26
    • 2014-09-27
    • 2017-01-05
    • 2014-04-14
    • 1970-01-01
    相关资源
    最近更新 更多