JSP: Java Server Pages
The IDEA:
- Enables you to embed JSP calls inside of a HTML page that
creates dynamically generated content from servlets.
- Hence is really a scripting language with no compilation
(unlike Servlets, Applets, Applications in Java).
- Unlike CGI and servlets, do not have to generate the entire
web-page, you can have some static HTML and only generate what
you need.
- File has extension .jsp
- Can install
the file in any directory where you would put an HTML file (unlike
Servlets, which can be restricted in location)....however, this
is not true for support classes, beans, etc may call from JSP.
- JSP engine
on server receives request for a .jsp page it:
- Reads in the page, and transforms the contents to a Servlet
- Even the static HTML is converted to print statements,
printing to the output stream associated with the Servlet’s
service() method.
- This translation is done the first time the page is requested.
- Then the server runs the resulting created Servlet and
resturns the results to the client.
How it works:
- Runs on server before delivering HTML to the client.
- Web-Server with built in JSP support process the JSP commands
(actually translates them into servlets behind the scenes), and
inserts into the static HTML of the document the results of running
the code
- What really happens is the web-server
takes the JSP commands and interprets them forming an automatically
generated servlet that it then invokes. The results of the
generated servlet are then returned, inserted into the HTML where
the JSP command was found. See below.
- Important: when you use other java classes, beans, etc. from
a JSP file, you need to deploy them in a package inside of a webapplication.
The location will be WEB-INF/classes/directoryMatchingPackageName.
Or alternatively you can place in a JAR file and put in WEB-INF/lib.
This is important because you need to import them into the JSP
file(<%@ page import="packagename.*" %>
. Also, the default package that a JSP or Servlet is placed into
is not standardized byu the JSP spec so, different containers
do different things!!!! Be carefull and always use packages.
|
JSP Constructs
Scripting Elements
specifies code that will become part of
the servlet produced from the Server translation of the .jsp
file
|
Directives
controls structure of servlet produced.
|
Actions
specifies existing components that should
be used in the servlet.
|
|
|
JSP Coding Recommendations (from core
Servlets...): |
Simple application |
Java Code inside of JSPs
- place code in JPS page. Good for only small amounts of code.
|
|
Call Java code indirectly
- Put is separate classes
- place in a package
- import into JSP code and use.
|
|
Use Java Beans
- Create seperate classes structured as bea ns.
- jsp:useBean, jsp:getProperty and jsp:setProperty
to invoke the code..
- Like seperate classes, deploy in a web application.
|
|
MVC Architecture
- Servlet responds to original request, looks up data, stores
results in a bean.
- Then forwards to a JSP to present results. JSP uses beans.
|
Complex application or large
development team |
JSP Expression Language
- Use shorthand syntax to access and output object properties
- Used with MVC
- See book for details
|
Complex application or large
development team |
Custom Tags
- Create tag handler classes.
- Invoke handlers with XML-like cutsom tags.
|
Simple Example
<HTML>
<HEAD> <TITLE> My web-site </TITLE> </HEAD>
<BODY>
<H1> Welcome, </H1>
<!--get user name from cookie if not a first-time visitor...otherwise
use, "New User" -->
<%= Utils.getUserNameFromCookie(request)
%>
.....now more HTML......
</BODY>
</HTML>
|
In the above example, there is a single JSP line of code.
Note the %= at the start of the tag, tells the Web-server that
this is a JSP call and not a static HTML tag. |
Recall, that JSP commands are interpreted by
the Web-server and are actually converted into an automatically generated
servlet. Below shows the three possible general forms of JSP
commands and how they are interpreted during this converstion process. |
<%= expression %>
The expression isevaluationed and inserted into the automatically
generated servlet's output
- Current Time: <%= new java.util.Date() %>
|
<% code %>
Here the code is inserted into the automatically
generated servlet's _jspService method (called by service)
|
<%! code %>
Here the code is treated as a declaration, and are insterted
into the body of the automatically generated servlet class, outside
of any existing method.
|
<%@ directive> |
<%-- JSP_Comments --%>
The above syntax is treated as a JSP comment.
|
|