Thursday, April 28, 2011

JSP : Database Connectivity



Why have a chapter about database connectivity in a book on Servlets and
JSP? Databases are widely used in business today. Just about every real-world
application will use databases in some form or another. The advertised mindset
in J2EE is to build an application that uses an EJB layer to hide database con-
nectivity. In practice this can work, but it is often more problematic than it is
helpful. In this book we are taking the approach that you should not have to use
EJB and that instead you should know more about database connectivity. By
taking this approach, you will be able to build complete Web Applications
without having to worry about a slew of other J2EE specifications. However, if
one day you decide those other specifications are needed, you will be more than
prepared for them. In this chapter we will briefly introduce databases and SQL,
but the focus will be on JDBC and successfully using Java to manipulate a
database. You will be expected to pick up a good book on SQL if you want to be
a database guru.
This session we discusses the following topics:
• What a database, relational database, and SQL are.
• CRUD (Creating, Reading, Updating, and Deleting database tables).
• JDBC (Java Database Connectivity).
• javax.sql.DataSource: an abstract method for obtaining a reference
to a database.
• java.sql.Connection: Java representation of a database connection.
• java.sql.Statement: Executing queries against a database.
• java.sql.ResultSet: Obtaining results of a database query
• Optimally using JDBC.
• JDBC Web Application design patterns.






























































jsp:forward

<jsp:forward/>
JSP provide an equivalent to the RequestDispather.forward() method by use of
the forward action. The forward action forwards a request to a new resource and
clears any content that might have previously been sent to the output buffer by
the current JSP. Should the current JSP not be buffered, or the contents of the
buffer already be sent to a client, an IllegalStateException is thrown.

jsp:include


<%@ include %> and <jsp:include />

The include directive is used to include text and/or code at translation time of a
JSP. The include directive always follows the same syntax, <%@ include
file="relativeURL" %>, where the value of relativeURL is replaced with the file
to be inserted. Files included must be part of a Web Application. Since include
directives take place at translation time, they are the equivalent of directly
including the source code in the JSP before compilation and do not result in per-
formance loss at runtime.
Server-side includes are a commonly used feature of JSP. Includes allow the
same repetitious bit of code to be broken out of multiple pages and have one
instance of it included with them all. A good example to use is including a
common header and footer with multiple pages of content. Usually this tech-
nique is used to keep a site’s navigation and copyright information correct and
maintainable for all individual pages on the site.As an example take the following
two files, header.jsp and footer.jsp.
The header.jsp file (fig 1) includes information that is to appear at the
top of a page. It includes site navigation and other miscellaneous information.
This header also tracks how many people have visited the site since the last time
the Web Application was reloaded by reusing code from PageCounter.jsp.
fig 1 header.jsp
<%! int pageCount = 0;
void addCount() {
pageCount++;
}
%>
<% addCount(); %>
<html>
<head>
<title>Header/Footer Example</title>
</head>
<body>
<center>
<h2>Servlets and JSP the J2EE Web Tier</h2>
<a href="http://www.jspbook.com">Book Support Site</a> - 
<a href="ShowSource">Sites Source code</a><br>
This site has been visited <%= pageCount %> times.
</center>
<br><br>


The footer.jsp file (fig 2) includes information that is to appear at
the very bottom of a page. It includes copyright information, disclaimers, and
any other miscellaneous information.
fig 2 footer.jsp
<br><br>
<center>Copyright &copy; 2003
</center>
</body>
</html>
By themselves header.jsp and footer.jsp do not do much, but when com-
bined with some content, a full page can be generated. For this example the
content does not matter. Arbitrarily make up a JSP, but be sure to include
header.jsp at the top of the page and footer.jsp at the bottom. fig 3 pro-
vides an example of such a page.
fig 3 content.jsp
<%@ include file="header.jsp" %>
Only the content of a page is unique. The same header and footer 
are reused from header.jsp and footer.jsp by means of the include 
directive
<%@ include file="footer.jsp" %>


Save all three files, header.jsp, footer.jsp, and  content.jsp, in the base
directory of the jspbook Web Application and browse to http://127.
0.0.1/jspbook/content.jsp. All three files are mashed together at translation
time to produce a Servlet that includes the contents of header.jsp, content.jsp,
and footer.jsp in the appropriate order. Figure shows a browser rendering
of the output.




So what is the advantage of this approach over combining header.jsp,
content.jsp, and footer.jsp all in one file in the first place? As the files are set
up now, many more content pages can be created that reuse the header and
footer. The header and footer can also be changed at any given time, and the
changes are easily reflected across all of the JSP

few more JSP SYNTAX AND SEMANTICS


Directives
Directives are messages to a JSP container. They do not send output to a client,
but are used to define page attributes, which custom tag libraries use and which
other pages include. All directives use the following syntax.
<%@ directive {attribute="value"}* %>
Directives may optionally have extra whitespace after the <%@ and before the
%>. There are three different JSP directives for use on a page page, taglib, and
include.