JSP
INTRODUCTION TO JSP
INTRODUCTION TO JSP
INTRODUCTION TO JSP
The namespace name for JSP itself is [Link]
while [Link] is the name of the core namespace
of the JSP Standard Tag Library (JSTL)
A JSP tag library is a mechanism for incorporating functionality in a JSP
document beyond that defined by the JSP standard itself. The JSP
Standard Tag Library is included as part of the JWSDP 1.3 version of
the Tomcat 5.0 distribution and provides a number of useful functions
The JSP [Link] and output elements can be used for setting certain
properties of the HTML document that will be produced by a JSP
document. In this case, the [Link] element directs the server to set
the Content-Type header field of the HTTP response to the text/html
MIME type.
the output element causes the XML declaration to be suppressed in the
XHTML document produced and provides all of the content for the
document type declaration: the name of the root element along with the
public and system identifiers
JSP AND SERVLETS
every JSP document is translated into a Java servlet, which is then
compiled. This translation and compilation normally occurs the
first time the JSP document’s URL is requested. Tomcat places
translated JSP documents and their class files under the work
subdirectory of the JWSDP 1.3 installation directory.
work/Catalina/localhost/HelloCounter/org/apache/jsp/HelloCount
er [Link]. After translating and compiling a JSP document, the
server then executes the resulting servlet class to produce the
response to the request.
Depending on how the server is configured, if a revised JSP
document is placed in the web application directory, then the next
time the JSP document is visited, the translation and compilation
process may be automatically performed again before executing
the servlet to produce a response.
MERITS AND DEMERITS
One disadvantage is in debugging: if a servlet translated from a
JSP document throws an exception to the server, the exception
will refer to the servlet code and not to the original JSP document
There can also be a noticeable delay the first time a JSP document
is requested.
The primary advantage is that, after the first access to a JSP
document, subsequent accesses tend to be relatively fast because
they are running a compiled program rather than causing
interpretation of a text file.
The translator creates a subclass of HttpServlet that also
implements the [Link] interface. This
interface includes a method jspService() that is called by the
server rather than doGet() or doPost() when an HTTP request
for the JSP document is received. The jspService() method
begins by defining a number of implicit objects that are
available to scriptlet code embedded within the document.
SOME JAVA IMPLICIT OBJECTS AUTOMATICALLY
CREATED WHEN A JSP DOCUMENT IS TRANSLATED
INTO A SERVLET
JSP EL
JSP DIRECTIVE ATTRIBUTES
JSTL CORE ACTIONS
JAVABEAN
the class must have a public no-argument
constructor (which may be the constructor that is
supplied automatically by Java.
getWelcome() method conforms with the getter
simple property design pattern, which requires that
the method be public, return a value.
attempts to retrieve the value of welcome,
getWelcome() is called automatically and the value
returned by this method is used as the value of the
welcome bean property.
A tag file, in turn, is one of several JSP
mechanisms for defining a custom action. A
collection of definitions of custom actions is
known as a JSP tag library
<%@ page import="[Link].*" %>
<html>
<head><title>Database Connection Example</title></head>
<body>
<%
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
// Load JDBC Driver
[Link]("[Link]");
// Establish Connection
con = [Link](
"jdbc:mysql://localhost:3306/DatabaseName", "username", "password");
// Create Statement
stmt = [Link]();
// Execute Query
rs = [Link]("SELECT * FROM users");
%>
</body>
</html>
// Process Result Set
while([Link]()) {
[Link]("Name: " + [Link]("name") + "<br>");
}
} catch (ClassNotFoundException e) {
[Link]("Driver not found: " + [Link]());
} catch (SQLException e) {
[Link]("SQL Error: " + [Link]());
} finally {
// Close resources
try {
if (rs != null) [Link]();
if (stmt != null) [Link]();
if (con != null) [Link]();
} catch (SQLException e) {
[Link]("Closing Error: " + [Link]());
}
}