Java Server Pages: A Code-Intensive Premium Reference- P15
Số trang: 10
Loại file: pdf
Dung lượng: 223.76 KB
Lượt xem: 5
Lượt tải: 0
Xem trước 2 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Java Server Pages: A Code-Intensive Premium Reference- P15:Before you begin reading Pure JSP Java Server Pages, you might want to take a look at its basicstructure. This should help you outline your reading plan if you choose not to read the text from cover tocover. This introduction gives you an overview of what each chapter covers.
Nội dung trích xuất từ tài liệu:
Java Server Pages: A Code-Intensive Premium Reference- P15 TitleRatingPriceQuantity - 141 -There are four sections of this JSP that need to be examined in order to understand how theConnectionPool works. The first section is included in the following code snippet:This section of code tries to find an instance of a ConnectionPool with application scope and an idof pool. If it cannot find an instance of the pool, it will create one. This bean uses application scope,because the application beans can be accessed by any JSP until the JSP engine is shut down.The next section of code to be studied is contained in the following code snippet:// The pool is not initializedif ( pool.getDriver() == null ) { // initialize the pool pool.setDriver(sun.jdbc.odbc.JdbcOdbcDriver); pool.setURL(jdbc:odbc:Movie Catalog); pool.setSize(5); pool.initializePool();}In this code snippet we are checking to see if the pool has been initialized. If it has not, then we set theappropriate properties to initialize the pool.The third section of code to be looked at is// Get a connection from the ConnectionPoolcon = pool.getConnection();This section gets a normal JDBC Connection object from the pool. At this point the JSP can use thisconnection just like any other.The final section to be examined isfinally { try { if ( con != null ) { // release the connection no matter what pool.releaseConnection(con); } } catch (Exception e) { out.println(e.getMessage()); }}This final section is used to put our connection back into the ConnectionPool for further use. Theconnection is released by calling the pool.releaseConnection() method with the Connectionobject. This method call is placed in the finally block to guarantee its execution. - 142 -To see how the ConnectionPool improves performance, compile the ConnectionPool andPooledConnection objects and move them to the /purejsp/WEB-INF/classes/com/purejsp/connectionpool/ directory. Then move theJDBCPooledExample.jsp to the /purejsp/ directory and open your browser to thefollowing URL:http://yourserver:8080/purejsp/JDBCExample.jspYou will now see a page similar to Figure 14.1.Figure 14.1: Output of the JDBCPooledExample.jsp.SummaryIn this chapter, we covered how to use a JDBC connection pool in a JSP. We also covered how to sharethe pool with other JSPs, by creating it with a scope of application.In Chapter 15, we cover how you can combine XML and JSPs.Chapter 15: JSP and XMLOverviewThe Extensible Markup Language, or XML, is a meta language for creating markup languages used todescribe structured data. XML is a self-describing language, composed of tags and values. It is often usedto describe objects that are passed in messages between applications. An example of a simple XMLdocument is included in Listing 15.1.Listing 15.1: item.xml 33445 Austin Powers The International Man of Mystery 19.95 56 - 143 -The first line of this snippet describes a processing instruction which states that this XML document isbased on version 1 of the XML specification. Processing instructions begin with a less-than sign and aquestion mark ().The rest of this document describes an ITEM object with four attributes: ID, DESCRIPTION, PRICE, andQUANTITY. Each of these attributes is contained in an open and closed pair. You shouldnotice how the hierarchy of the object is described in a container-like fashion, wherein the attributes of theITEM are between the ITEMs open and closing tags. This shows the parent/child relationship of the ITEMobject. All XML documents can be viewed as navigable tree structures. Figure 15.1 shows the standardstructure of our XML document.Figure 15.1: The XML document tree structure.While this is hardly a complete definition of XML, which is well beyond the scope of this book, it iscomplete enough to show how XML and JSP can be used together.XML and JavaNow that you understand XML basics, lets take a look at how we can use XML and Java together. Therehave been many Java parsers developed to interact with XML documents. The three most common havebeen developed by Sun Microsystems, IBM, and Microsoft. For our example, we will be using Suns JavaAPI for XML parsing, which can be downloaded from the following URL:http://java.sun.com/xml/download.htmlFollow the installation instructions for your platform, including adding the jaxp.jar and the parser.jarfiles to your classpath.Suns API is composed of two core components, the Document Object Model (DOM) and the Simple APIfor XML (SAX API). The DOM is a tree-based API, and the SAX is an event-based API. For our examples,we will be using the SAX API.The SAX APIAs we stated earlier, the SAX ...
Nội dung trích xuất từ tài liệu:
Java Server Pages: A Code-Intensive Premium Reference- P15 TitleRatingPriceQuantity - 141 -There are four sections of this JSP that need to be examined in order to understand how theConnectionPool works. The first section is included in the following code snippet:This section of code tries to find an instance of a ConnectionPool with application scope and an idof pool. If it cannot find an instance of the pool, it will create one. This bean uses application scope,because the application beans can be accessed by any JSP until the JSP engine is shut down.The next section of code to be studied is contained in the following code snippet:// The pool is not initializedif ( pool.getDriver() == null ) { // initialize the pool pool.setDriver(sun.jdbc.odbc.JdbcOdbcDriver); pool.setURL(jdbc:odbc:Movie Catalog); pool.setSize(5); pool.initializePool();}In this code snippet we are checking to see if the pool has been initialized. If it has not, then we set theappropriate properties to initialize the pool.The third section of code to be looked at is// Get a connection from the ConnectionPoolcon = pool.getConnection();This section gets a normal JDBC Connection object from the pool. At this point the JSP can use thisconnection just like any other.The final section to be examined isfinally { try { if ( con != null ) { // release the connection no matter what pool.releaseConnection(con); } } catch (Exception e) { out.println(e.getMessage()); }}This final section is used to put our connection back into the ConnectionPool for further use. Theconnection is released by calling the pool.releaseConnection() method with the Connectionobject. This method call is placed in the finally block to guarantee its execution. - 142 -To see how the ConnectionPool improves performance, compile the ConnectionPool andPooledConnection objects and move them to the /purejsp/WEB-INF/classes/com/purejsp/connectionpool/ directory. Then move theJDBCPooledExample.jsp to the /purejsp/ directory and open your browser to thefollowing URL:http://yourserver:8080/purejsp/JDBCExample.jspYou will now see a page similar to Figure 14.1.Figure 14.1: Output of the JDBCPooledExample.jsp.SummaryIn this chapter, we covered how to use a JDBC connection pool in a JSP. We also covered how to sharethe pool with other JSPs, by creating it with a scope of application.In Chapter 15, we cover how you can combine XML and JSPs.Chapter 15: JSP and XMLOverviewThe Extensible Markup Language, or XML, is a meta language for creating markup languages used todescribe structured data. XML is a self-describing language, composed of tags and values. It is often usedto describe objects that are passed in messages between applications. An example of a simple XMLdocument is included in Listing 15.1.Listing 15.1: item.xml 33445 Austin Powers The International Man of Mystery 19.95 56 - 143 -The first line of this snippet describes a processing instruction which states that this XML document isbased on version 1 of the XML specification. Processing instructions begin with a less-than sign and aquestion mark ().The rest of this document describes an ITEM object with four attributes: ID, DESCRIPTION, PRICE, andQUANTITY. Each of these attributes is contained in an open and closed pair. You shouldnotice how the hierarchy of the object is described in a container-like fashion, wherein the attributes of theITEM are between the ITEMs open and closing tags. This shows the parent/child relationship of the ITEMobject. All XML documents can be viewed as navigable tree structures. Figure 15.1 shows the standardstructure of our XML document.Figure 15.1: The XML document tree structure.While this is hardly a complete definition of XML, which is well beyond the scope of this book, it iscomplete enough to show how XML and JSP can be used together.XML and JavaNow that you understand XML basics, lets take a look at how we can use XML and Java together. Therehave been many Java parsers developed to interact with XML documents. The three most common havebeen developed by Sun Microsystems, IBM, and Microsoft. For our example, we will be using Suns JavaAPI for XML parsing, which can be downloaded from the following URL:http://java.sun.com/xml/download.htmlFollow the installation instructions for your platform, including adding the jaxp.jar and the parser.jarfiles to your classpath.Suns API is composed of two core components, the Document Object Model (DOM) and the Simple APIfor XML (SAX API). The DOM is a tree-based API, and the SAX is an event-based API. For our examples,we will be using the SAX API.The SAX APIAs we stated earlier, the SAX ...
Tìm kiếm theo từ khóa liên quan:
nhập môn lập trình kỹ thuật lập trình lập trình flash lập trình web ngôn ngữ html lập trình hướng đối tượngTài liệu liên quan:
-
Đề cương chi tiết học phần Cấu trúc dữ liệu và giải thuật (Data structures and algorithms)
10 trang 329 0 0 -
Giáo trình Lập trình hướng đối tượng: Phần 2
154 trang 283 0 0 -
Kỹ thuật lập trình trên Visual Basic 2005
148 trang 281 0 0 -
NGÂN HÀNG CÂU HỎI TRẮC NGHIỆM THIẾT KẾ WEB
8 trang 224 0 0 -
Giới thiệu môn học Ngôn ngữ lập trình C++
5 trang 206 0 0 -
101 trang 205 1 0
-
Bài giảng Nhập môn về lập trình - Chương 1: Giới thiệu về máy tính và lập trình
30 trang 177 0 0 -
Luận văn: Nghiên cứu kỹ thuật giấu tin trong ảnh Gif
33 trang 156 0 0 -
Luận văn tốt nghiệp Công nghệ thông tin: Xây dựng website bán hàng nông sản
67 trang 146 0 0 -
Giáo trình nhập môn lập trình - Phần 22
48 trang 140 0 0