JAVA RUNTIME OBJECTS METHOD
8.5 BUILD JAVA WEB PROJECT TO ACCESS SQL SERVER
8.5.2 Build the Selection Page
To handle the users ’ input and direct to the different target pages based on the users ’ input, we still want to use the MVC mode to build this page. We can use the Selection.
jsp page as a view to display the input and output, and create another JSP page
Figure 8.71. The displayed LogIn.jsp page.
SelectionProcess.jsp as the model and controller to process the users ’ input and direct to the target page.
Of course, you can combine the MVC mode together to perform displaying and processing page at a single JSP page fi le. However, you need to add a hidden fi eld to the page and use that hidden fi eld as an identifi er to indicate whether the page has been submitted or not. That will make the Selection.jsp page complex in the coding process.
We divide this page - building process into two steps: modify the Selection.jsp page and create the SelectionProcess.jsp page.
Let ’ s fi rst perform some necessary modifi cations to the Selection.jsp page.
Launch the NetBeans IDE and open the Selection.jsp page by double clicking on it from the Projects window, and perform the modifi cations shown in Figure 8.73 to this page. All modifi cations have been highlighted in bold.
Let ’ s have a closer look at these modifi cations to see how they work.
A. An action attribute is added to the Form tag and the destination of this action is the
SelectionProcess.jsp page. The ′ .\ ′ operator is used to indicate to the Web controller that the next page, SelectionProcess.jsp , is located at the current folder.
Figure 8.72. The successful page — Selection.jsp page.
Figure 8.73. The coding modifi cations to the Selection.jsp page.
………
<div style='position:absolute;width:10.-2040in;height:2.047in'>
<![if !pub]>
<form method=post action=".\SelectionProcess.jsp">
………
<input type=submit value=OK v:shapes="_x0000_s1028">
………
<input type=button value=Exit onclick="self.close()" v:shapes="_x0000_s1029">
<![if !pub]></span><![endif]><![if !pub]>
</form>
………
A
B
c08.indd 638
c08.indd 638 7/20/2011 11:12:30 AM7/20/2011 11:12:30 AM
www.traintelco.com
B. The type of the second button, Exit , is changed from the submit to the button since we do not want to submit any form data to the next page when this button is clicked. Instead, we want a system method, self.close() , to be executed as this button is clicked to exit our project. Therefore an onclick attribute is used to direct the control to this method when this button is clicked.
Now, let ’ s create the selection process page SelectionProcess.jsp .
Open our project JavaWebDBJSPSQL from the Projects window. Perform the fol- lowing operations to create this page:
1. Right click on our project JavaWebDBJSPSQL from the Projects window and select the
New > JSP item from the pop - up menu. If you cannot fi nd the JSP item from the pop - up menu, go to the Other item to open the New File wizard. Select the Web from the Categories list and JSP from the File Types list to do this.
2. On the opened New JSP File wizard, enter SelectionProcess into the File Name fi eld and click on the Finish button.
Now let ’ s do the coding for this page. Double click on our newly created page
SelectionProcess.jsp from the Projects window to open its code window. On the opened code window, perform the modifi cations shown in Figure 8.74 to this page. All modifi ca- tion parts have been highlighted in bold.
Let ’ s have a closer look at this piece of codes to see how it works.
A. A JSP directive tag is used to indicate that this page uses the Java language and it is a JSP fi le.
B. A local string variable nextPage is declared fi rst. This variable is used to hold the URL of the next page, which we will use later to direct the control to the associated page.
C. The getParameter() method is used to pick up the selected item by the user from the selection list in the Selection.jsp page. The argument of the getParameter() method is the name of the selection list in the Selection.jsp page. The selected item is then assigned to another local string variable userSel .
Figure 8.74. The codes for the SelectionProcess.jsp page.
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Selection Process Page</title>
</head>
<body>
<%@page language="java" %>
<%
String nextPage = null;
String userSel = request.getParameter("ListSelection");
if (userSel.equals("Faculty Information")) nextPage = "Faculty.jsp";
else if (userSel.equals("Course Information")) nextPage = "Course.jsp";
else
nextPage = "Student.jsp";
%>
<jsp:forward page = "<%=nextPage%>" />
</body>
</html>
A B C D
E
D. An if selection structure is used to check the user ’ s selection and assign the associated next page to the local variable nextPage .
E. Finally, a JSP forward directive is used to direct the program to the next page.
Now we can build and run this page to test its function.
Click on the Clean and Build Main Project button to compile and build our project.
Then right click on the Selection.jsp page from the Projects window and select the Run
File item from the pop - up menu to run the project. The Selection.jsp is displayed, as shown in Figure 8.75 , when the project runs.
Select a desired item, such as Faculty Information , from the Selection listbox, and click on the OK button. You can fi nd that the Faculty.jsp page is displayed. You can try to select other item from the listbox to open other related pages.
Click on the Exit button to terminate our project.
Our Selection page is successful!