Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 51 Scripting With JSP Elements JavaServer Pages By Xue Bai.

Similar presentations

Presentation on theme: "Chapter 51 Scripting With JSP Elements JavaServer Pages By Xue Bai."— Presentation transcript:

1 Chapter 51 Scripting With JSP Elements JavaServer Pages By Xue Bai

2 Chapter 52 Objectives In this chapter, you will: Use the JSP page directives to determine some characteristics of JSP pages Use JSP include directive and include action to include content from another file Use simple JSP scripting elements Understand variable duration Use JSP comments to make your code more readable

3 Chapter 53 Page Directives Page directive attributes apply to the entire JSP page:

4 Chapter 54 Import Attribute Make Java classes available to the scripting environment Use Java API to add power to your JSP script When a Java class is imported to a JSP page, you can reference this class without explicitly specify the class package names

5 Chapter 55 Using java.util.Date class Without import: If the class imported, then:

6 Chapter 56 Session Attribute A session attribute can hold information across multiple requests A session begins when a user requests a JSP page at the first time The session attribute specifies how the page uses the predefined session object in JSP Default value is true If the value is set to false, then no session is created

7 Chapter 57 Session Object The method isNew() tests whether the session is newly created The method getCreationTime() returns when the session is created

8 Chapter 58 Buffer Attribute Specifies the size of the buffer used by the implicit out variable If a buffer is specified then output is buffered with a buffer size not less than the specified size

9 Chapter 59 Clear Buffer out.clear() clears whatever is buffered in the output stream Let's send some text to the HTML output stream The content is buffered The buffered content is not cleared

10 Chapter 510 isThreadSafe Attribute Specify whether to allow more than one thread to execute the JSP code

11 Chapter 511 Thread A thread is a process that handles client requests by executing the code sequentially Each client request for this page generates a thread that requests access to this code segment

12 Chapter 512 AutoFlush Specify whether the buffered output should be sent automatically when the buffer is filled:

13 Chapter 513 ErrorPage Specify a JSP page that can handle any exceptions that are not handled in the current page

14 Chapter 514 IsErrorPage Specify whether or not the current page can act as the error page for other JSP pages (default is false)

15 Chapter 515 Include Files Usage: –Provide the same navigation links and footer on each of the pages –Reuse JSP code

16 Chapter 516 Include Directive The content of the specified page is read at translation time (when it is converted into its corresponding servlet) and is merged with the original page when the page is requested for the first time After the original JSP page has been request, updating included file has no impact on the original file

17 Chapter 517 Include Action The action does not merge the actual contents of the specified page at translation time The page is included each time the JSP page is requested That means whenever you modify the included file, the changes are reflected the next time the page is requested

18 Chapter 518 Declaration A declaration is used to declare variables and methods in the scripting language used in a JSP page The variables and methods declared are inserted into the servlet class generated when the JSP container translates the JSP page A declaration has the following syntax:

19 Chapter 519 Declaration Example <%! int count = 0; public int getCount(){ return ++count; } %> Variables are shared among all clients

20 Chapter 520 JSP Expressions Insert a value directly into the output stream:

21 Chapter 521 JSP Scriptlets A scriptlet is a code fragment that is executed at request-processing time You can embed any valid Java language code inline between the tags The basic syntax is:

22 Chapter 522 Variable Duration The duration of variable (also called its lifetime) is the period during which the variable exists in memory Variables of instance duration exist from the point at which the corresponding servlet that defines them is loaded into memory Local variables are created when program control reaches their declaration

23 Chapter 523 Variable Duration They exist while the block in which they are declared is active, and they are destroyed when the block in which they are declared is exited Variables defined in declaration have instance duration Variables defined in scriptlet have local duration

24 Chapter 524 Comments Improve your program’s readability Ignored when the program is translated There are two types of comments: –Content comments –And server-side comments

25 Chapter 525 Content Comments Sent back the client via response output stream They are not displayed on the browser The same syntax as comments in HTML: It can include dynamic data: <!-- JSP Page = -->

26 Chapter 526 Server-side Comments They are ignored at translation time They are not sent to clients <%-- multiple line comments -- %> <% /* Multiple line comments */ %>

Download ppt "Chapter 51 Scripting With JSP Elements JavaServer Pages By Xue Bai."

Similar presentations

Ads by Google