Java Programming History Part-6

Java programming topics
Post Reply
User avatar
shchamon
Sergeant
Sergeant
Posts: 20
Joined: Sat Apr 14, 2012 8:48 pm
Location: gobindogonj, bangladesh.

Java Programming History Part-6

Post by shchamon » Sat Apr 21, 2012 12:48 pm

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.

Code: Select all

// 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 of the public classes and interfaces from the java.io and javax.servlet packages in the compilation.

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 Print Writer object that is used to write the data that is sent to the client. The printing (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.
Java Server Pages
Java Server 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 Mac OS X. 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:

Code: Select all

// 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 of 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 super class 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 a large number of container classes, each containing almost identical code. In addition to enabling more efficient code, certain runtime exceptions are converted to compile-time exceptions, a characteristic known as type safety.
Criticism
............................................................................................................................
A number of criticisms have been leveled at Java for various design choices in the language and platform. Such criticisms include the implementation of generics, the speed of Java, the handling of unsigned numbers, the implementation of floating-point arithmetic, and a history of security vulnerabilities in the primary Java VM implementation Hotspot.
Also, the JIT optimizer comes at a heavy cost of memory usage. Performance benchmarks have showed that although Java can be faster than C. Java applications are on average 4 to 5 times heavier on memory usage because they keep as many as 10 optimized variations of the code in memory.
Use by external companies
............................................................................................................................
Google
Google and Android, Inc. have chosen to use Java as a key pillar in the creation of the Android operating system, an open-source Smartphone operating system. Besides the fact that the operating system, built on the Linux 2.6 kernel, was written largely in Java, the Android SDK uses Java to design applications for the Android platform.
N:B:_see more Java Programming History Part-7
Post Reply

Return to “Java Programming”