将浮点值从一个 servlet 传递到另一个 servlet 时的 servlet 问题

servlet problem while passing values of float from one servlet to other servlet

<!-- language: java-->
    package bhatti;
    public class find extends HttpServlet {

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

      response.setContentType("text/html;");
      PrintWriter out = response.getWriter();

      out.println("<html><body>");
      out.println("<feildset>");
      out.println("<form action='display' method='get'>");
      out.println("<tr>");
      out.println("<td>ID :</td><td> <input type='text' name='id' style='width=70px;'></td><tr><br>");
      out.println("<tr><td>Uints: </tr><tr><input type='text' name='uints' style='width=70px;' ></td></tr><br>");
      out.println("<tr><td><input type='submit' value='submit' ></td></tr><br>");
      out.println("</form>");
      out.println("</feildset>");
      out.println("</body></html>");

      int i = Integer.parseInt(request.getParameter("id"));
      int u = Integer.parseInt(request.getParameter("uints"));

      float bill;

      bill = (float)(u * (1.5));

      float s;

      s = (float)(bill * 0.05);

      float totalbill;

      totalbill = bill - s;

      RequestDispatcher requestDispatcher = getServletContext().getRequestDispatcher("display.java");
      requestDispatcher.forward(request, response);

      request.setAttribute("id", i);
      request.setAttribute("uints", u);
      request.setAttribute("totalbill", totalbill);

     }
    }
second servlet
package bhatti;

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 javax.servlet.http.HttpSession;

public class display extends HttpServlet {

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

  response.setContentType("text/html;");
  PrintWriter out = response.getWriter();

  int i = Integer.parseInt(request.getParameter("id"));

  int u = Integer.parseInt(request.getParameter("uints"));

  float tb = Float.parseFloat(request.getParameter("totalbill"));

  out.println("<p>ID: " + i + "  </p><br>");
  out.println("<p>Uints: " + u + "</p><br>");
  out.println("<p>Subsidy: " + "  5%" + "</p><br>");
  out.println("<h1>Total Bill:" + tb + "</h1><br>");

 }

}

float value is not sending to the other servlet

error is HTTP Status 500 -

type Exception report

message

description The server encountered an internal error that prevented it from fulfilling this request.

exception

java.lang.NullPointerException sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1838) sun.misc.FloatingDecimal.parseFloat(FloatingDecimal.java:122) java.lang.Float.parseFloat(Float.java:451) bhatti.display.doGet(display.java:26) javax.servlet.http.HttpServlet.service(HttpServlet.java:622) javax.servlet.http.HttpServlet.service(HttpServlet.java:729) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)

note The full stack trace of the root cause is available in the Apache Tomcat/8.0.27 logs.

您正在一个 servlet 中设置属性,并试图在另一个 servlet 中获取参数。

getParameter获取不到setAttribute值,但是
setAttribute值只能通过getAttribute获取

所以使用请求中的 getAttribute 作为

request.getAttribute ("totalBill") ;

在您的情况下,request.getParameter("totalBill") 为 NULL,并且 Float.parseFloat(null) 将通过 NullPointerException,因此您获得了 HttpResponse 状态 500,即内部服务器错误。