Issue
I am a complete beginner to java web applications and I'm trying to design a page to show the total pay for an ArrayList. I have a method in my Service class that returns a double from an ArrayList:
public double totalPayroll(String name) {
double total = 0;
for (int i=0; i < payrollList.size(); i++) {
total += payrollList.get(i).calWage();
}
return total;
}
On my servlet, I want to add a request to the doGet method in a Servlet for this method so that I can display the total on my jsp file. On the Servlet file, I have tried:
request.setAttribute("productList", service.totalPayroll());
and tried the following for the jsp file:
"<h3>Total Payroll to Pay: </h3><c:out value="$${payroll.totalPayroll()}" />"
but I am getting an error that method is not applicable for the argument. It seems that I need to set the attribute to match the return value of the method but I cannot figure out how to do that on the Servlet and how to call it on the jsp file. I appreciate any help or guidance on how to solve this issue. Thank you!
Solution
Well Question the question isn't clear but still i would like give an solution based on my understanding of question
Try This:
On Servlet
double totalPayroll=00;
totalPayroll=service.totalPayroll("parameterValue"); //You have to send parameter/argument as your method defination requires 1 parameter/argument
request.setAttribute("Demo", totalPayroll);
RequestDispatcher rd= request.getRequestDispatcher("yourjspfile.jsp"); //Enter the name of your jsp file
rd.forward(request, response);
on Jsp:
<%@ page isELIgnored = "false" %> //top of the page
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> //top of the page &add jstl jar to your project
Total Payroll to Pay: <c:out value="${Demo}"/> You can add condition for its visiblity.
Answered By - naveen4181