Books
in black and white
Main menu
Share a book About us Home
Books
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics
Ads

More Java Pitfalls Share Reactor - Daconta M,C.

Daconta M,C. More Java Pitfalls Share Reactor - Wiley publishing, 2003. - 476 p.
ISBN: 0-471-23751-5
Download (direct link): morejavapitfallssharereactor2003.pdf
Previous << 1 .. 73 74 75 76 77 78 < 79 > 80 81 82 83 84 85 .. 166 >> Next

Along comes JSP, which allows the developer to invert his or her servlet and embed the logic into the presentation code. This proves very helpful to script writers who suffer from limited-capability scripting languages and convoluted programming structures (like conditional loops and iterators). Furthermore, a JSP is compiled into a servlet, so you receive the benefits of compiled code in a script-driven programming environment.
Listing 24.2 is an example JSP implementation of the previous salary servlet.
01 <%@ page import= java.util.*"%>
02 <%@ page import= java.sql.*"%>
03 <HTML>
04 <HEAD>
05 <TITLE>
06 Salary Jsp
Listing 24.2 BadSalaryJsp.jsp
JSP Design Errors 211
07 </TITLE>
08 </HEAD>
09 <BODY>
10 <H1>
11 Here are the people who make over $<%= 2
request.getParameter("salary") %>:
12 <%
13 // Database config information
14 String driver = "oracle.jdbc.driver.OracleDriver";
15 String url = "jdbc:oracle:thin:@joemama:1521:ORACLE";
16 String username = "scott";
17 String password = "tiger";
18
19 String salary = request.getParameter("salary");
20
21 // Establish connection to database
22 try {
23 Class.forName(driver);
24 Connection connection =
25 DriverManager.getConnection(url, username, password);
26
27 PreparedStatement statement 2
28 = connection.prepareStatement("SELECT ename FROM emp
WHERE sal > ?");
29
30 statement.setFloat(1, Float.parseFloat(salary));
31
32 ResultSet results =
33 statement.executeQuery();
34 %>
35 </H1>
36 <table>
37 <tr>
38 <td><b>Employee</b></td>
39 </tr>
40
41 <% while (results.next()) { %>
42 <tr>
43 <td>
44 <%= results.getString(1) %>
45 </td>
46 </tr>
47 </table>
48 <% }
49
50 } catch(ClassNotFoundException cnfe) {
51 System.err.println("Error loading driver: " + cnfe);
52
53 } catch(SQLException sqle) {
Listing 24.2 (continued)
212 Item 24
54 sqle.printStackTrace();
55 }
56
57 %>
58 </BODY>
59 </HTML>
60
Listing 24.2 (continued)
This approach looks a lot like the servlet pulled inside out. In fact, to demonstrate how close this is to reality, Listing 24.3 shows a snippet of the source generated by Apache Tomcat to compile the JSP.
01: // HTML // begin
[file= "/BadSalaryJsp.jsp";from=(0,31);to=(1,0)]
02: out.write("\r\n");
03:
04: // end
05: // HTML // begin 2
[file= "/BadSalaryJsp.jsp";from=(1,30);to=(10,35)]
06: out.write("\r\n<HTML>\r\n<HEAD>\r\n<TITLE>\r\nSalary2
Jsp\r\n</TITLE>\r\n</HEAD>\r\n<BODY>\r\n<H1>\r\nHere are the people who 2
make over $");
07:
08: // end
09: // begin 2
[file= "/BadSalaryJsp.jsp";from=(10,38);to=(10,70)]
10: out.print( request.getParameter("salary") );
11: // end
12: // HTML // begin 2
[file= "/BadSalaryJsp.jsp";from=(10,72);to=(11,0)]
13: out.write(":\r\n");
14:
15: // end
16: // begin 2
[file= "/BadSalaryJsp.jsp";from=(11,2);to=(33,0)]
17:
18: // Database config information
19: String driver = 2
"oracle.jdbc.driver.OracleDriver";
20: String url = 2
"jdbc: E" L C RAC 0 1 2 5 1 a: m a m e 0 j n: 1 h t e: l c a r o
21: String username = "scott";
22: String password = "tiger";
Listing 24.3 Apache Tomcat-generated BadSalaryJsp.jsp
JSP Design Errors 213
23
24 String salary = request.getParameter("salary") ;
25
26 // Establish connection to database
27 try {
28 Class.forName(driver);
29 Connection connection =
30 DriverManager.getConnection(url, username,2
password);
31
32 PreparedStatement statement
33 = connection.prepareStatement("SELECT 2
ename FROM emp WHERE sal > ?");
34
35 statement.setFloat(1, 2
Float.parseFloat(salary));
36
37 ResultSet results =
38 statement.executeQuery();
39 // end
40 // HTML // begin 2
[file="/BadSalaryJsp.jsp";from=(33,2);to=(40,0)]
41
out.write("\r\n</H1>\r\n<table>\r\n<tr>\r\n<td><b>Employee</b></td>\r\n</2
tr>\r\n\r\n");
42
43 // end
44 // begin 2
[file="/BadSalaryJsp.jsp";from=(40,2);to=(40,32)]
45 while (results.next()) {
46 // end
Listing 24.3 (continued)
So what is wrong with that? The user gets the ease of scripting combined with the power of compilation. This seems like the best of both worlds. However, the example JSP shows the most obvious problem. Why do I want to put database configuration information in each of my pages? This is a maintenance nightmare. A fundamental of good software development, particularly object-oriented development, is the separation of concerns. The database is a common piece that should be accessible to numerous JSP pages.
While it is possible to do some of this by creating an independent JSP, this kind of issue—accessing an enterprise resource—screams for a programmatic implementation. The solution to this came almost immediately with the JSP specification: Use JavaBeans— now known as the Model 1 Architecture—to encapsulate your business logic.
214 Item 24
Request ->
Response
?!
:D
Pass/
Retrieve data
Previous << 1 .. 73 74 75 76 77 78 < 79 > 80 81 82 83 84 85 .. 166 >> Next