Java Server Pages
API
Tutorials:
examples:
JWS examples (note: these are all pre 2.0)
JavaServer Pages
( 1, 2,
3 , 4 , 5 )
Sales Pitch and SUN .( 1.3)
What is a JSP page? (1.3, Hall)
JavaServer Pages are Web pages that contain a combination of HTML and code written in a scripting language such as Java. The two primary benefits of
JavaServer Pages are:
-
They cleanly separate page presentation from page content (presentation logic vs. application logic) in dynamically generated pages by letting you call objects from within a JavaServer page
- They are automatically recompiled by the PageCompileServlet when changes are made to the source file. (JavaServer Pages are processed by a special servlet called the PageCompileServlet.)
Presentation aspects are emphasized in JSPs, if your primary purpose is
presentation, then use JSP. You could then use a Java Bean for the implementation of the Business logic.
Introductions
Hall's Overview with Syntax summary (1.0). Here might be a good time to also
note the life-cycle in tutorial ch. 12 .
In addition to HTML and XML (Template Text: Static HTML ), a JavaServer (1.2) Page can contain five types of information.
The first three are scripting elements and let you specify java code that will become part of the resultant servlet,
directives let you control the overall structure of the servlet, and actions let you specify existing componets that should be used (see below):
- Declarations ( <%! ... %> )
- Scriptlets ( <% ... %> )
- Expressions ( <%= .... %> )
- Directives ( <%@ ... %> )
- Standard Actions (example: Bean tags jsp:useBean, jsp:setProperty) Called Elements in newer versions
The JSP elements in a JSP page can be expressed in two syntaxes--standard and XML--though any given file can only use one syntax. (No longer true - I have used both in a file.)
Newer specs (2.0) allow more use of custom tags and expression language
- Custom tags set a variable (c:set), iterate over a collection of locale names (c:forEach), and conditionally insert HTML text into the response (c:if, c:choose, c:when, c:otherwise).
See more in Java Server Pages Standard Tag library (Ch 14) JSTL and its Core tag library
- JSP expression language expressions (${ }) retrieve the value of object properties. The values are used to set custom tag attribute values and create dynamic content. examples, another, and hall
- A function (f:equals) tests the equality of an attribute and the current item of a collection. (Note: A built-in == operator is usually used to test equality).
SUNs Syntax reference card (2.0) and 1.2
What's it look like? How do I know if it did something wrong?
A look at an example jspform.jsp1 Look at the Page Source to see the page "code". on my Netscape this shows the tags, on IE it doesn't and its source.
Note the tags. Notice use of scriptlets (conditional logic in the web page!). Then
check out what it does jspform.jsp . Tries to compile - Note the "invalid directive" (only directive is import...note you may have errors like this if you use older JSPs...check it out in 2.0)
<%@ page import="java.util.*" %>
I changed it in jspform2.jsp...interesting errors - what does go on in the background!
Hint from Hall: When debugging JSP pages, be sure to turn off Internet Explorer's "friendly" HTTP error messages.
This is also interesting to me. The above example was from version 1.0.
Look at the SUN tutorial initial example for
JSP 1.? and JSP 2.0 (Ch 12 first example)
use of custom tags (a whole tutorial chapter (Ch. 15) on them ). What a difference! Also note 2.0's use of Expression Language (EL) ${} for expressions (relatively new).
Here might be a good place to have a link to HTML tags too.
Tutorials talk about custom tags and the Struts tag library and java standard tag library jstl. I especially enjoyed the Struts introduction
Hall's Syntax Summary:
JSP Scripting Elements
JSP Scripting elements let you insert code into the serlvet that will be generated from the JSP. (Hall has good simple examples)
Hall's tutorial, pdf from Hall book JSP Scripting Elements,
and the J2EE tutorial Ch 16 Scripting in JSP pages, also 1.2 reference
and J2EE section (Ch 16)
- of the form <% code %> and are inserted into the servlet's _jspService method (called by service)
- request response out in - can use if we already declared)
Finally, note use sparingly
Declarations and J2EE section (Ch 16)
- of the form <%! code %> and are inserted into the body of the servlet class, outside of any existing methods
Directives and Action Elements
Directives
and J2EE section (Ch 13)
Enable you to modify certain characteristics of the JavaServer page and resulting servlet. These specifics include the scripting language and the packages used. They tell the compiler information at compile time. Changed with version 1.2 to use only page and include directives with attribute (many more directives were used in earlier versions (above))
Actions (or elements)
Also the J2EE tutorial on JavaBeans Components in JSPs
Objects: Implicit and Application Specific
Using Objects within JSPs(what is implicitly given to you and using your own) ( newer)
Reusing Content in JSP pages
Reusing Content in JSP pages J2EE tutorial and transferring control to another web component
More
Developing XML Solutions with JavaServer[tm] Pages
Comments in the jsp page
Form processing using JSP from jGuru and What happens to the Form data
look around on this tutorial for more informative pages, for example exception handling
Finally - about JSP to servlet code, see The Life Cycle of a JSP Page . Here is newer version with 2.0 stuff
In the jGuru tutorial an example translation from JSP to servlet
Blueprints - Could be quite nice for examples
and Troubleshooting Common problems and Their Solutions