One of my laboratory activities in Java Server Pages or JSP is a simple machine problem that will compute and display the subject grade of a student to the web browser. I started it by giving a machine problem in this case the problem is “Create a JSP program that will accept midterm grade and final grade as input (data type double), then the program will compute the subject grade using the formula: Sgrade = (mgrade + fgrade) / 2.”
The student needs to analyze the machine problem, and then make an algorithm followed by writing a source code, test and debug the program until the output is correct. The algorithm for this machine problem is the program will ask for two inputs and that is the grade for midterm and final, then the program will compute the subject grade using the formula subject grade = midterm grade plus final grade divided by 2 and lastly the program will display or print the subject grade.
We need to create first and interface wherein a user can type the values for midterm grade and final grade, then a button that will be used by the user to trigger an event that will compute and display the subject grade to the web browser. Am HTML code is need because our interface need text boxes for the input and a button. Below is the source code of our interface:
<form action="computegrade.jsp" method="post">
<table border="0" bgcolor="green">
Enter Midterm grade:
<input type="text" name="txtmidterm" size="20">
Enter Final grade:
<input type="text" name="txtfinal" size="20">
<input type="submit" value="Compute">
After the interface have been created, it is now time to create the logic of the program. Java Server Pages or JSP code is needed. Below is the source code of the laboratory activity in JSP that will compute and display the subject grade. You should save the JSP code with file name “computegrade.jsp” because that is the file name declared in the form action in the code above.
String strmidterm= request.getParameter("txtmidterm");
String strfinal= request.getParameter("txtfinal");
double dblmidterm = Double.parseDouble(strmidterm);
double dblfinal = Double.parseDouble(strfinal);
double subgrade = (dblmidterm + dblfinal)/2;
out.println("Subject grade:" + subgrade);
The logic of the JSP code is to get the input from form enter by user, the midterm grade and final grade. The code request.getParameter is used to get the input from form then store it to String variables strmidterm and strfinal. The code Double.parseDouble is used to convert string values to data type double then assign it to the double variables the dblmidterm and dblfinal. The formula to compute the subject grade is subgrade = (dblmidterm + dblfinal)/2 and finally, the program will display or print the subject grade to the web browser using the out.println("Subject grade:" + subgrade) command.