javaservletsjsp-tagsservletconfig

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.


Solution

  • You are setting attribute in one servlet, and trying to getParameter in another servlet.

    setAttribute value can not be obtained by getParameter but
    setAttribute value can only be obtained by getAttribute

    So use getAttribute from request as

    request.getAttribute ("totalBill") ;
    

    In your case request.getParameter("totalBill") Is NULL and Float.parseFloat(null) will through NullPointerException and hence you got HttpResponse status 500 i.e, Internl Server Error.