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 .. 74 75 76 77 78 79 < 80 > 81 82 83 84 85 86 .. 166 >> Next

?!
:D
Interact with other systems as
necessary to handle business logic
Java Server Page Figure 24.1 Model 1 Architecture.
Business Logic (JavaBean)
JSP Development with Beans (Model 1 Architecture)
Figure 24.1 shows the JSP Model 1 Architecture.
The architecture features the following:
¦¦ Beans provide encapsulated data structure and logic.
¦¦ External resources (databases, Web services, etc.) are abstracted from presentation layer.
Although this is an improvement, the following drawbacks still exist:
¦¦ Control logic is still tied to presentation.
¦¦ Maintenance is better but still poor—too closely coupled to presentation logic. Listing 24.4 shows the logic from the previous JSP in a bean.
01 public class SalaryServletBean {
02 private String salary = "30000";
03 private LinkedList nameList;
04 private Connection connection;
05 private PreparedStatement statement;
06
07 public SalaryServletBean() {
08
09 // Database config information
10 String driver = "oracle.jdbc.driver. OracleDriver";
11 String url = "jdbc:oracle:thin:@joemama:1521:ORACLE";
12 String username = "scott";
13 String password = "tiger";
14
15 // Establish connection to database
16 try {
17 Class.forName(driver);
18 connection =
19 DriverManager.getConnection(url, username, password);
20
21 PreparedStatement statement
Listing 24.4 SalaryServletBean.java
JSP Design Errors 215
22 = connection.prepareStatement("SELECT ename FROM emp ^
WHERE sal > ?");
23
24 } catch(ClassNotFoundException cnfe) {
25 System.err.println("Error loading driver: " + cnfe);
26
27 } catch(SQLException sqle) {
28 sqle.printStackTrace();
30 }
31 }
32
33 /**Retrieve the List of Names*/
34 public LinkedList getNameList() {
36 return nameList;
37 }
38
39 /**Specify the salary level for getting the list of names*/
40 public void setSalary(String newValue) {
41 if (newValue!=null) {
42 salary = newValue;
43 }
44
45 statement.setFloat(1, Float.parseFloat(salary));
46
47 ResultSet results =
48 statement.executeQuery();
49
50 nameList.clear();
51
52 while (results.next()) {
53 nameList.add(results.getString(1));
54 }
56 }
57 }
58
Listing 24.4 (continued)
Listing 24.5 shows the accompanying JSP that uses the bean.
01 <%@ page import="java.util.*"%>
02 <HTML>
03 <HEAD>
04 <jsp:useBean id="mySalaryServletBean" scope="session" ^
class="SalaryServletBean" />
Listing 24.5 SalaryJsp.jsp (continued)
216 Item 24
05 <jsp:setProperty name="mySalaryServletBean" property="*" />
06 <TITLE>
07 Salary Servlet Jsp
08 </TITLE>
09 </HEAD>
10 <BODY>
11 <H1>
12 Salary Jsp
13 </H1>
14 <table>
15 <tr>
16 <td><b>Employee</b></td>
17 </tr>
18
19 <%
20 LinkedList myList = mySalaryServletBean.getNameList();
21 ListIterator li = myList.listIterator();
22 while (li.hasNext()) {
25 %>
26 <tr>
27 <td>
28 <%= (String)li.next() %>
29 </td>
30 </tr>
31 <% } %>
32 </table>
33
34 </BODY>
35 </HTML>
36
Listing 24.5 (continued)
Another way to abstract business logic out of the JSP code is through the use of JSP custom tag libraries. This approach allows for component reuse of particular code and also allows content developers to have access to complex Java code programming logic in a form that is familiar to them: HTML-like tags.
Listing 24.6 is an example of our salary business logic in a custom tag library. Notice this tag library has not been designed for the purpose of reuse, but rather to show the same logic expressed in a different manner.
01: package mypackage;
02: import javax.servlet.jsp.tagext.TagSupport; 03: import javax.servlet.jsp.tagext.BodyContent;
Listing 24.6 SalaryTag.java
JSP Design Errors 217
04 import javax.servlet.jsp.JspException;
05 import javax.servlet.jsp.JspTagException;
06 import javax.servlet.jsp.JspWriter;
07 import javax.servlet.jsp.PageContext;
08 import javax.servlet.ServletRequest;
09 import java.io.PrintWriter;
10
11 public class salarytag extends TagSupport
12 {
13 /*
14 tag attribute: salary
15 */
16
17 private String salary = "30000";
18
19 /**
20 * Method called at start of tag.
21 * @return SKIP_BODY
22 */
23 public int doStartTag() throws JspException
24 {
25 try
26 {
27 JspWriter out = pageContext.getOut();
28 SalaryServletBean mySalaryBean = new SalaryServletBean();
29 mySalaryBean.setSalary(salary);
30 LinkedList myList = mySalaryBean.getNameList();
31 ListIterator li = myList.listIterator();
32 while (li.hasNext()) {
33
34 out.println("<tr><td>");
35 out.println((String)li.next());
36 out.println("</td></tr>");
37 }
38
39 }
40 catch(Exception e)
41 {
42 e.printStackTrace();
43 }
44
45 return SKIP_BODY;
46 }
47
49 /**
50 * Method called at end of tag.
51 * @return SKIP_PAGE
52 */
53 public int doEndTag()
Listing 24.6 (continued)
218 Item 24
54 {
55 return SKIP_PAGE;
56 }
57
58 public void setSalary(String value)
59 {
60 salary = value;
61 }
62
63 public String getSalary()
Previous << 1 .. 74 75 76 77 78 79 < 80 > 81 82 83 84 85 86 .. 166 >> Next