Create the CourseProcess.jsp Page 2 – Develop Java Web Applications to Access Databases

F. Then the method getCourse() defined in the Java Bean class file CourseQuery. java is called to query all courses, that is, all course _ id values, based on the selected faculty name, fname, obtained from step E.

G. If the getCourse() method is executed successfully, which means that all matching course _ id values have been retrieved and stored in our HashMap object list, c _ course, a session method, setAttribute(), is used to pass the HashMap list from the server side to the client page, Course.jsp, to enable the latter to recognize and use it later. The facultyName variable is also passed to the client page to make sure that it is dis-played correctly in that client page after a new refreshed page is sent back from the server.

H. A system method, getRequestDispatcher(), and the forward() method are used to pass the variables or objects to the client page, Course.jsp. The forward slash indi-cates that the page is located in the current folder.

I. If the Details button is clicked by the user, it means that all course (course _ id) for the selected faculty member have been retrieved and displayed in the CourseList box in our Course.jsp page. When users click a course _ id from that CourseList box, we need to generate code to query details for that selected course _ id and display them in six TextFields in our client page.

J. First we need to pick up the key for the selected course _ id since all course _ id values are stored in a HashMap list, c _ course, with an associated key that is an inte-ger sequence number represented as a string, such as “1”, “2” and so on. Here the system method getParameter() is used for that purpose. A point is that this method can only return a String. But the c _ course is a HashMap object list. The result of executing this method is to return a key value, since it is the first variable in that list.

K. An instance of our HashMap class, selCourse, is generated and assigned with our HashMap object list, c _ course. A session method, getAttribute(), is used to trans-fer the HashMap object from our Java Bean class to this controller page. The class HashMap must be prefixed to this method to convert the object to the HashMap list. This system method is a popular way to transfer objects among JSP pages as long as they are in the same session. L. The get(key) method of the HashMap class is executed to obtain the selected course _ id value related to the key obtained in step J. The obtained course _ id is assigned to
a local String variable, cid, to be used in the next step.


M. If the cid variable contains a valid value, the QueryCourse() method that will be built in our Java Bean class later is called to get all the details for the obtained course _ id.

N. If the execution of the method QueryCourse() fails, a false is returned to indicate this. In that case, we need to cleanup all six TextFields by setting them to null with a for() loop. The system method setAttribute() and the string array C _ Field[] are used for that purpose.

O. Otherwise, the query is successful. The setAttribute() method is used to set up all properties defined in the Java Bean class file with database query results by using the asso-
ciated getter methods in that class.
P. The system method sendRedirect() is issued by the server to direct control back to our client page, Course.jsp, to reflect and display all of the query results on that page.

Q. From step Q to step S, a sequence of getParameter() methods are used to identify which button on the client page has been clicked. A non-null value will be returned if a but-ton, such as Insert, Update or Delete, has been clicked by the user. We will build a related Java bean class to handle these related queries later.
T. If the getParameter(“Back”) method returns a non-null value, it means that the Back button has been clicked by the user in the Course.jsp page and the user wants to return to the Selection.jsp page to perform other data query operations. The CloseDBConnection() method is first executed to close the connection to our sample database, and then the sendRedirect() method is called to do this return function.

Now let’s build our Java Bean class or Java model file, CourseQuery.java, to handle all course data query actions, getter methods, class properties and related business logic.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *