0

I am currently building a Budget Estimator web application using JSP, Servlets and Apache POI.

I have a servlet file which accepts form inputs from a user and writes budget estimate values into an Excel File based on these inputs after performing some computations. There are 4 different budget categories. I'll be referring to them as A,B,C,D for convenience. The application generates an Excel File which comprises of: 1) Budget summary sheet 2) Separate sheet for each of the 4 categories

Currently, the budget computation code for all these categories is in a single servlet file. I want to split the computation part for these 4 categories into separate servlet files. I want to allow the user to track how much budget is allocated for each category. I am planning to implement this as follows: A button will be placed at the end of each category. When the user clicks that button, a servlet should be called which will perform the budget computations and return the budget estimate for that category which should be displayed on the same webpage (maybe within a text field). Same for categories B,C and D. Eventually, if the user is satisfied with the allocations, he can generate an excel report by clicking on a "Generate report" button. Clicking this button will call the main servlet file will in turn will call 5 different servlets that generate the 5 sheets of the Excel file. The final excel would be generated by binding these 5 sheets together and returned to the user through the main servlet file.

I need advice on how to go about this and efficient ways to accomplish this. Any help is appreciated! Thanks in advance!

  • 3
    why cant you just use java class ,whats the purpose of submitting into 5 servlets. just submit into a single servlet and depending on the user input call different methods .get help from java classes and methods inside the servlet. – Priyamal Apr 17 '16 at 05:54
  • Creating five different servlets is a bad idea (in terms of performance as well as maintainance). Just have your servlet handle the four different categories. If you don't want an entire page to be reloaded you can even use AJAX and display the budget in a text field. – dsp_user Apr 17 '16 at 07:12
  • you still can do it in 1 servlet, pass a parameter with the request to determine which process you want to do, then call the appropriate method in the servlet `doGet()` or `doPost()`, all calculations method should be in a separate class, that you instantiate in the servlet and call a method from the object – Yazan Apr 17 '16 at 07:55

2 Answers2

0

You still can do it in 1 servlet, pass a parameter say calculate with the request to determine which process you want to do, then call the appropriate method in the servlet, all calculations method should be in a separate class BudgetCalculation , that you instantiate and use in the servlet

find below a suggestion:

public class BudgetCalculation {

    public String calculateA(...some params...){

        return result;
    }

    public String calculateB(...some params...){

        return result;
    }

    public String calculateC(...some params...){

        return result;
    }

    public String calculateD(...some params...){

        return result;
    }
}

assume you have a library that generates Excel sheets, ExcelUtil

public class MyServlet extends HttpServlet {

    public void doGet(...request,....){ // this can also be doPost(), use the one your current solution works with
        BudgetCalculation budgetCalculation = new BudgetCalculation();

        if(request.getParameter("calculate").equals("a")){
            return budgetCalculation.calculateA(...params...);
        }else if(request.getParameter("calculate").equals("b")){
            return budgetCalculation.calculateB(...params...);
        }else if(request.getParameter("calculate").equals("c")){
            return budgetCalculation.calculateC(...params...);
        }else if(request.getParameter("calculate").equals("d")){
            return budgetCalculation.calculateD(...params...);
        }else if(request.getParameter("calculate").equals("all")){
            ExcelUtil excelUtil = new ExcelUtil();
            excelUtil.createExcelFile(..params...);
            return ExcelFileUrl;
        }
    }
}

now send a, b, c, d or all with each button click as a value for parameter calculate

Yazan
  • 6,074
  • 1
  • 19
  • 33
  • Thank you for the suggestion. I'm calling the servlet through a form action as follows:
    Presently, clicking the submit button results into the servlet call. How can I pass a parameter to the servlet? I will be having 5 buttons in the form: 1 for each of the four categories which should display the respective current budgets without a page refresh and the 5th one that generates an Excel file. And how do I make all the 5 buttons make a call to the same servlet?
    – Sridhar Narayanan Apr 17 '16 at 13:12
  • you can have a hidden field, named `calculate`, `` and `onClick()` on each button set the hidden value to a,b ,c ... then submit the form, if you set `action` in that form to a servlet, then all calls will go to same servlet. Regarding the no-refresh you need to use Ajax. – Yazan Apr 17 '16 at 13:24
  • Thank you for the advice! I will definitely try this out. – Sridhar Narayanan Apr 17 '16 at 19:46
-1

As others have already pointed out, you should use a class to perform this operation but if you are really looking to call multiple servlts in a sequence, you can use a RequestDispatcher which works by mapping urls to servlet requests (as suggested by BalusC here):

public class Test_ALL extends HttpServlet {
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     response.setContentType("text/plain");
     request.getRequestDispatcher("/Servlet1").include(request, response);
     request.getRequestDispatcher("/Servlet2").include(request, response);
     .....

  }
}
Community
  • 1
  • 1
thepiyush13
  • 1,321
  • 1
  • 8
  • 9
  • 1
    A fat downvote for blatant [plagiarism](http://meta.stackexchange.com/q/160077) from [this answer](http://stackoverflow.com/a/3025000). It's really great that you're good at searching, but please include links to sources where you found the information instead of disrespectfully crediting yourself. Otherwise, just vote for close as duplicate. – BalusC Apr 17 '16 at 08:17
  • sorry I forgot about that , I have included the link – thepiyush13 Apr 17 '16 at 08:20
  • I agree, the only reason I advised sequential calling is because OP mentioned about budget summary , which could only be generated once OP has budget results for all 4 categories. – thepiyush13 Apr 17 '16 at 08:27