chương 9 lập trình mạng Các trang JavaServer (JSP)

14 11 0
chương 9 lập trình mạng Các trang JavaServer (JSP)

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

• Vì các JSP về cơ bản phục vụ cùng một mục đích như các servlet, nên điều tự nhiên là yêu cầu tại sao cần có cả hai. • Câu trả lời đơn giản là các servlet yêu cầu kiến thức chuyên môn về Java lập trình viên, trong khi sản xuất các trang Web cho bất kỳ điều gì khác hơn là một trang web đơn giản thường là trách nhiệm của các tác giả trang web, những người thường

1/2/2020 Chapter JavaServer Pages (JSPs) Contents • 9.1 The Rationale Behind JSPs • 9.2 Compilation and Execution • 9.3 JSP Tags • 9.4 Implicit JSP Objects • 9.5 Collaborating with Servlets • 9.6 JSPs in Action • 9.7 Error Pages • 9.8 Using JSPs to Access Remote Databases 9.1 The Rationale Behind JSPs • Since JSPs serve essentially the same purpose as servlets, it is natural to ask why there is a need for both • The simple answer is that servlets require the expertise of Java programmers, whilst the production of Web pages for anything more than a simple site is usually the responsibility of Web page authors, who often not have such programming skills • The introduction of the JavaServer Pages technology attempts to return the job of Web page authoring to those people whose responsibility it is, whilst the Java programmers maintain responsibility for the software components used upon the Web pages Using JSPs rather than servlets also removes the rather tedious repetition of out.println for HTML tags 1/2/2020 9.1 The Rationale Behind JSPs • JSPs don’t make servlets redundant Servlets are still useful for supplying overall control to all/part of a Web site • This is achieved by a servlet receiving HTTP requests, determining what action to take, carrying out the necessary background processing (e.g., opening up a connection to a remote database) and then passing control to a JSP or ordinary HTML page that provides the response to the initial browser request • This last stage may involve the servlet selecting the appropriate page from a number of possible pages Thus, servlets and JSPs may be used together in a complementary and harmonious manner 9.2 Compilation and Execution • JSPs are held in the same Web application server folder as that holding HTML files (For Tomcat, of course, this means the root folder of the Web application.) • When a JSP is first referenced by a Web server, it is actually converted into a servlet • This servlet is then compiled and the compiled code stored on the server for subsequent referencing (For Tomcat, this compiled code is stored in \ work.) 9.2 Compilation and Execution • If the referencing by the server was in response to a request for the JSP from a browser, the compiled code would then be executed • All subsequent browser requests for this JSP would cause the compiled code to be executed on the server • This would continue to be the case until either the server was shut down (a rare event for most Web servers) or the JSP source code was changed (The Web server detects a change by comparing dates of source and compiled files.) 1/2/2020 9.2 Compilation and Execution • A consequence of the above is that, if the first time that a JSP is referenced by the Web server occurs when a request is received from a browser, there is a noticeable delay for the user of the browser as the Web server goes through the conversion and translation phases before executing the compiled code • In order to avoid this first-time delay, pre-compiled JSPs may be used 9.2 Compilation and Execution • One way of creating pre-compiled JSPs is for the Web page developer to use a development environment to go through all the JSPs on the site (causing the conversion-compilation-execution cycle to be performed) and then save the resultant class files in the appropriate directory of the production version of the site 9.2 Compilation and Execution • However, a more convenient way of producing the precompiled pages is provided by the JSP specification in the form of a special request parameter called jsp_precompile • Use of this parameter avoids the need to execute the associated JSP and may be used by a JSP container to produce the required class file(s) Like any request parameter included within a URL, jsp_precompile is preceded by a question mark • The following example shows the format required to precompile a JSP called MyPage.jsp: MyPage.jsp?jsp_precompile • The parameter jsp_precompile is a Boolean parameter, so the above line could alternatively end in jsp_precompile=true to make this explicit However, this is not necessary, since the default value for this parameter is true 1/2/2020 9.2 Compilation and Execution • When a browser calls up a Web page, the Web server executes the compiled JSP elements to produce HTML elements, merges these with the static HTML elements of the page and serves up the completed page to the browser • One important difference between testing servlets and testing JSPs is that it is not necessary to stop and restart the server when changes are made to a JSP 9.3 JSP Tags • In addition to standard HTML tags and ordinary text, a number of JSPspecific tags may be used on a JavaServer Page • The differing categories of JSP tag are listed below • • • • • • Directives Declarations Expressions Scriptlets Comments Actions • Note that all of the keywords used in the tags below must be in lower case 9.3.1 Directives • There are three tags in this category: page (used to define the attributes of the Web page, via a number of other keywords such as language, contentType and import); include (specifying an external file to be inserted); taglib (specifying a custom tag library to be used) • These directives are processed by the JSP engine upon conversion of the JSP into a servlet • Such tags commence with Note there must be no spaces between % and @! 1/2/2020 9.3.1 Directives • Examples (The language and contentType settings above are actually superfluous, since they are the default values.) Note the use of the import attribute with the page tag to allow the usual abbreviated reference to the contents of any available Java package 9.3.2 Declarations • These declare variables for subsequent use in expressions or scriptlets The tags commence with • Examples • Such declarations refer to instance variables of the servlet class that will be created from this JSP and will be recognized within any subsequent JSP tags on the page 9.3.3 Expressions • These tags are used to specify Java expressions, the values of which will be calculated and inserted into the current page • The tags commence with • Examples • Not that, unlike declarations (and scriptlets below), an expression must not be terminated with a semi-colon 1/2/2020 9.3.4 Scriptlets • Scriptlets are blocks of Java code that are to be executed when the JSP is called up • It is possible to include large amounts of code via this method, but that would not be good practice As noted in Sect 9.1, such code should be kept to a minimum • It will be seen in the latter part of the next chapter that the bulk of such processing may be encapsulated within a JavaBean, the methods of which may then be called from the JSP Methods of the standard Java classes may also be called, of course • Scriptlets tags commence with 9.3.4 Scriptlets • Example ’ is preceded by a forward slash ( ‘/’) • Example

Ngày đăng: 10/08/2021, 21:09

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan