Using Java Help Class Files for Java Web Applications 2 – Develop Java Web Applications to Access Databases

I. The executeQuery() method is executed to perform the query, and the returned result is assigned to the ResultSet object rs.
J. A while loop is used to pick up any possible matching username and password. In fact, only one row is returned, and thus this loop can run only one time. The getString() method is used to pick up the queried username and password. A point to be noted is that the arguments of this method, user _ name and pass _ word, both are column names in the LogIn Table in our database CSE _ DEPT, and they are different from those member data variables declared at the beginning of this class, even though they have the same names. The retuned username and password are assigned to two local variables, c _ uname and c _ pword, respectively.

FIGURE 8.8   The modified Login.html file (now it is index.jsp).

K. If a matching username and password pair is found, they are assigned to the two member data variables user _ name and pass _ word and return a “Matched” string to indi-cate that the checkLogIn() method is successful and matching results were found.

L. Otherwise, an “UnMatched” string is returned to indicate that no matching login infor-mation can be found.

Now let’s do a little modification to our Login.html file and break this file into two JSP files: index.jsp and LogInQuery.jsp. The reason for us to make it into two JSP files is that we want to process and display data in two separate files to make it clear and easy. Generally, index.jsp can be considered a starting or home page as a Web application runs. Figure 8.8 shows the modi-fied code for our original Login.html file that will be renamed to index.jsp, and the modified parts are in bold.

Let’s have a closer look at this piece of modified code to see how it works.

A. The first modification is that a Form tag is added to this page with a POST method and an action attribute. Generally, a Form tag is used to create an HTML form to collect user information and send all that collected information to the server when a submit button on this Form is clicked. Therefore, a Form and all submit buttons on that Form have a coordinating relationship. If a button is defined as a submit button by its type attribute, all Form data

FIGURE 8.9   The code for the LogInQuery.jsp page.

will be sent to the server whose URL is defined in the action attribute on the Form tag when this button is clicked by the user. Here we use a JavaServer Page, .\LogInQuery. jsp, as the URL for our target page. That is, this target page is used to access our Java help class file to handle all JDBC- and data-related processing and business logic. The .\ symbol is used to indicate that our JSP file is located in the relatively current folder, since this page is a part of the server functions and will be run on the server side when the whole project runs.

B. The second modification is to change the type of our Cancel button from submit to button and add one more attribute, onclick, for this button. The reason for us to do this is that we want to close our Login.jsp page when the Cancel button is clicked when the project runs, but we do not want to forward this button-click event to the server to allow the server to do a close action. Therefore, we have to change the type of the button to but-ton (not submit) to avoid triggering the action attribute in the Form tag. We also need to add a self.close() method to the onclick attribute of the button to call the system close() method to terminate our application. self means the current page.


C. The Form close tag is also added when the form arrives at its bottom.

Leave a Comment



                    Blog, Contribute ,Privacy,Terms of Use,Trademarks,© estherknow 2024