• Loading
    • How To Deploy Java Applications On A Network?

      The Java programming language enables you to develop and deploy Web-based applications that consist of a client- and a server-side application. You can create a Web-based application using the Graphical User Interface (GUI) controls in Java. The GUI enables a server-side application to interact with clients on a network through the use of controls, such as buttons, frames, and textboxes.

      This document presents an overview of the client-server model and the method of deploying Java applications on a network. It explains how to create GUI controls using Java Application Programming Interfaces (APIs) that are deployed on a server.

      An Overview of the Client-Server Model

      A client-server model consists of multiple clients connected to a server. This model enables you to separate data presentation from internal processing and storage. The data is displayed at the client side, and it is processed and stored at the server side. The client generates requests to access the server and the server accepts the requests made by clients. Following Figure shows the client-server model:-

      Creating and Deploying a GUI Control

      You create a GUI control in Java using a Java API. The Java APIs enable you to create a Java application with various types of controls, such as menus and buttons. The swing package of Java enables you to create text boxes, labels, and drop-down menus. These GUI controls are implemented using the Jcomponent class in the swing package. In addition, you can use the Jcomponent class to implement various other GUI controls, such as frames, buttons, and tabs in a Java application.

      For example, you can create the following Employee Detail form that accepts various employee details, such as name, date of birth, and address.

      import javax.swing.*;
      import java.awt.event.*;
      public class Employee
      // Java Header files that need to be included in a Java application.
          static JButton b1;
          // Buttons that are used to accept or reject the details.
          static JFrame frame;
          // Frame is an object of the JFrame class.
          static JPanel panel;
          // Panel is the object of the JPanel class.
          JLabel lname;
          JLabel lbirth_date;
          JLabel laddress;
          // Variables lname, lbirth_date, and laddress are Jlabel data types and 
          // they are used to create Labels.
          JTextField tname;
          JTextField tbirth_date;
          JTextField taddress;
          // These three statements create Textfields named as tname, tbirth_date, 
          // and taddress. These textfields are used to accept user input.
          public Employee()
              frame = new JFrame();
              panel = new JPanel();
              // Creates new custom controls named as frame and panel. Frame is an object of 
              // the Jframe class and enables you to create a frame in the application. Similarly, 
              // panel is an object of the JPanel class and enables you to add a panel in the application. 
              lname = new JLabel("Employee Name");
              lbirth_date = new JLabel("Date of Birth");
              laddress = new JLabel("Address");
              // These three statements are used to initialize the labels. 
              tname = new JTextField(30);
              tbirth_date = new JTextField(25);
              taddress = new JTextField(60);
              // These three statements are used to initialize the textfields. 
              // add is the method of the JPanel class and is used to add controls for variable, name
              // Adding controls for Date of Birth
              // Adding controls for the field address
              validateAction button1 = new validateAction();
          public static void main(String args[])
              frame = new JFrame("Employee Details");
              b1 = new JButton("Submit");
              // This statement defines the frame named as Employee Details
              Employee objects;
              objects = new Employee();
              frame = new JFrame("Employee Details");
              frame.getContentPane( ).add(panel);
          class validateAction implements ActionListener
              public void actionPerformed(ActionEvent event)
                 Object obj = event.getSource();
                 if(obj ==b1)
                     String emp_name = tname.getText();
                     if(emp_name.length() == 0)
                         String emp = " There is an error !!";

      Deploying Java Applications on a Network

      The java.net package deploys Java applications on a network. The various Java components that enable you to create an interactive Java application and deploy it on a network are: interfaces, classes, and exceptions.

      Defining Interfaces for Java Applications

      Java interfaces enable you to create sockets and set the various options for a socket connection on a network.

      Interface Description
      ContentHandlerFactory Creates a content handler of the Multipurpose Internet Mail Extensions (MIME) type. MIME enables a non-American Standards Code for Information Interchange (ASCII) code, such as pictures, to be transferred on a network.
      DatagramSocketImplFactory Defines the factory methods for socket implementations. Factory methods are static and return an instance value of a class.
      FileNameMap Provides mapping between the file name and the MIME-type strings.
      SocketOptions Sets the socket options, such as selecting the traffic mode.
      URLStreamHandlerFactory Defines a factory for URLStreamHandler for specific protocols, such as Transmission Control Protocol/Internet Protocol (TCP/IP).

      Defining Java Classes for Java Applications

      Java classes enable you to set various options, such as security options, when deploying a Java application on a network. These classes are:

      Authenticator: Creates authentication for the network connection, such as a password, to enable the connection.

      ContentHandler: Enables you to read an object from the URL connection.

      DatagramPacket: Provides connectionless data communication on a network. No socket information is required to route the data on a network. The data is transferred using the packet information.

      DatagramSocketImpl: Implements the socket. This class accepts the port number and connects or disconnects the socket from the connection.

      HttpURLConnection: Represents the support for HyperText Transfer Protocol (HTTP) features.

      InetAddress: Represents the Internet Protocol (IP) address.

      MulticastSockets: Sends and receives multicast IP packets on a network. These packets are data packets transmitted to multiple clients.

      NetPermission: Provides network permissions, such as socket and network permissions defined in security policies in Java.

      ServerSocket: Implements server sockets on a server computer.

      Socket: Implements the client socket on a network.

      SocketAddress: Provides a socket address to the server that has no protocol attached.

      SocketImpl: Implements the Transfer Control Protocol (TCP) socket model in Java.

      SocketPermission: Provides access to a network through the socket.

      Defining Exceptions for Java Application

      Java contains built-in exception types that enable you to track and remove errors when creating a socket. Various types of exceptions provided by the java.net package are listed below:-

      Exceptions Description
      BindException Displays that an error has occurred when binding a socket to a port. Binding a port means initializing a port for a particular protocol.
      ConnectException Displays an error that occurs while connecting a socket to a remote address and a port.
      SocketException Displays an error that occurs during data transmission.
      SocketTimeoutException Displays an error when the time for socket connection has lapses.
      UnknownHostException Displays an error when the IP address for the host cannot be determined.
      UnknownserviceException Displays an error when an unknown service occurs on a network.

      To access Java applications on a network, Java provides various classes and methods for creating and deploying a client-server model. The Java tools that enable a server to communicate with the clients are datagrams and sockets. A datagram is used with the Universal Datagram Protocol (UDP) protocol and a Socket is used with the TCP protocol. The difference between datagrams and socket tools that implement UDP and TCP are listed below:-

      Datagram Socket
      Provides a connection for UDP. Provides a connection for TCP
      Enables short-distance data communication. Enables large-distance data communication.
      Provides less reliability as data gets lost on the network if the traffic is very high. Provides more reliability and security for data on the network.
      Because of the advantages of using sockets over datagrams, sockets are used to establish a connection between the clients and the servers using the TCP protocol.

      Defining a Socket Class in Java

      The Socket class enables you to implement a secure communication between the client and the server. It connects one Java program with another on a network. The Socket class encapsulates the constructor and the methods that provide network connection. The syntax is:

      Socket(String hostname, Int port)

      The above syntax creates a socket that connects the server computer to the client whose name and port number are specified to the server.

      Methods in the Socket Class

      After a socket has been initialized in a constructor, you can examine the socket contents using the following methods:

      • inetAddress getInetAddress(): Returns the address associated with the existing port address.
      • int getPort(): Returns the remote port where the socket object is connected.
      • int getLocalPort(): Returns the local port where the socket object is connected.

      Creating a Client Socket

      You can create a socket to enable a client a connection to the server using the TCP protocol by using the Socket class. The following example illustrates the code to create a client socket:-

      import java.net.*;
      import java.io.*;
      Socket connection;
          connection = new Socket (www.xyz.com,3333);
      catch(IOException e)
      In the above listing, import java.net.* is the Java class that is imported to enable the Socket class for providing a network connection to a Java application. The import java.io.* provides input and output values to a Java application.

      The Socket is the Java-defined class and connection is the user-defined class that connects server xyz.com to port 3333. To terminate a connection to a port, you can use the close() method.

      Reading and Writing to the Client Socket

      You can read and write the data to a socket. Reading and writing to a socket is similar to reading and writing to a data file. The methods you can use to read and write a socket are:

      • getInputstream(): Reads a socket.
      • getOutputstream(): Writes a socket assigned to a port.
      • void close(): Restricts reading and writing to a socket.

      Creating a Server Socket

      You can create a server with the ServerSocket class, which is derived from the Socket class. The ServerSocket class provides constructors to create a socket on a specified port. The syntax is:

      ServerSocket(int port)

      In the above syntax, ServerSocket is the class that specifies the port to be used as the server socket. The queue length of the port is 50.

      The various functions provided by the ServerSocket class are:

      • Listen for a connection made by the client.
      • Return the address of the local port.
      • Return a string representation of a socket.

      Following table lists the methods of the ServerSocket class and their functions:

      Method Description
      accept() Listens for the connection made by the client to the server.
      bind() Binds the ServerSocket to a specific address, which may be the port number or the IP address.
      close() Closes the server socket connection.
      toString() Returns the address of the port as a string.

      Example for creating a server
      Public Server()
              server_socket = new ServerSocket(3333);
      In the above example, the server_socket is a user-defined variable that assigns the port number 3333 to the server.

      Listening to Client’s Request from a Server

      The server listens to the client’s request by invoking the run() method. The following code that enables a server to listen to the client’s request:

      Public void run()
                  Socket  client = server_socket.accept();
                  Connection connect = new Connection(client);
          catch(IOException e)
      In the above listing, the run() method implements the Runnable interface in Java. The accept() method of the ServerSocket class enables the server to listen to the client. Connection is a user-defined class for the client. This class passes the object to the Socket class that enables the connection between the client and the server.

    Disclaimer: Users of techforum4u.com are responsible for ensuring that any material they post (article, blog posts, images or other mulitimedia content) does not violate or infringe upon the copyright, patent, trademark, or any personal or proprietary rights of any third party, and is posted with the permission of the owner of such rights.Anyone who violates these rules may have their access privileges removed without warning.