Using JavaServer Pages Technology for Java Web Applications 2 – Develop Java Web Applications to Access Databases

In fact, JavaServer Pages can handle more complicated jobs, such as business logic, JDBC-related database connections, data processing and JSP switching. Generally, a main or controller JSP takes charge of passing parameters between the server and clients, forwarding the user to the other target JSP or Web pages based on the run result of the Servlet.

The example code shown in Figure 8.6 illustrates how to use JSP to handle multiple jobs, includ-ing parameter collection from the client page, database accessing and data processing and forward-ing from the current page to the target Java Server Pages based on the run results of data processing. Let’s have a closer look at this code to see how it works.

A. The getParameter() method is called to pick up two pieces of login information, user-name and password, which are entered by the user from the client page and assigned to two local variables, uname and pword, in the Servlet.
B. These two pieces of login data are displayed on the client side with the JSP tags.
C. Starting from the JSP tag <%, a piece of Java code is developed. An Oracle JDBC database driver is loaded, and this is a Type IV JDBC driver.

D. The Oracle JDBC URL is assigned to the local variable url.
E. The getConnection() method is executed to establish the database connection.

F. A query string is created and used to query a matching username and password from the LogIn Table.
G. The createStatement() method is called to create a Statement object.
H. The executeQuery() method is executed to perform the query, and the returned result is assigned to the ResultSet object rs.
I. A while loop is used to pick up any possible matching username and password. In fact, only one row is returned, and therefore this loop can run only one time.

J. If a matching username and password pair is found, nextPage is assigned to Selection.jsp.

FIGURE 8.6  A piece of example code.

K. Otherwise, nextPage is assigned to LoginError.jsp.
L. is used to direct the page to an appropriate page based on the match result.

A good point of using this JSP technique to handle a lot of JDBC-related code or business logic in JavaServer Pages is that the Servlet processing speed and efficiency can be improved. However, you may find that, at the same time, a shortcoming also comes with this benefit, which is relatively complex code development. Quite a lot of code for JDBC database access and data processing as well as business logic is involved in JSP and therefore makes it a big mess during code development.

Develop Applications to Access Databases 281

To solve this problem, separate the business logic and JDBC-related database processing from the results displayed in Web pages and make our coding process easy and clear, three possible methods can be used:

1) Using a Java help class to handle all business logic and database-related processing. In this way, we can separate the login process into two different parts: the data display Web page and JDBC-related database processing or business logic to make the process more objec-tive and clear based on its functionality. This Java help class file works just like a bridge or intermediate layer to help JavaServer Pages perform business-related jobs in a separate file to allow the JSP to concentrate on the data display process. You will see that this Java help class file can be translated to a Java Bean later.

2) Using the session implicit object provided by JSP to store and transfer data between clients and server. This method still belongs to the Java help class category. That is, session objects are used in the Java help class to help with data storage and retrieval between clients and clients and between clients and the server.

3) Using Java Beans techniques to cover and handle JDBC-related database access, data pro-cessing and business logic such as data matching and comparison processes. The main role of JavaServer Pages is to provide a view to display the results. JSP can also need to load Java Beans, pass the necessary parameters between a Servlet and clients and forward users to the different target pages based on the run result.

Let’s have a detailed discussion of these methods one by one.

Leave a Comment



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