CS 318 - Week 10 Lecture - 4-3-13
JSP - JavaServer Pages
* allows you to put snippets of Java directly
into a text-based document (e.g., HTML and XML
documents)
* a JSP page is a text-based document
with static data (HTML, XML, etc.)
and JSP elements
* when a client requests a JSP page,
IF it is not already loaded into memory,
the JSP is compiled and converted to a servlet --
and successive requests to the jsp are handled
by the running resulting servlet
^ SO -- JSP pages end up being really rather
like a FLAVOR of servlet...!
* BUT please note: a single JSP is compiled
into a SINGLE servlet;
(an application consisting of 10 JSPs would
compile into 10 servlets -- whereas one
"hand-written" Java servlet that generated
10 different pages would be compiled still
into a single servlet...)
* nuts and bolts:
* the suffix for a JSP page is traditionally .jsp
* BECAUSE of HSU's "interesting" JSP/servlet setup,
and because we are SHARING a JSP container directory,
THOU SHALT start EVERY .jsp with YOUR HSU USERNAME!!!!!!!
* there IS a tool cpjsp which, like cpservlet,
will copy over your JSP to the required location
and give the copy the required permission
to save some effort
* intro to JSP elements:
* JSP comment element
<%-- comment text --%>
* remember, JSP is executed on the application tier --
thus, this element is good for comments for
the application-tier programmer, since the
client tier will NEVER SEE them;
* JSP declaration element
<%! declaration-WITH-SEMICOLON %>
<%! int i=0; %>
<%! String file="blah.html"; %>
* I *suspect* these declarations become
data-field-level in the compiled servlet...
* JSP expressom element
<%= Java-expression %> <%-- NO SEMICOLON, it's an
expression!!! --%>
<%= Math.sqrt(2) %>
<%= i %>
* JSP scriptlets
<% Java code %>
<%! String name; %>
<%-- note: you get an HttpServletRequest request,
and HttpServletResponse reponse,
and an HttpSession session
ALREADY declared and ready for you to use --%>
<%
name = request.getParameter("name");
if (name == null)
{
%>
<strong> Hello, stranger! </strong>
<%
}
else
{
%>
<strong> Hello, <%= name %>! </strong>
<%
}
%>
* JSP directive element
<%@ Java directive %>
FOR EXAMPLE -- the lovely include directive!!!
<%@ include file="filename" %>
for example,
<%@ include file="St10CommonTop.jsp" %>
* server-side includes -- where you include
common stuff via a file like this --
can be VERY beneficial,
for similar reasons that external CSS and
external JavaScripts are useful...!
* there are also some JSP-specific elements --
such as jsp:forward, jsp:-plugin, and more --
you can have custom tags,
and there are libraries of custom tags,
such as:
JavaServer Pages Standard Tag Library, JSTL,
which provides a collection of reusable standard
tags
* see:
St10HelloWorld.jsp
St10CommonTop.jsp
St10CommonBottom.jsp
...and a few additional examples, too;