简体   繁体   English

使用Servlet的doPost方法

[英]Using a Servlet doPost method

Im new to Java and trying out some financial analysis java file I'm using for my studies. 我是Java新手,请尝试我用于学习的一些财务分析Java文件。 I got it to work when I created a main method within the bean itself. 当我在bean本身中创建一个main方法时,它就可以工作。 However when I try to pass the values needed through a jsp form and using a servlet doPost method to do the calculations by instantiating the methods from the bean nothing happens I get a NullPointerException. 但是,当我尝试通过jsp表单传递所需的值并使用servlet doPost方法通过从Bean实例化方法来进行计算时,什么也没有发生,我得到了NullPointerException。 Im assuming the BlackScholes.java works as a bean should and that the issue is within the code presented. 我假设BlackScholes.java可以像bean一样工作,并且问题出在所提供的代码之内。 Help here would be greatly appreciated. 这里的帮助将不胜感激。

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);
    }
  }
}

The jsp file used to creat the form data: 用于创建表单数据的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>

Potentially one of your action parameters, which you are reading using getParameters is not filled. 您使用getParameters读取的动作参数之一可能未填充。 This could lead to a Null Pointer exception in the subsequent calls where you are parsing. 这可能会导致您解析后的后续调用中出现Null Pointer异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM