DR Chen Scripting Components
DR Chen Scripting Components
Using JSP[TM]
Scripting/Page Directive
• Declarations:
– define variables and methods.
– <%! String destination; %>
• Scriptlets
– JSP code fragments.
– <% destination =
request.getParameter("destination");
%>
• Expressions:
– results of evaluating the expression are
converted to a string.
– <p>Destination: <%= destination %>
Declarations
• Used to define variables, methods.
• Examples:
– Variable declaration:
• <%! String destination; %>
• Results in member variables within the
compiled servlet.
– Method declaration:
• <%! public String getDestination() { return
destination} %>
The scope of the declaration is in a JSP file.
– If the JSP file includes other files within the
include directive, the scope expands to cover the
included files as well.
Scriptlets
server
Session 1
Session ID 1
Client 2 Session ID 2
Session 2
Client 1
server
application
Client 2
Session, Request, Page Scope
client
request
response request response
request scope
request scope
Session scope
Comments
<jsp:useBean id="session_counter"
class="component.CounterBean" scope="session" />
<jsp:useBean id="app_counter" class="component.CounterBean"
scope="application" />
<% session_counter.increaseCount();
synchronized(page) {
app_counter.increaseCount();
}
%>
<h3>
Number of accesses within this session:
<jsp:getProperty name="session_counter" property="count" />
</h3><p><h3>
Total number of accesses:
<% synchronized(page) { %>
<jsp:getProperty name="app_counter" property="count" />
<% } %>
</h3>
Counter Bean Sample
package component;
<%
if ( pay.getPrincipal() !=0 && pay.getInterestRate() !=0 &&
pay.getTerm() !=0 ) {
%>
public PaymentBean() {
reset();
}
• <jsp:forward>
– Forwards request to HTML page, JSP, or
servlet
– <jsp:forward page="relativeUrl" />
• <jsp:include>
– Includes output or response from another
file into this current JSP page.
– <jsp:include page="relativeUrl"
flush="true" />
• <jsp:plugin>
– Downloads Java plugin to browser to
execute applet or bean.
Including Pages at Translation
Time
• <%@ include file ="Relative URL" %>
• The include directive includes the content of this
specified page into main where JSP content
affects main page.
– However, servers are not required to detect
changes to the included file, you need to force
main JSP file to change whenever the included
file changes.
– Can use Unix "touch" command to enforce
change, or
• <%--Navbar.jsp modified 3/1/00 --%>
<%@ include file="Navbar.jsp" %>
• Use include directive if the file changes rarely.
It is faster than include action.
Including Pages at Request Time