Chapter 15: JavaServer Pages: Part I
pages 1 - 18
Introduction
- Java based technology for creating dynamic web pages
- JSP scripts use XML, HTML and Java code
- JSP scripts are stored on web servers
- JSP scripts are suffixed with .jsp
- JSP pages are ultimately compiled into Java servlet classes
Apache Tomcat
- Web site
- I downloaded and installed the Tomcat 5.5.9 exe binary
- You will need to generate an admin password during
the installation process.
- If you are successful, http://localhost:8080 will generate
a Congratulations page
- By default, JSP pages that you create must appear in the
following directory (or one of its subdirectories):
C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\ROOT
- Can be used as a stand-alone web server and as a JSP engine
- The Tomcat server detects newly created directories only when
it starts
Simple JSP Example
JSP Element Groups
- Directives: page, include, taglib
- Scripting Elements: declarations, expressions, code fragments (scriptlets)
- Comments
- Actions
Directives
Page Directives
- Place at top of JSP file
- Syntax: <%@ page attribute1="value1" ... %>
- attribute: import="java.util.*"
- attribute: session="true" or session="false" (default is "true")
- attribute: buffer="10kb" or buffer="none" (not as important, default
is server specific, but at least "8kb")
- attribute: isThreadSafe="true" (default is "true")
Include Directives
- Static Includes - done at page translation time.
e.g. <%@ include file="whatever.html" %>
- Dyamic Includes - done at request time
e.g. <jsp:include page="whatever.html" flush="true" />
Scripting Elements
JSP Declarations
- Have instance duration
- <%! private int count = 0; %>
- <%! public int incrementCount() { return count++; } %>
JSP Expressions
JSP Scriptlets
- Declarations in scriptlets have local duration
- <% valid Java code %>
Comments
- <!-- content comment --> - returned by server
- <%-- code comment --%> - not returned by server
- <% /* code comment */ %> - not returned by server
- <% // single line code comment %> - not returned by server
Exercise
Install Apache Tomcat on your laptop. Create a "hi world" servlet.
Demo the servlet to either Jack or John. 3 points.