Looking to Reprint this content?
Cover | Table of Contents | Colophon
http://www.oreillynet.com
http://www.oreilly.com/catalog/javaebp).
http://java.sun.com, which can also be reached
by the now deprecated http://www.javasoft.com. Another web site
specifically for Java and J2EE developers is the Java Developer
Connection: http://developer.java.sun.com. Most
of the technical articles and beta software on this developer site is
password-protected, and access to it requires registration. However,
registration is free, and you can allow the site to automatically log
in through the use of cookies.
http://java.oreilly.com and
http://www.onjava.com. These sites contain links
to our catalog of latest books, as well as insightful tips and tricks
for every level of Java programmer. Some other useful sites that you
can access are those for Javaworld magazine, at
http://www.javaworld.com;
JavaPro magazine, at
http://www.javapro.com; and IBM
developerWorks, at http://www.ibm.com/developerworks.
http://java.sun.com/products and find
the appropriate API.
RemoteException,
you are also forced to do this in your business interface. Also, a
minor inconvenience is that all method parameters must be
Serializable
.
http://jakarta.apache.org/ant/index.html.
HttpSession and
SingleThreadModel features. As we near the end of
the chapter, I explain how to reliably control caching to improve the
user's experience. Then I address the frequently
asked question: "How do I download a file to the
client so that the client sees a `Save
As' pop up?" As
you'll see, the answer lies in setting the right
HTTP headers.
out.println( ).
HttpSession and
SingleThreadModel features. As we near the end of
the chapter, I explain how to reliably control caching to improve the
user's experience. Then I address the frequently
asked question: "How do I download a file to the
client so that the client sees a `Save
As' pop up?" As
you'll see, the answer lies in setting the right
HTTP headers.
out.println( ).
out.println(
) that came before. These technologies make it easier than
ever before to quickly develop, deploy, and maintain dynamic web
content. You can find a full discussion of these and other templating
technologies in my book // Set the headers.
res.setContentType("application/x-download");
res.setHeader("Content-Disposition", "attachment; filename=" + filename);
// Send the file.
OutputStream out = res.getOutputStream( );
returnFile(filename, out); // Shown earlier in the chapter
Content-Type
header to a nonstandard value such as
application/x-download. It's very
important that this header is something unrecognized by browsers
because browsers often try to do something special when they
recognize the content type. Then set the
|
Driver
|
Description
|
|---|---|
|
Type 1
|
A bridge between JDBC and another database-independent API, such as
ODBC. The JDBC-ODBC driver that comes with the Java SDK is the
primary example of a Type 1 driver.
|
|
Type 2
|
Translates JDBC calls into native API calls provided by the database
vendor.
|
|
Driver
|
Description
|
|---|---|
|
Type 1
|
A bridge between JDBC and another database-independent API, such as
ODBC. The JDBC-ODBC driver that comes with the Java SDK is the
primary example of a Type 1 driver.
|
|
Type 2
|
Translates JDBC calls into native API calls provided by the database
vendor.
|
|
Type 3
|
Network bridges that enable an application to take advantage of the
WORA (write once, run anywhere) capabilities of Type 4 drivers, even
when your database of choice supports only Type 2 drivers.
|
|
Type 4
|
Talks directly to a database using a network protocol. Because it
makes no native calls, it can run on any Java Virtual Machine (JVM).
|
PersonBean entity bean contains no JDBC code or
other database logic. Instead, it delegates all persistence
operations to a generic persistence API. Underneath the covers, an
implementation of that API performs the actual mapping of bean values
to the database through the data access object. In fact, the
implementation could persist to any entirely different data storage
technology, such as an object database or directory service, without
the entity bean being any wiser.
Statement, PreparedStatement,
and CallableStatement. All too often, however,
discussions of which kind of statement to use focus purely on
performance. That's not to say that the choice of
statement class doesn't impact performance. As a
general rule, CallableStatement instances based on
database-stored procedures provide the best performance, with
PreparedStatement instances close behind. Finally,
Statement instances generally perform
significantly worse than the other kinds of statements. Focusing
purely on performance, however, disguises two important facts:
CallableStatement and
PreparedStatement is generally negligible.
Statement gives you optimal performance.
Statement-based
calls,
the driver sends the SQL to the database, which parses it every time
you execute the statement. Calls through a
PreparedStatement (as the name implies) are
"prepared" before they are
executed. In other words, the driver sends the SQL to the database
for parsing when the statement is created but before it is executed.
By the time you call
execute( ), the
statement has been preparsed by the database. And if
you're truly lucky, the same SQL has already been
executed, and no parsing even needs to occur. Finally, a
WHERE clauses will do more for
your application performance than anything you can do in Java. A good
way to find out what is happening with your SQL code is to use your
database engine's command-line utility and run the
SQL through the EXPLAIN SELECT
command.
EXPLAIN
SELECT command provides is database-dependent.
Whatever your database, it should tell you some basic things about
how it is trying to execute your query. Is the query utilizing
indexes fully, or is it doing multiple table scans for what should be
a simple query?
static final variable in Java in that it cannot
alter its value from an initial value defined in a Document Type
Definition (DTD).
static final variable in Java in that it cannot
alter its value from an initial value defined in a Document Type
Definition (DTD).
<!ENTITY phoneNumber "800-775-7731">
<content>O'Reilly's phone number is &phoneNumber;.</content>
<page>
<title>O'Reilly Java Enterprise Best Practices</title>
<content type="html">
<center><h1>O'Reilly Java Enterprise Best Practices</h1></center>
<p>
Welcome to the website for <i>O'Reilly Java Enterprise Best
Practices</i>. This book was written by O'Reilly's Java
authors for Java Enterprise professionals. And so on and
so on, ad infinitum.
</p>
</content>
</page>org.xml.sax.InputSource class. This is a class
that allows the specification of an input (e.g., a file or I/O
stream), as well as a public and system ID. SAX then extracts this
information from the InputSource at parse time and
is able to resolve external entities and other document
source-specific resources.
InputSource class even when
you do not. Consider the code fragment in Example 5-6, which uses JAXP to initiate a SAX parse.
import java.io.*; import java.xml.parsers.*; File myFile = ... DefaultHandler myHandler = ... SAXParserFactory spf = SAXParserFactory.newInstance( ); SAXParser parser = spf.newSAXParser( ); parser.parse(myFile, myHandler);
Transformer instances and the JAXP
TransformerFactory
.
package com.oreilly.xml;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
public class FormatConverter {
private static boolean initialized = false;
private static Transformer transformer;
private static void initialize( ) throws TransformerException {
TransformerFactory factory = TransformerFactory.newInstance( );
transformer = factory.newTransformer( );
initialized = true;
}
public static void convert(StreamSource source, SAXResult result)
throws TransformerException {
transformer.transform(source, result);
}
public static void convert(StreamSource source, DOMResult result)
throws TransformerException {
transformer.transform(source, result);
}
public static void convert(SAXSource source, DOMResult result)
throws TransformerException {
transformer.transform(source, result);
}
public static void convert(SAXSource source, StreamResult result)
throws TransformerException {
transformer.transform(source, result);
}
public static void convert(DOMSource source, StreamResult result)
throws TransformerException {
transformer.transform(source, result);
}
public static void convert(DOMSource source, SAXResult result)
throws TransformerException {
transformer.transform(source, result);
}
static {initialize();}
}RemoteException
,
as in the following code snippet:
public vo