Thursday, October 19, 2017

Bean

 http://courses.cs.vt.edu/~cs4244/Notes/Beans/createBean.html

https://javaproglang.blogspot.com/2014/04/developing-simple-bean.html#.X4-CUdUzbIU


Please See Viedo First & Follow it . 



Step 1:  Put this source code into a file named "SimpleBean.java"

import java.awt.*;
import java.io.Serializable;

public class SimpleBean extends Canvas
  implements Serializable{

//Constructor sets inherited properties
  public SimpleBean(){
    setSize(60,40);
    setBackground(Color.red);
  }

}

What will the above code look like when displayed in the bean box?

Step 2:  Compile the file:

    javac SimpleBean.java

Step 3:  Create a manifest file, named "manifest.tmp":


Check other or create path folder in demo/SimpleBean/META-INF/MANIFEST.MF

Manifest-Version: 1.0
Name: FirstBean.class
Java-Bean: True
Created-By: 1.2.2 (Sun Microsystems Inc.)

Step 4:  Create the JAR file, named "SimpleBean.jar":

jar cfM SimpleBean.jar SimpleBean.class

Then verify that the content is correct by the command "jar tf SimpleBean.jar".

Step 5:

  1. Start the Bean Box.
    1. CD to c:\Program Files\BDK1.1\beanbox\.
      Then type "run".
  2. Load JAR into Bean Box by selecting "LoadJar..." under the File menu.

  3.  

Step 6:

  1. After the file selection dialog box is closed, change focus to the "ToolBox" window.  You'll see "SimpleBean" appear at the bottom of the toolbox window.
  2. Select SimpleBean.jar.
  3. Cursor will change to a plus.  In the middle BeanBox window, you can now click to drop in what will appear to be a colored rectangle.

Using Apache Derby (Java DB) from JDBC


 

Tuesday, October 17, 2017

What is the transient keyword in Java?

The transient keyword in Java is used to avoid serialization. If any object of a data structure is defined as a transient, then it will not be serialized.


Serialization is the ​process of converting an object into a byte stream.




Code

In the code below, we have created a Member class object with a transient int id and a simple String name. Since the id is transient,​ it cannot be written in the file, so ​0 is stored instead.


import java.io.Serializable;

import java.io.*;

class TransientExample {

public static class Member implements Serializable{
transient int id; // This will not serialized.
String name;
// constructor
public Member(int i, String k) {
this.id = id;
this.name = k;
}
}

public static void main(String args[]) throws Exception {
Member temp =new Member( 2, "Clausia");//creating object
//writing temp object into file
FileOutputStream fread=new FileOutputStream("member.txt");
ObjectOutputStream fout=new ObjectOutputStream(fread);
fout.writeObject(temp);
fout.flush();

fout.close();
fread.close();
System.out.println("Data successfully saved in a file");

// retrieving the data from file.
ObjectInputStream fin=new ObjectInputStream(new FileInputStream("member.txt"));
Member membr=(Member)fin.readObject();
System.out.println(membr.id+" "+membr.name+" ");
fin.close();
}
}

Output
1.909s

Data successfully saved in a file 0 Clausia

Wednesday, October 11, 2017

Servlet Lab Program 1

 



------- ----------------------            Servlet Configure                   -------------------- -------------------

Copy the file "servlet-api.jar" from location YOUR_INSTILLATION_PATH\tomcat\lib\servlet-api.jar and Paste the file into your Java Directory YOUR_INSTILLATION_PATH\Java\jdk1.8.0_121\jre\lib\ext

this will work (tested).

 

--------------------                 ------------------------------------          Example to config Servlet               ------------------------------

Copy : C:\xampp\tomcat\lib\servlet-api.jar

 

Paste:  C:\Program Files\Java\jdk1.8.0_261\jre\lib\ext\servlet-api.jar

------- ---------------------- ----------------------------- ----------------------------------- -------------------

----- ----------------------------------- ----------------------------- Example 1 -------------------------------------------- ------------------------ ------

C:\xampp\tomcat\webapps\first

//My.java

import javax.servlet.*;

import javax.servlet.http.*;

import java.io.*;

 

public class My extends HttpServlet {

public void service(HttpServletRequest req, HttpServletResponse res) throws

ServletException, IOException{

               PrintWriter p = res.getWriter();

               p.println("hello servlet");

}

}

 

--------------  --------------- Compile ----------------

Class file will store in folder WEB-INF\classes as file name My.class

Javac –d WEB-INF\classes My.java

 

----------- -







 

C:\xampp\tomcat\webapps\first\WEB-INF

// WEB-INF/web.xml

<web-app>

<servlet>

               <servlet-name>My</servlet-name>

               <servlet-class>My</servlet-class>

</servlet>

<servlet-mapping>

               <servlet-name>My</servlet-name>

               <url-pattern>/My</url-pattern>

</servlet-mapping>

</web-app>

----- ----------------------------------- ----------------------------- Example 2 -------------------------------------------- ------------------------ ------

Servlets are Java classes which service HTTP requests and implement the javax.servlet.Servlet interface. Web application developers typically write servlets that extend javax.servlet.http.HttpServlet, an abstract class that implements the Servlet interface and is specially designed to handle HTTP requests.

Sample Code

Following is the sample source code structure of a servlet example to show Hello World −

// Import required java libraries

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

 

// Extend HttpServlet class

public class HelloWorld extends HttpServlet {

 

   private String message;

 

   public void init() throws ServletException {

      // Do required initialization

      message = "Hello World";

   }

 

   public void doGet(HttpServletRequest request, HttpServletResponse response)

      throws ServletException, IOException {

     

      // Set response content type

      response.setContentType("text/html");

 

      // Actual logic goes here.

      PrintWriter out = response.getWriter();

      out.println("<h1>" + message + "</h1>");

   }

 

   public void destroy() {

      // do nothing.

   }

}

 

 

Compiling a Servlet

Let us create a file with name HelloWorld.java with the code shown above. Place this file at C:\ServletDevel (in Windows) or at /usr/ServletDevel (in Unix). This path location must be added to CLASSPATH before proceeding further.

Assuming your environment is setup properly, go in ServletDevel directory and compile HelloWorld.java as follows −

$ javac HelloWorld.java

 

 

If everything goes fine, above compilation would produce HelloWorld.class file in the same directory. Next section would explain how a compiled servlet would be deployed in production.

Servlet Deployment

By default, a servlet application is located at the path <Tomcat-installationdirectory>/webapps/ROOT and the class file would reside in <Tomcat-installationdirectory>/webapps/ROOT/WEB-INF/classes.

If you have a fully qualified class name of com.myorg.MyServlet, then this servlet class must be located in WEB-INF/classes/com/myorg/MyServlet.class.

For now, let us copy HelloWorld.class into <Tomcat-installationdirectory>/webapps/ROOT/WEB-INF/classes and create following entries in web.xml file located in <Tomcat-installation-directory>/webapps/ROOT/WEB-INF/

<servlet>
   <servlet-name>HelloWorld</servlet-name>
   <servlet-class>HelloWorld</servlet-class>
</servlet>
 
<servlet-mapping>
   <servlet-name>HelloWorld</servlet-name>
   <url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>

Above entries to be created inside <web-app>...</web-app> tags available in web.xml file. There could be various entries in this table already available, but never mind.

You are almost done, now let us start tomcat server using <Tomcat-installationdirectory>\bin\startup.bat (on Windows) or <Tomcat-installationdirectory>/bin/startup.sh (on Linux/Solaris etc.) and finally type http://localhost:8080/HelloWorld in the browser's address box. If everything goes fine, you would get the following result

 

 

Monday, October 9, 2017

Java RMI – Java Remote Method Invocation Example

 

1. What is RMI

RMI (Remote Method Invocation) is an object-oriented way of RPC (Remote Procedure Call) to realize communication between distributed objects in a distributed computing environment. It allows an object to invoke methods on a remote object.

Java RMI, a Java implementation of remote method invocation, consists of several APIs under java.rmi package. It allows a Java program running on one Java virtual machine (client) to invoke methods on another Java virtual machine (server). Especially it supports transferring serialized Java objects from machine to machine over the network which makes automatic management of distributed objects come true. With the help of Java RMI, the complexity of working with local and remote objects becomes minimal. The type safety is preserved and distributed garbage collection (DGC) becomes possible.

The diagram below shows the core components of Java RMI and how Java RMI works. We have simplified the diagram and will go through all the technical details in section 3 when building an example.

Java RMI - How Java RMI works
Fig. 1. How Java RMI works

2. When is Java RMI used

From the diagram above we can see that Java RMI is basically a client-server model. It can be used when we want to communicate with different Java virtual machines. These Java virtual machines can be on different hosts or on the same host. In addition to the benefits mentioned in the previous section, one advantage of Java RMI is that we don’t need to re-invent the wheel. The business logic can be implemented and maintained in one place and be re-used in other places. But as the invocation process involves serialization/deserialization and network transport, it is slow and not very reliable. Unexpected errors such as network error could happen during the invocation.

3. Building a simple product information service by using Java RMI

Imagine we have a product inventory for an e-commerce website. The product inventory contains ten thousand products. Each of them has a unique id and name. Our staff needs to look up product information on different devices from different places such as desktops, mobile phones, or handheld terminals. To meet these requirements, we can build a product information server holding the lookup logic and product information clients deployed on different devices to look up product information from the server. Let’s start building it step by step using Java RMI.

3.1 Defining the Contract

In order to communicate between an RMI server and an RMI client, we need to define a contract known to both ends. Java interfaces are used for this purpose. RMI remote interfaces must extend java.rmi.Remote interface. The ProductInfoService interface defines methods we would like to expose to clients remotely. To make things simple, we define one method getProductInfoById(int id) which will return the product info by the given product Id. Note that the method must throw java.rmi.RemoteException.

ProductInfoService.java
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
public interface ProductInfoService extends Remote {
    /**
     * The name used in the RMI registry.
     */
    static final String SERVICE_NAME = "ProductInfoService";
 
    /**
     * Get product info by the given Id.
     *
     * @param id the product id
     * @return a ProductInfo instance
     * @throws RemoteException
     */
    ProductInfo getProductInfoById(int id) throws RemoteException;
}

3.2 Creating the Product Information Server

Once we have defined the contract, we can start building the Product Information server. There are two parts that need to be built: the service implementation and the RMI server.

In our example, the ProductInfoServiceImpl class implements ProductInfoService and extends java.rmi.server.UnicastRemoteObject class. By extending java.rmi.server.UnicastRemoteObject class, the ProductInfoServiceImpl can export a remote object with JRMP (Java Remote Method Protocol) and obtain a stub that communicates to the remote object. Firstly, we define a POJO ProductInfo with two fields: id and name. Note that ProductInfo must implement java.io.Serializable and we need to make sure the ProductInfo class on both server and client sides has the same serialVersionUID. Otherwise serialization and deserialization will fail during the remote invocation.

ProductInfo.java
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
public class ProductInfo implements Serializable {
    // important: make sure the class on both client and server sides have the same value
    private static final long serialVersionUID = 1L;
 
    private int id;
 
    private String name;
 
    /**
     * Constructor.
     *
     * @param id
     * @param name
     */
    public ProductInfo(int id, String name) {
        super();
        this.id = id;
        this.name = name;
    }
 
    /**
     * @return the id
     */
    public int getId() {
        return id;
    }
 
    /**
     * @param id the id to set
     */
    public void setId(int id) {
        this.id = id;
    }
 
    /**
     * @return the name
     */
    public String getName() {
        return name;
    }
 
    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }
 
    @Override
    public String toString() {
        return "ProductInfo [id=" + id + ", name=" + name + "]";
    }
 
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + id;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }
 
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        ProductInfo other = (ProductInfo) obj;
        if (id != other.id)
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }
}

Then in the implementation of getProductInfoById(int id) method, we simply return a new ProductInfo instance with the id and the name.

ProductInfoServiceImpl.java
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class ProductInfoServiceImpl extends UnicastRemoteObject implements ProductInfoService {
 
    private static final long serialVersionUID = 1L;
 
    /**
     * Constructs the service.
     *
     * @throws RemoteException
     */
    protected ProductInfoServiceImpl() throws RemoteException {
        super();
    }
 
    /**
     * Get the product info by the given id.
     *
     * @param id the product id
     * @return a ProductInfo instance
     */
    public ProductInfo getProductInfoById(int id) throws RemoteException {
        return new ProductInfo(id, "Sample Product");
    }
}

The product information server will construct an instance of the ProductInfoService and registers it with the RMI registry. The RMI registry is a separate program shipped with the JDK and you can run it from the command line by typing rmiregistry. It will run at port 1099 by default. For your convenience, we will start the RMI registry programmatically on the localhost at port 1099. The RMI server will run for about 10 seconds waiting for any RMI request from clients. Then it will shut down the RMI registry and quit.

ProductInfoServer.java
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
public class ProductInfoServer {
    /**
     * @param args
     */
    public static void main(String[] args) {
        System.out.println("ProductInfoServer is starting...");
 
        try {
            // create a RMI registry on localhost at port 1099
            Registry registry = LocateRegistry.createRegistry(1099);
 
            System.out.println("RMI registry is running on port 1099");
 
            // create an instance of the service object
            ProductInfoService service = new ProductInfoServiceImpl();
 
            System.out.println("Binding ProductInfoService...");
 
            // bind it in the RMI registry
            registry.rebind(ProductInfoService.SERVICE_NAME, service);
 
            System.out.println("ProductInfoService is ready.");
 
            System.out.println("Wait for 10 seconds for any incoming client call before terminating the RMI registry...");
 
            // sleep 10 seconds
            Thread.sleep(10000);
 
            // unbind the service object
            registry.unbind(ProductInfoService.SERVICE_NAME);
 
            // remove the service object from the registry
            UnicastRemoteObject.unexportObject(service, true);
 
            System.out.println("Shutting down the RMI registry...");
 
            // shut down the registry
            UnicastRemoteObject.unexportObject(registry, true);
 
            System.out.println("ProductInfoServer has stopped.");
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(-1);
        }
    }
}

Compile and run the product information server from the IDE or the command line, we can see the following output on standard output.

1
2
3
4
5
6
7
ProductInfoServer is starting...
RMI registry is running on port 1099
Binding ProductInfoService...
ProductInfoService is ready.
Wait for 10 seconds for any incoming client call before terminating the RMI registry...
Shutting down the RMI registry...
ProductInfoServer has stopped.

3.3 Creating an RMI Client

After the product information server is up and running, how can we use the product information service provided by the server? An RMI client comes into play. We create an RMI client named ProductInfoClient to locate the remote service object and call its method. In the client we use java.rmi.Naming class to obtaining a reference to the ProductInfoService remote object in the RMI registry running on the localhost at port 1099. Then we can simply call the getProductInfoById(int id) method with an id “123” and print the returned product information to standard output.

ProductInfoClient.java
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
public class ProductInfoClient {
    /**
     * @param args
     */
    public static void main(String[] args) {
        try {
            System.out.println("ProductInfoClient> get product info with id '123'...");
            // looks up the registry by service name and returns a stub
            ProductInfoService productInfoService = (ProductInfoService) Naming.lookup(ProductInfoService.SERVICE_NAME);
            // invoke the remote method via the stub
            ProductInfo productInfo = productInfoService.getProductInfoById(123);
            System.out.println("ProductInfoClient> production info received: " + productInfo.toString());
        } catch (Exception e) {
            System.err.println("ProductInfoClient> RemoteDate exception: " + e.getMessage());
            e.printStackTrace();
        }
    }
}

Compile and run the product information client from the IDE or the command line, we can see the following output on standard output.

1
2
ProductInfoClient> get product info with id '123'...
ProductInfoClient> production info received: ProductInfo [id=123, name=Sample Product]

4. Java RMI RemoteException

Since there is a chance of network issues during remote invocations, an exception named RemoteException may occur. If the method invocation results in an exception being thrown, the exception is indicated to the caller. For more information about Java RMI RemoteException and how to handle it properly, please see this example: java.rmi.RemoteException – How to solve RemoteException

5. Download the Source Code

Download
You can download the full source code of this example here: Java RMI – Java Remote Method Invocation Example

CORBA Java Tutorial using Netbeans and Java 8.

CORBA-Example A simple CORBA implementation using Java Echo.idl module EchoApp{ interface Echo{ string echoString(); }; }; ...