Using a User Defined Method to Find Factorials and Combinations
Introduction.
Now we will work through another page that traps for errors and makes use
of one method within another. It's not a big difference from what
we've done, but it provides another example. You should call this JSP page enumeration.jsp. |
<html> <head> <title>A Sticky Form for Enumerating Subsets</title> </head> <body bgcolor = "003344" text="cccccc"> <% String str_setsize = request.getParameter("txt_setsize"); if (str_setsize == null) str_setsize = ""; String str_subsetsize = request.getParameter("txt_subsetsize"); if (str_subsetsize == null) str_subsetsize = ""; String str_factorial = request.getParameter("txt_factorial"); if (str_factorial == null) str_factorial = ""; String str_combinations = request.getParameter("txt_combinations"); if (str_combinations == null) str_combinations = ""; long setsize = 0; long subsetsize = 0; // checking to see if this form has been submitted if (request.getMethod().equals("POST")) {
}
}
} |
After copying this code and uploading it and accessing it on your web you should see something like the following after leaving one entry blank and putting a non-integer in the other. |
Notice how all of the entries have been blanked by the
code. It is also important that the page doesn't blow up! After entering appropriate numbers we get something like the following. |
The code has nothing particularly clever other than it uses the computeFactorial( ) method within the computeCombinations( ). |