Download (direct link):
You must first create the file you want to include. The file can contain plain text or HTML code, such as a table, header or footer. If the file contains plain text, you can save the file with the .txt extension. If the file contains HTML code, you can save it with the .html extension.
The include file should not contain any JavaServer Pages code. The Web server will ignore any JavaServer Pages code included in the file.
To include a file in a JSP page, you add an include statement to the page. The include statement must be enclosed between the <%@ opening delimiter and the %> closing delimiter. The filename specified in the include statement must be enclosed in quotation marks. The filename must be a fixed value, such as "footer.html".
You cannot use a variable that represents the name of a file in an include statement.
If you change the code in the file, all of the JSP pages that include the file will be updated. You may have to clear the Web server's buffer before your JSP pages will display the changes made to the file. For information about clearing the Web server's buffer, see page 92.
USING THE INCLUDE DIRECTIVE
JS\ Untitled - Notepad
File Edit Search Help
<h1>Welcome to ny Web site</h1>|
Z] images 2] jsp
22 META-INF Ö] servlets Z]WEB-INF
<t= Â î ø-
File name: | welcome, html
Save as type: | Tent Documents
CREATE A FILE TO INCLUDE
-D In a text editor, create the file you want to include in several JSP pages.
Untitled - Notepad
File Edit Search Help
É Save the file.
Note: If the file contains plain text, save the file with the .txt extension. If the file contains HTML code, save the file with the .html extension.
<title>My Web Page</title> </head>
<%B include file="" %>|-
INCLUDE A FILE
D Display the code for the JSP page in which you want to include a file.
H Between the <body> and < /body> tags, type <%@ include file="" %>.
GETTING STARTED WITH JAVASERVER PAGES
The include directive allows you to include a file that is stored in the same directory as the JSP page that includes the file or in a subdirectory of that directory. In this example, the JSP page is stored in a directory called test and the footer.html file is stored in a subdirectory called test/pages.
<%® include file="pages/footer.html" %>
You can also include a file that is located in the parent directory of the directory that stores the JSP page. To do so, you use the double dot notation to represent the name of the parent directory. In this example, the JSP page is instructed to look for the welcome.html file in the parent directory.
<%® include file="../welcome.html" %>
Using the include directive allows you to break code into manageable sections and then include the code in JSP pages as needed. Each include file should contain code specific to only one task. If you create a file that contains code for many tasks, the JSP pages may not use all the code and the Web server's resources will be wasted.
File ?dit Search Help
J File Edit View Favorites Tools Help
J <i=i Back ? ^ J ffl a Search [j§] Favorites ^History | ^ Qf Q JPt
<title>My Web Page</title>
Welcome to my Web site
<%e include file='jwelcone^htri3]" %>
between the quotation marks, type the name of the file you want to include.
? Save the page with the .jsp extension and then display the JSP page in a Web browser.
I The Web browser displays the result of using the include directive.
INTRODUCTION TO IMPLICIT OBJECTS
Implicit objects are created automatically when a Web server processes a JSP page. The available implicit objects include application, config, exception, out, page, pageContext, request, response and session. Each object is used to perform a specific task, such as handling errors,
sending text generated by a JSP page to a Web browser or interpreting information submitted by a form on a Web page.
Implicit objects are available for use in every JSP page you create. You do not have to write code that imports or instantiates an implicit object.
The scope of an object determines where the object can be accessed in an application. For example, the session object has session scope, which means that the object can be accessed by any JSP page processed during a session. Most implicit objects have page scope. When an object has page scope, the object can be accessed only in the JSP page in which the object was created. You can access implicit objects only from within scriptlets or expressions on a JSP page. Implicit objects are not available for use in directives, such as the page directive.
OBJECT: SCOPE: |
Since JSP pages use the underlying servlet technology of the Web server, implicit objects are usually derived from class files that are part of the servlet packages. For more information about implicit objects and the servlet packages, you can consult the Java SDK documentation.