Java Extensions
 | javax.swing
 | What is JFC and Swing?
 | JFC is a loose collection of APIs for client-side
graphics, graphical user interfaces (GUIs), and other
related tasks. It specifically contains:
 | Abstract Window Toolkit (AWT 1.1 and beyond) |
 | Swing Components ("Swing") |
 | Java2D |
 | Accessibility |
 | Drag and Drop |
|
 | Swing is a set of mostly light-weight components built
on top of AWT. It offers a multitude of components
missing in AWT, plus it has an impressive infrastructure
to build real, industrial-strength GUIs. Thus, it
does not replace, but rather extends the AWT in several
significant ways:
 | Replacement of AWT's heavy-weight components
with light-weight components (except JApplet,
JFrame, JWindow, and JDialog)
 | Transparent backgrounds |
|
 | Pluggable look and feels (plafs) |
 | Full suite of GUI building blocks
 | Buttons, Labels, Lists, and Menus that
can accommodate graphics as well as text |
 | True pop-up windows and tooltips |
 | Dockable toolbars and menu bars |
 | Default Dialog boxes and Color and File
Chooser dialog boxes |
 | Tree, Table, and Tabbed Panes |
 | MDI support |
|
|
|
 | Swing Component Architecture
 | Model-View-Controller (MVC) architecture
 | Model
 | Responsible for maintaining data and
firing events to registered Views when
the model changes |
|
 | View
 | Responsible for providing a visual
representation of some portion of the
model's data |
|
 | Controller
 | Handle events for the views |
|
|
 | Swing actually uses a variation of the MVC architecture
by combining the View and Controller into an UI Delegate
that's responsible for handling events and drawing a
visual representation of the Model |
 | It is Swing's adoption of the MVC architecture that
enables plaf to be easily implemented -- Models don't
depend on Views or Controllers, so just plug a different
View/Controller to a model to change the look and feel |
|
 | Practical Usage of Swing
 | Don't mix heavy weight AWT components with light weight
Swing components |
 | Swing components are not thread-safe, so only modify the
model's data through the event dispatch thread |
 | Add components to the content panes and not to the
containers themselves |
|
 | Live Example 2
 | Build a Java version of Windows® WordPad |
 | Additionally, it can change its look and feel on the fly
JWordPad.java |
|
|
 | javax.servlet
 | What's a Servlet?
 | Servlets are server-side Java applications
 | Most popularly, they are run on web servers to
dynamically generate HTML pages |
|
 | Analogy: Applets run in web browsers. Servelets
run in web servers. |
 | Java Servlet API is a standard Java extension |
 | Lots of web servers support the Servlet API
 | Apache |
 | Jigsaw |
|
|
 | Why use Servlets?
 | Better performance
 | CGI scripts usually use one process per
invocation; better implementations use processes
more efficiently, but Java handles concurrency
much better |
|
 | Better technology
 | ASP (Active Server Pages) offers comparable
features but is a hodge-podge solution from the
early 90's that 's not easily portable |
|
 | Better bargain
 | Servlet extensions into web servers are usually
free! |
|
|
 | Quick Introduction
 | Client Interaction
 | When a servlet accepts a call from a client, it
receives two objects:
 | A ServletRequest ,
which encapsulates the communication
from the client to the server. |
 | A ServletResponse ,
which encapsulates the communication
from the servlet back to the client. |
|
 | ServletRequest and ServletResponse
are interfaces defined by the javax.servlet
package |
|
 | The ServletRequest interface allows the
servlet access to:
 | Information such as the names of the parameters
passed in by the client, the protocol (scheme)
being used by the client, and the names of the
remote host that made the request and the server
that received it. |
 | The input stream, ServletInputStream .
Servlets use the input stream to get data from
clients that use application protocols such as
the HTTP POST and PUT methods. |
Interfaces that extend ServletRequest
interface allow the servlet to retrieve more
protocol-specific data. For example, the HttpServletRequest interface
contains methods for accessing HTTP-specific header
information. |
 | The ServletResponse interface gives
the servlet methods for replying to the client. It:
 | Allows the servlet to set the content length and
MIME type of the reply |
 | Provides an output stream, ServletOutputStream ,
and a Writer through which the
servlet can send the reply data |
Interfaces that extend the ServletResponse
interface give the servlet more protocol-specific
capabilities. For example, the HttpServletResponse interface
contains methods that allow the servlet to manipulate
HTTP-specific header information |
|
 | Live Example 3 |
|
public class SimpleServlet extends HttpServlet {
/**
* Handle the HTTP GET method by building a simple web page.
*/
public void doGet (HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out;
String title = "Simple Servlet Output";
// set content type and other response header fields first
response.setContentType("text/html");
// then write the data of the response
out = response.getWriter();
out.println("<HTML><HEAD><TITLE>");
out.println(title);
out.println("</TITLE></HEAD><BODY>");
out.println("<H1>" + title + "</H1>");
out.println("<P>This is output from SimpleServlet.");
out.println("</BODY></HTML>");
out.close();
}
}
|