You are on page 1of 23

Including files and applets in JSP

pages
Topics to cover
• Using Jsp:include to include pages at request
time.
• Using <%@include..%> the include directive to
include file at page translation time.
• Understand why jsp:include is usually better
that the include directive.
• Using jsp:plugin to include applets for the java
plug-in.
JSP 3 main capabilities for including
external pieces into a JSP document

• The jsp:include action


• The include directive
• The jsp:plugin action
The jsp:include action
1. It includes the output of a page at request time.
2. Its main advantage is that it saves you from
changing the main page when the included pages
change.
3. Its main disadvantage is that since it includes the
output of the secondary page, not the secondary
page’s actual code , the included pages cannot use
any JSP constructs that affect the main page as a
whole.
The include directive
1. This construct lets you insert JSP code into the
main page before that main page is translated into
a servlet.
2. Its main advantage is that it is powerful: the
included code can contain JSP constructs such as
field definitions and content-type settings that
affect the main page as a whole.
3. Its main disadvantage is that it is hard to
maintain: you have to update the main page
whenever any of the included pages change.
The jsp:plugin action
1. The jsp:plugin element is used to insert
applets that use the Java Plug-in into JSP
pages.
2. Its main advantage is that it saves you from
writing long, tedious, and error-prone OBJECT
and EMBED tags in your HTML.
3. Its main disadvantage is that it applies to
applets, and applets are relatively infrequently
used.
jsp:include Action Tag : Including
Pages at Request Time
The jsp:include action includes the output of a secondary page at
the time the main page is requested.
jsp:include is a portable mechanism that inserts any of the
following into the JSP output:
• The content of an HTML page.
• The content of a plain text document.
• The output of JSP page.
• The output of a servlet.
Although the output of the included pages cannot contain JSP, the
pages can be the result of resources that use servlets or JSP to
create the output.
The server runs the included page in the usual way and places the
output into the main page.
The page Attribute: Specifying the
Included Page
The page attribute
<jsp:include page="relative-path-to-resource" />

• It should be a relative URL referencing the resource whose output should


be included.
• Relative URLs that do not start with a slash are interpreted relative to the
location of the main page.
• Relative URLs that start with a slash are interpreted relative to the base
Web application directory, not relative to the server root.
• URLs that start with slashes are interpreted differently by the server than
by the browser.
• The server always interprets them relative to the current Web application.
• The browser always interprets them relative to the server root.
• To prevent the included files from being accessed separately, place them
in WEB-INF or a subdirectory thereof.
XML syntax and jsp:include
Note the following three things to be
followed in XML:
1) XML element names can contain colons:
jsp:include
2) XML tags are case sensitive
3) XML tags must be explicitly closed
<xyz></xyz> with <xyz/>
The flush Attribute
jsp:include has a second attribute: flush

<jsp:include page="relative-path-to-resource"
flush="true" />

This attribute is optional.


It specifies whether the output stream of the
main page should flushed before the inclusion of
the page (the default is false).
Example: A news headline page
The jsp: param Element:
Augmenting Request Parameters
<jsp:include
page=“/fragments/Standardheading.jsp”>
<jsp:param name=“bgclor” value=“YELLOW”/>
</jsp:include>
This main page is invoked by means of
http://host/path/Mainpage.jsp?fgcolor=RED
http://host/path/Mainpage.jsp?bgcolor=RED
Request.getparameter(“fgcolor”) returns “RED”
Including files at page translation
time: the include directive
<%@ include file=“relative url”%>
The difference between the jsp:include and the
include directive is the time at which they are
invoked.
jsp:include is invoked at request time
include directive is invoked at page translation
time.
Difference between jsp:include & include
Action jsp:include include
Syntax <jsp:include page=“path” /> <%@ include file=“url”%>

Inclusion occurs Request Time Page Translation Time

What included Output of page Actual content of file

How many servlet Two (main & included One (included file inseted
creates page) into main)
Can included page No Yes
set response header
Does main page No Yes
needed to update
Can include No Yes
methods, fields
for main page
Maintenance problems with include
1) The problem of inclusion occurring at page
translation time is difficult to maintain pages is
the case with jsp:include.
2) The JSP pages that use it may needed to be
update.
3) Servers are required to detect the changes and
translate into servlets, but they are not do so.
• This is a significance inconvenience; it results
serious maintenance problems.
Additional power from include
directive
Secondary page:
<%! private int accessCount = 0; %>

Main page:
<%@ include file="/WEB-INF/ContactSection.jsp" %>
<%=accessCount++ %>
XML syntax for the include
directive
<%@ include file=“ . … …. “%>

<jsp:directive.include file=“…..” />

Main page should be the web pages


All secondary pages should be in the WEB-INF
where direct access can be restricted.
Forwarding Requests with jsp:forward
The jsp:forward is used to obtain the complete output
from the auxiliary page. For example here is the page
that randomly selects either page1.jsp or page2.jsp to
output.
<% String s;
if (Math.random()>0.5) {
s=“examples/page1.jsp”;
} else {
s=“examples/page1.jsp”; } %>
<jsp:forward page=“<% s %> />
Including Applets for Java Plug-In
Early in the evolution, Java’s main application area was
Applet.
<APPLET CODE="MyApplet.class"
WIDTH=475 HEIGHT=350>
</APPLET>

Replaced by

<jsp:plugin type="applet"
code="MyApplet.class"
width="475" height="350">
</jsp:plugin>
The jsp:plugin Element
The jsp:plugin supplies four attributes to include applets :
type, code, width, height.
We need to supply “applet” to type attribute to plugin the
applets in JSP pages.
<jsp:plugin type=“applet”
code=“myapplet.class”
width=“400” height=“400”
</jsp:plugin>
Few more attribute are: codebase, align, hspace, vspace,
archive, name, and title (tooltip).
Jsp:plugin element has a number
of optional attributes
• type
• code
• width
• height
• codebase
• align
• hspace
• vspace
• name
The jsp:param and jsp:params Element
The JSP param element is used with jsp:plugin in a manner
of PARAM is used with APPLET by getParameter.
<jsp:plugin type=“applet”
code=“myapplet.class”
width=“400” height=“400”>
<jsp:params>
<jsp:param name=“p1” vlaue=“hi”
<jsp:param name=“p2” vlaue=“Hello”
</jsp:param>
</jsp:pluin>
The jsp:fallback element
The jsp:fallback element provides alternative text to
browsers that do not support OBJECT or embed.

<jsp:plugin type=“applet”
code=“myapplet.class”
width=“400” height=“400”>
<jsp:fallback>
<b> Error: This example requires Java </b>
</jsp:fallback>
</jsp:pluin>

You might also like