JSP Scripting Elements

The Idea:

Controls the structure of the Servlet produced from the .jsp file.

 <%@ directive >

Types of Directives:

 

page

 <%@ page %>

Import classes

Cusotmize Servlet superclass

Set Content Type

include

<%@ include %>

Insert file into servlet class during translation into servlet

MUST be placed in document at point where want file to be insterted.

taglib

<%@ taglib %>

Can be used to define custom markup tags. (will not cover in class).

 

 

 

page

Attributes of page directive:

import

<%@ page import="package.class1, ..., package.classN" %>

Default packages imported:

java.lang.* javx.servlet.*   javax.servlet.jsp.*   

javax.servlet.http.*

Modified example from Book

Example of your own package.

contentType

<%@ page contentType="MIME-TYPE" %>

Specifies MIME type

Note: can also do with a JSP scriplet call

<% response.setContentType("MIME-TYPE" %>

isThreadSafe

<%@ page isThreadSafe="true OR false" %>

Specifies if the JSP page will implement SingleThreadModel interface or not.

FALSE= make it implement SingleThreadModel interface

DEFAULT: is TRUE...means you have taken the responsiblity to make your code multi-thread safe (synchronization of data, resources etc) and each time JSP page is invoked will create a different thread to run the underlying produced Servlet.

session

<%@ page session="true OR false" %>

Specifies if page participates in HTTP sessions or not.

DEFAULT = true.

buffer

<%@ page buffer="none OR size_in_kb" %>

Specifies if NO (none) buffer or the size in kilobytes.

Recall that the document being produced from the JSP is buffered (hence why we don't need to specify Content-Type right away).

DEFAULT: server specific, minimum 8K.

autoflush

<%@ page autoflush="true OR false" %>

true= flush when output buffer is full

false= raise exception when buffer overflows.

DEFAULT = true.

extends

<%@ page extends="package.class" %>

Specifies superclass of JSP produced Servlet.

info

<%@ page info="message" %>

Defines the string that can be retrieved from the Servlet via the getServletInfo() method.

errorPage

<%@ page errorPage="relative_URL" %>

Specifies another JSP page via a relative URL that will be used when any exceptions are thrown but, not code in the current JSP page.

isErrorPage

<%@ page isErrorPage="true OR false" %>

Specifies if this JSP page can act as an error page for another JSP page.

DEFAULT = false.

NOTE:

XML Syntax:

<jsp:directive.page attribute="value" />

 

 

include

<%@ include file="relative-URL" %>

Place in JSP , EXACTLY where you would like the file referenced to be included. THIS IS INCLUDED AT THE time of translation into the Servlet, but, before the Servlet is compiled and run.

Generally, use to include:

  • static HTML
  • other JSP code

POTENTIAL PROBLEM: Your server may not be set up to detect changes in included files into the JSP after the produced Servlet has been created!!!!!!!

>>>There is no simple way to invoke a "retranslate the JSP page now"...you must modify the date (by somehow "touching" the file) the JSP file.

LIMITED SOLUTION: Use the XML action instead which does not include the file at Translation time, but, afterwards at each client request (each new thread...if multi-threaded on).

<jsp:include page="relative-URL" flush="true">

LIMITATION: Can only use for static HTML, not to include other JSP code!!!!!!!!!!!!!!!!!!!!!!!

forward

<jsp:forward page="next.jsp">
            Zero or more <jsp:param name="x" value="y"> tags

</jsp:forward>

Calls the JSP, next.jsp, and if any param tags, passes these as parameters to it (inside of this other JSP you will use the request.getParameters("x") method to retrieve the value of the parameter being sent.

After this call to the next.jsp code, the program returns to this JSP and continues execution

Example: using forward tag and scriplets to test if some variable X has a certain value invoke one JPS otherwise invoke another

<%
       if(X == 1) //scriplet making some test
       {
%>

<jsp:forward page="processX.jsp">
       <jsp:param name="X" value=X>
</jsp:forward>

<%
      }else {
%>

<jsp:forward page="default.jsp">
</jsp:forward>

<%
}
%>

 

plugin

<jsp:plugin>

</jsp:plugin>

Place to load a plugin

 EXERCISE (YOU MUST TURN IN FOR RECORDING)

© Lynne Grewe