Applet
Java applets are programs that are embedded in other applications, typically in a Web page displayed in a web browser.
// Hello.java
import javax.swing.JApplet;
import java.awt.Graphics;
public class Hello extends JApplet {
public void paintComponent(final Graphics g) {
g.drawString("Hello, world!", 65, 95);
}
}
The
import statements direct the
Java compiler to include the
javax.swing.JApplet and
java.awt.Graphics classes in the compilation. The import statement allows these classes to be referenced in the
source code using the
simple class name (i.e.
JApplet) instead of the
fully qualified class name (
FQCN, i.e.
javax.swing.JApplet).
The
Hello class
extends (
subclasses) the
JApplet (Java Applet) class; the
JApplet class provides the framework for the host application to display and control the
lifecycle of the applet. The
JApplet class is a JComponent (Java Graphical Component) which provides the applet with the capability to display a
graphical user interface (GUI) and respond to user
events.
The
Hello class
overrides the
paintComponent(Graphics) method (additionally indicated with the
annotation, supported as of JDK 1.5,
Override) inherited from the
Container superclass to provide the code to display the applet. The
paintComponent() method is passed a
Graphics object that contains the graphic context used to display the applet. The
paintComponent() method calls the graphic context
drawString(String, int, int) method to display the
"Hello, world!" string at a
pixel offset of (
65, 95) from the upper-left corner in the applet's display.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<!-- Hello.html -->
<html>
<head>
<title>Hello World Applet</title>
</head>
<body>
<applet code="Hello.class" width="200" height="200">
</applet>
</body>
</html>
An applet is placed in an
HTML document using the
<applet> HTML element. The
applet tag has three attributes set:
code="Hello" specifies the name of the
JApplet class and
width="200" height="200" sets the pixel width and height of the applet. Applets may also be embedded in HTML using either the
object or
embed element,
[51] although support for these elements by web browsers is inconsistent.
[52] However, the
applet tag is deprecated, so the
object tag is preferred where supported.
The host application, typically a Web browser, instantiates the
Hello applet and creates an
AppletContext for the applet. Once the applet has initialized itself, it is added to the AWT display hierarchy. The
paintComponent() method is called by the AWT
event dispatching thread whenever the display needs the applet to draw itself.
Servlet
Java Servlet
technology provides Web developers with a simple, consistent mechanism
for extending the functionality of a Web server and for accessing
existing business systems. Servlets are
server-side Java EE components that generate responses (typically
HTML pages) to requests (typically
HTTP requests) from
clients. A servlet can almost be thought of as an applet that runs on the server side—without a face.
// Hello.java
import java.io.*;
import javax.servlet.*;
public class Hello extends GenericServlet {
public void service(final ServletRequest request, final ServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
final PrintWriter pw = response.getWriter();
try {
pw.println("Hello, world!");
} finally {
pw.close();
}
}
}
The
import statements direct the Java compiler to include all the public classes and
interfaces from the
java.io and
javax.servlet packages in the compilation. Packages make Java well suited for large scale applications.
The
Hello class
extends the
GenericServlet class; the
GenericServlet class provides the interface for the
server to forward requests to the servlet and control the servlet's lifecycle.
The
Hello class overrides the
service(ServletRequest, ServletResponse) method defined by the
Servlet interface to provide the code for the service request handler. The
service() method is passed: a
ServletRequest object that contains the request from the client and a
ServletResponse object used to create the response returned to the client. The
service() method declares that it
throws the
exceptions ServletException and
IOException if a problem prevents it from responding to the request.
The
setContentType(String) method in the response object is called to set the
MIME content type of the returned data to
"text/html". The
getWriter() method in the response returns a
PrintWriter object that is used to write the data that is sent to the client. The
println(String) method is called to write the
"Hello, world!" string to the response and then the
close()
method is called to close the print writer, which causes the data that
has been written to the stream to be returned to the client.
JavaServer Pages
JavaServer Pages (JSP) are
server-side Java EE components that generate responses, typically
HTML pages, to
HTTP requests from
clients. JSPs embed Java code in an HTML page by using the special
delimiters <% and
%>. A JSP is compiled to a Java
servlet, a Java application in its own right, the first time it is accessed. After that, the generated servlet creates the response.
Swing application
Swing is a graphical user interface
library for the Java SE platform. It is possible to specify a different look and feel through the
pluggable look and feel system of Swing. Clones of
Windows,
GTK+ and
Motif are supplied by Sun.
Apple also provides an
Aqua look and feel for
macOS.
Where prior implementations of these looks and feels may have been
considered lacking, Swing in Java SE 6 addresses this problem by using
more native
GUI widget drawing routines of the underlying platforms.
This example Swing application creates a single window with "Hello, world!" inside:
// Hello.java (Java SE 5)
import javax.swing.*;
public class Hello extends JFrame {
public Hello() {
super("hello");
super.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
super.add(new JLabel("Hello, world!"));
super.pack();
super.setVisible(true);
}
public static void main(final String[] args) {
new Hello();
}
}
The first
import includes all the public classes and interfaces from the
javax.swing package.
The
Hello class
extends the
JFrame class; the
JFrame class implements a
window with a
title bar and a close
control.
The
Hello() constructor initializes the frame by first calling the superclass constructor, passing the parameter
"hello", which is used as the window's title. It then calls the
setDefaultCloseOperation(int) method inherited from
JFrame to set the default operation when the close control on the title bar is selected to
WindowConstants.EXIT_ON_CLOSE – this causes the
JFrame
to be disposed of when the frame is closed (as opposed to merely
hidden), which allows the Java virtual machine to exit and the program
to terminate. Next, a
JLabel is created for the string
"Hello, world!" and the
add(Component) method inherited from the
Container superclass is called to add the label to the frame. The
pack() method inherited from the
Window superclass is called to size the window and lay out its contents.
The
main() method is called by the Java virtual machine when the program starts. It
instantiates a new
Hello frame and causes it to be displayed by calling the
setVisible(boolean) method inherited from the
Component superclass with the boolean parameter
true. Once the frame is displayed, exiting the
main method does not cause the program to terminate because the AWT
event dispatching thread remains active until all of the Swing top-level windows have been disposed.
Generics
In 2004,
generics
were added to the Java language, as part of J2SE 5.0. Prior to the
introduction of generics, each variable declaration had to be of a
specific type. For container classes, for example, this is a problem
because there is no easy way to create a container that accepts only
specific types of objects. Either the container operates on all subtypes
of a class or interface, usually
Object, or a different
container class has to be created for each contained class. Generics
allow compile-time type checking without having to create many container
classes, each containing almost identical code. In addition to enabling
more efficient code, certain runtime exceptions are converted to
compile-time errors, a characteristic known as
type safety.