0

I've gone through similar questions on this site and still in doubt with it.

I have a basic calculation on one servlet call add servlet and want to get user input to show the total on the same JSP page via another servlet called view total servlet (a table view).

E.g. addServlet on doGet

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

        int subTotal = 10 + i;

        System.out.println("subtotal is (servlet) = " + subTotal);

        // session mgmt to share data
        request.setAttribute("subTotal", subTotal);

        RequestDispatcher rd = request.getRequestDispatcher("view_total");
        rd.forward(request, response);


The `view total servlet` on doGet

// get input from add servlet
        int subTotal = (int) request.getAttribute("subTotal");
        subTotal = 0 * subTotal;

        String viewTotal = "frontend/view_total.jsp";
        RequestDispatcher rd = request.getRequestDispatcher(viewTotal);
        rd.forward(request, response);

The JSP

            <tr>
                <td>1</td>
                <td>$10</td>
                <td><input type="text" name="add" /></td> `// get user input`
                <td> ${ subTotal } /td> `// how to show total value here`
            </tr>
Stu_Dent
  • 370
  • 1
  • 4
  • 19
  • 1
    Hi , what doesn't work in your current code ? Also , you need to set that `subTotal` value in request attribute and access same at jsp using `EL` i.e `${yourattributename}` or `<%= request.getAttribute("yourattributename")%>` .Also have a look at [this](https://stackoverflow.com/questions/10898393/how-to-access-a-request-attribute-set-by-a-servlet-in-jsp) post . – Swati Oct 26 '20 at 04:02
  • But `int subTotal = (int) request.getAttribute("subTotal");` is NPE even I put ${ subTotal } in the tag. – Stu_Dent Oct 26 '20 at 11:47
  • You have added servlet code of two different servlet or both are same ? Please remove irrelevant code . – Swati Oct 26 '20 at 13:17
  • @Swati - I want to separate into two servlets so I can understand how it works. Besides, can it be done in one servlet, let said only put to `view_total servlet` – Stu_Dent Oct 26 '20 at 13:22

1 Answers1

1

You can check if there is any value in session if yes then get that value and add new with old one and then set new subTotal again else just add the value which is there in i in your session .So your code will somewhat look like below :

String viewTotal = "frontend/view_total.jsp";
int i = Integer.parseInt(request.getParameter("add"));
int subTotal,
newvalue;
//check if there is value in session
if (request.getAttribute("subTotal") != null) {
  //get that
  subTotal = (int) request.getAttribute("subTotal");
  //add new value to subtotal not sure why you add 10..make change according to your requirements
  newvalue = subTotal + i;
  //set it again with new total
  request.setAttribute("subTotal", newvalue);
}
else {
  //set the value which user sends
  request.setAttribute("subTotal", i + 10);
}

RequestDispatcher rd = request.getRequestDispatcher(viewTotal);
rd.forward(request, response);
Swati
  • 28,069
  • 4
  • 21
  • 41
  • Thanks! I somehow got `java.lang.NumberFormatException` on the line where `getParameter("add") `... – Stu_Dent Oct 26 '20 at 14:10
  • 1
    Are you sure `add` is not null . Put that code around `if(request.getParameter("add") != null ){ // get that thing here }` .Also , this is just demo code you need to modify according to your requirement :) – Swati Oct 26 '20 at 14:12
  • Thanks for help! May I ask are you done it all in one servlet or using two servlets to share input data? – Stu_Dent Oct 26 '20 at 14:22
  • I have use only one servlet here has changes need to make in `subTotal` only . – Swati Oct 26 '20 at 14:24
  • Thanks for your help. I'm lost how to get user input and show it on the same JSP page using the dispatcher... – Stu_Dent Oct 26 '20 at 14:30
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/223637/discussion-between-swati-and-stu-dent). – Swati Oct 26 '20 at 14:35