Using a User Defined Method to Find Powers
Introduction.
Currently, we will work through another page that does little more than
find the power of one number raised to another. This will be
relatively simple. To make it somewhat more sophisticated we will
also use try and catch blocks to error trap for input errors. You should call this JSP page find_power.jsp. |
<html> <head> <title>A Sticky Form for Finding Powers</title> </head> <body bgcolor = "005533" text="cccccc"> <% String str_base = request.getParameter("txt_base"); if (str_base == null) str_base = ""; String str_power = request.getParameter("txt_power"); if (str_power == null) str_power = ""; String str_base_output = str_base; String str_power_output = str_power; String str_result = request.getParameter("txt_result"); if (str_result == null) str_result = ""; long base = 0; long power = 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, but you should see how some of our earlier programs react to bad inputs. |