Java Programming: Controllers, Servlets, JSPs, and Data Streams

Types of Controllers

There are four different driver bridges or JDBC drivers defined by the JDBC specification from Sun.

Bridge 1

Drivers using the system as a gateway or bridge. One example is the JDBC-ODBC. This is not the best solution because in many cases it is necessary to install specific software on the client, besides being slow to access the database.

Bridge 2

This type of controller is called native API. The driver contains Java code in which calls are made to the native methods of the database in C or C++ made in access to the database. Sometimes it is necessary to install client software to use this type of controller.

Bridge 3

JDBC drivers or drivers of this type are reported with an intermediate application server using sockets that move the client program requests to a driver-specific API. This type of driver has the advantage of not using any software on the client.

Bridge 4

Drivers or drivers of this type use network protocols that are included in the DBMS (Database Management System Data), and therefore the drivers communicate directly with the database, also using Java sockets. It is the best because these drivers are written entirely in Java. Most of these drivers are provided by the manufacturer of your DBMS.

The method createStatement() creates an open channel through which to run queries.

executeQuery(). This method returns a ResultSet object, which can be used to access each of the returned records.

What You Need to Run a Servlet (Also a JSP)

  1. A program for HTTP Web server. They may be the most used or popular as the Apache HTTP server, or IIS that comes with the operating system Windows NT 4.0 or Windows 2000 Server, or even in the professional version of Windows XP.
  2. Servers that support servlets
  3. Servlet classes packages:
    • javax.servlet
    • javax.servlet.http

A web application is a collection of servlets, JSPs, Java classes, description files the application, documents, static HTML, XHTML, images, etc., and other resources that can be packaged and executed on different servers from different vendors. That is, a web application could be defined as the web tier of any application.

The container is that which contains or houses a web application and is only the directory structure in which are placed all the files needed for execution of the application site.

JSP Stages of Evolution

A JSP is going through stages of evolution of three steps in your code:

JSP Source Code

It is written by the programmer or JSP developer. This is a text file with a .jsp extension and consists of a mixture of HTML, Java language instructions, JSP directives and actions that describe how to build a Web page to respond to a request by the client.

Java Source Code

The JSP container translates the JSP source code to source code of a Java servlet equivalent. This source code is stored in a workspace and may be useful in the debugging process.

Compiled Java Class

As with any other Java program, the generated servlet is compiled into byte code (bytecode), resulting in a file. Class that is ready to be loaded and executed by the server.

Types of JSP Elements

There are three types of JSP elements:

  • Items scripts (scripts) that include expressions and statements scriplets.
  • Directives.
  • Actions

Declarations

A statement notifies the Java interpreter that are to define new variables or methods in the generated class file. The statements contain instructions or statements in Java with the following syntax:

<%! sentence; [judgments; … ]%>

Scriptlets

A scriptlet is a set of instructions or statements of Java included in an HTML page. These instructions are different from the HTML because the markers are placed between <% and %> for the JSP interpreter knows to process all the code that is within those tags. Its syntax is:

<% Case, [judgments; …]%>

Request Object

It is an instance of javax.servlet.ServletRequest and contains information of data sent to the server via a web page. For example, the getParameter() method of request object contains values sent through a form or a URL. Thus we can recover the value or contents of the fields in a form on a website, example:

<% String name = request.getParameter(“name”); %>

What is a Flow?

A stream is a communication system implemented in the java.io package designed to store and retrieve information in each of the various storage devices.

The Standard Flow

Java like any other programming language has its own set methods that allow you to capture information on inflows and outflows send by standard devices.

The standard streams are:

  • Input Data Flow:
    • Represented by the System.in class and often receive data from the keyboard
    • Utilizes the method read() to get read keyboard character
  • Output Data Flow:
    • Represented by the System.out class and often sends the data to screen
    • Utilizes the method print() and println() to output to screen
  • Data Flow Error:
    • Represented by the System.err class
    • Ensures that the output is redirected to the monitor to send error messages to the user

Streams that Offers java.io

Java provides two abstract classes to manage the flow of data from remote computers or files and are java.io.InputStream and java.io.OutputStream. Below is the class hierarchy of these classes.

Flows File Access

Whether reading or writing to a file, these flows are handled with file access. In Java we have the FileInputStream class, with the methods needed to open and interact with a communication channel to an input file for our application, and class FileOutputStream for the case of an output file.

The FileInputStream and FileOutputStream classes are in one of its builders as a parameter the name of the file to read or write. There are two variants: one that receives an object of type File and one that receives an object of type FileDescriptor. In the next section we will see the first alternative as the File class is used for this purpose.

The File Class

In the case of the File class that is used to encapsulate the interaction of our programs with the file system. Using the File class we do not just read the file contents, as with the FileInputStream class, but we can obtain additional information such as file size, its type, its creation date, access permissions that we have with him etc..

The class DataOutputStream, is an extension of the OutputStream class, and adds to it the possibility of writing data “complex” in an output stream. When we speak of data as “complex” actually we refer to primitive data type, but not restricted to only bytes and byte arrays, as in the case of OutputStream.

By DataOutputStream class can write data of type int, float, double, char, etc.. We can also write some objects, like String data in a large number of formats.

The Class DataInputStream

If we can write formatted data then we can read comfortably. This feature is possible because we have the class DataInputStream.

The class DataInputStream is designed to read data generated by a DataOutputStream object. The specification of this class ensures that any file written by a DataOutputStream, on any platform and operating system, will be readable by a DataInputStream correctly, without having to worry that if the machines are little-endian “or” big-endian”.