Introduction to JavaServer Pages
In the early days,
servlets were the only API available to develop server-side web
applications in Java. Servlets had a number of advantages over CGI
scripts, which were (and to some extent, still are) prevalent in those
days. Some of the advantages of servlets over CGI scripts included
increased performance and enhanced security.
However, servlets
also had one major disadvantage. As the HTML code to be rendered in the
browser needed to be embedded in Java code, most servlet code was very
hard to maintain. To overcome this limitation, Java Server Pages (JSP)
technology was created. JSPs use a combination of static HTML content
and dynamic content to generate web pages. As the static content is
separate from the dynamic content, JSP pages are a lot easier to
maintain than servlets that generate HTML output.
In most modern applications using JSPs, servlets are still used. However, they typically assume the role of a controller in the Model-View-Controller (MVC)
design pattern, with JSPs assuming the role of a view. As controller
servlets have no user interface, we don't run into the issue of having
HTML markup inside Java code.
Developing our first JSP
JSPs
are basically pages containing both static HTML markup and dynamic
content. Dynamic content can be generated by using snippets of Java code
called scriptlets or by using standard or custom JSP tags. Let's look
at a very simple JSP code that displays the current server time in the
browser:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.Date" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Server Date And Time</title>
</head>
<body>
<p>Server date and time: <% out.print(new Date()); %>
</p>
</body>
</html>
To deploy this JSP, all that
needs to be done is to put it in a WAR file. Like we mentioned before,
the easiest way to deploy the WAR file is to copy it to [glassfish installation directory]/glassfish/domains/domain1/autodeploy.
Quickly deploying simple JSPs
Simple JSPs can be quickly deployed without having to package them in a WAR file by copying them to [glassfish installation directory]/glassfish/domains/domain1/docroot/, and previewed in the browser by pointing them to http://localhost:8080/jspname.jsp.
After a successful deployment, pointing the browser to http://localhost:8080/firstjsp/first.jsp should result in a page like the following:
The Server date and time: string came from the static text immediately following the<p>
tag in the JSP page. The actual date and time displayed is the server's
date and time. The value came from the output of the code between the<% and %> delimiters. We can place any valid Java code between these two delimiters. Code inside these delimiters is known as a scriptlet. The scriptlet in the previous JSP makes use of the out
implicit object. JSP implicit objects are objects that can be readily
used in any JSP; no need to declare or initialize them. The out implicit object is an instance of javax.servlet.jsp.JspWriter. It can be thought of as an equivalent of calling the HttpServletResponse.getWriter() method.
The first two lines in the previous JSP are JSP page directives.
A JSP page directive defines attributes that apply to the entire JSP
page. A JSP page directive can have several attributes. In the previous
example, the first page directive sets the language, contentType, charset, and PageEncoding attributes. The second one adds an import statement to the page.
As can be seen in the example,
JSP page directive attributes can be combined in a single directive, or a
separate page directive can be used for each attribute.
The following table lists all attributes for the page directive:
Attribute
|
Description
|
Valid values
|
Default value
|
---|
autoFlush
|
Determines whether the output buffer should be flushed automatically when it is full.
|
true or false
|
true
|
buffer
|
The output buffer size in kilobytes.
|
Nkb, where N is an integer number."none" is also a valid value.
|
8kb
|
contentType
|
Determines the page's HTTP response MIME type and character encoding.
|
Any valid MIME type and character encoding combination.
|
text/html ; charset= ISO-8859-1
|
deferredSyntaxAll owedAsLiteral
|
In earlier versions of the JSP specification, the #{} syntax for the
expression language was not reserved. For backwards compatibility
purposes, this attribute sets any expressions using this syntax to be
string literals.
|
true or false
|
false
|
errorPage
|
Indicates which page to navigate when the JSP throws an exception.
|
Any valid relative URL to another JSP.
|
N/A
|
extends
|
Indicates the class this JSP extends.
|
The fully qualified name for the JSP's parent class.
|
N/A
|
import
|
Imports one or more classes to be used in scriptlets.
|
A fully qualified name of a class to import or the full package name +
".*" to import all necessary classes from the package (for example,<%@ page import java.util.* %>).
|
N/A
|
info
|
The value for this attribute is incorporated into the compiled JSP. It can later be retrieved by calling the page's getServletInfo() method.
|
Any string.
|
N/A
|
isELIgnored
|
Setting this value to true prevents expression language expressions from being interpreted.
|
true or false
|
false
|
isErrorPage
|
Determines if the page is an error page.
|
true or false
|
false
|
isThreadSafe
|
Determines whether the page is thread safe.
|
true or false
|
true
|
language
|
Determines the scripting language used in scriptlets, declarations, and expressions in the JSP page.
|
Any scripting language that can execute in the Java Virtual Machine (groovy, jruby, and so on).
|
java
|
pageEncoding
|
Determines the page encoding, for example, "UTF-8".
|
Any valid page encoding.
|
N/A
|
session
|
Determines whether the page has access to the HTTP session.
|
true or false
|
true
|
trimDirectiveWhi tespaces
|
When JSPs are rendered as HTML in the browser, the generated markup
frequently has a lot of blank lines in it. Setting this attribute to true prevents these extraneous blank lines from being generated in the markup.
|
true or false
|
false
|
Of the attributes in the table, errorPage, import, and isErrorPage are the most commonly used. Others have sensible defaults.
When deployed to the application server, JSPs are translated into (compiled into) servlets. The extends
attribute of the page directive indicates the generated servlet's
parent class. The value of this attribute must be a subclass of javax.servlet.GenericServlet.
Although the language
attribute can accept any language that can execute in the Java Virtual
Machine, it is extremely rare to use any language other than Java.