Sunday, February 5, 2023

CORBA Java Tutorial using Netbeans and Java 8.


CORBA-Example

A simple CORBA implementation using Java

Echo.idl

 module EchoApp{  
 interface Echo{  
   string echoString();  
 };  
 };  

EchoServer.java

 import EchoApp.EchoPOA;  
 public class EchoServer extends EchoPOA {  
   @Override  
   public String echoString() {  
     return "Hello World This is sital!!!!!!!";  
   }  
 }  

Server.java
 import EchoApp.Echo;  
 import EchoApp.EchoHelper;  
 import org.omg.CORBA.ORB;  
 import org.omg.CosNaming.NameComponent;  
 import org.omg.CosNaming.NamingContextExt;  
 import org.omg.CosNaming.NamingContextExtHelper;  
 import org.omg.PortableServer.POA;  
 import org.omg.PortableServer.POAHelper;  
 public class Server {  
   public static void main(String args[]) {  
     try{  
       // create and initialize the ORB  
       ORB orb = ORB.init(args, null);  
       // get reference to rootpoa & activate the POAManager  
       POA rootpoa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));  
       rootpoa.the_POAManager().activate();  
       // create servant  
       EchoServer server = new EchoServer();  
       // get object reference from the servant  
       org.omg.CORBA.Object ref = rootpoa.servant_to_reference(server);  
       Echo href = EchoHelper.narrow(ref);  
       org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");  
       NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);  
       NameComponent path[] = ncRef.to_name( "ECHO-SERVER" );  
       ncRef.rebind(path, href);  
       System.out.println("Server ready and waiting ...");  
       // wait for invocations from clients  
       orb.run();  
     }  
     catch (Exception e) {  
       System.err.println("ERROR: " + e);  
       e.printStackTrace(System.out);  
     }  
     System.out.println("Exiting ...");  
   }  
 }  
Client.java 

 import EchoApp.Echo;  
 import EchoApp.EchoHelper;  
 import org.omg.CORBA.ORB;  
 import org.omg.CORBA.ORBPackage.InvalidName;  
 import org.omg.CosNaming.NamingContextExt;  
 import org.omg.CosNaming.NamingContextExtHelper;  
 import org.omg.CosNaming.NamingContextPackage.CannotProceed;  
 import org.omg.CosNaming.NamingContextPackage.NotFound;  
 public class Client {  
   public static void main(String args[]) {  
     try {  
       // create and initialize the ORB  
       ORB orb = ORB.init(args, null);  
       org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");  
       NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);  
       Echo href = EchoHelper.narrow(ncRef.resolve_str("ECHO-SERVER"));  
       String hello = href.echoString();  
       System.out.println(hello);  
     } catch (InvalidName invalidName) {  
       invalidName.printStackTrace();  
     } catch (CannotProceed cannotProceed) {  
       cannotProceed.printStackTrace();  
     } catch (org.omg.CosNaming.NamingContextPackage.InvalidName invalidName) {  
       invalidName.printStackTrace();  
     } catch (NotFound notFound) {  
       notFound.printStackTrace();  
     }  
   }  
 }  

Instructions

1. Write the IDL file

The IDL file defines the interface that will be used by the client and server for communicating and passing objects.
When the IDL file gets compiled, it will produce a number of files, known as the stub and skeleton:

  • The stub is used by the client to communicate with the server
  • The skeleton is used by the server to communicate with the client
  • The stub and skeleton communicate with the ORB server to facilitate the remote procedure call

The module in the IDL file will correspond to the package and directory in which the Java code will be generated

Echo.idl

module EchoApp {
    interface Echo {
        string echoString();
    };
};

2. Generate the stub and skeleton code

There is an idlj program that comes with the JDK for generating the stub and skeleton code in Java IN COMMAND PROMPT "CMD" open 3 command

“D:\CORBA\src>"C:\Program Files\Java\jdk1.8.0_261\bin\idlj" -fall Echo.idl”

OR

idlj –fall Echo.idl

The following files are generated by the idlj program:

  • _EchoStub.java
  • Echo.java
  • EchoHelper.java
  • EchoHolder.java
  • EchoOperations.java
  • EchoPOA.java

3. Write the server code

The server program will inherit from the EchoPOA class that was generated as part of the idlj program.
The EchoPOA class implements the EchoOperations interface

  • This interface contains the methods we defined in the Echo.idl file, but standardized to Java.

We create an EchoServer.java class that extends the abstract EchoPoa class and then implement the methods contained in it

We create a main method in Server.java to communicate with the object request broker (ORB), registering the server with the ORB so clients are able to find it.

4. Write the client code

The client program Client.java needs to get a reference to the ORB then resolve the name of the server object it would like to invoke.

  • This is ECHO-SERVER in this case

After getting an instance of a Server object from the server, it can invoke methods on it just like a method within its own JVM.

5. Compile the code

  1. Compile the stub and skeleton from the directory that contains the IDL file.

    Windows

    javac EchoApp\*.java

    Linux

    javac EchoApp/*.java
  2. Generate a JAR file from the compiled stub and skeleton.

    Windows

    jar cvf echoapp.jar EchoApp\*.class

    Linux

    jar cvf echoapp.jar EchoApp/*.class
  3. Compile the server and client classes

    Windows

    javac -classpath .;echoapp.jar Server.java EchoServer.java Client.java

    Linux

    javac -classpath .:echoapp.jar Server.java EchoServer.java Client.java

6. Running the application

  1. Start the ORB server

    orbd -ORBInitialPort 1050 -ORBInitialHost localhost
  2. Start the server application

    java Server -ORBInitialPort 1050 -ORBInitialHost localhost
  3. Start the client application

    java Client -ORBInitialPort 1050 -ORBInitialHost localhost

If everything was compiled correctly the output should be:

Hello World!!!!!!!


https://github.com/akmalzz/DC_Assignments/tree/master/3%5BCORBA%5D
http://www.ejbtutorial.com/corba/tutorial-for-corba-hello-world-using-java
https://www.youtube.com/watch?v=LRzOwf8k3NM
https://github.com/johnngugi/CORBA-Example
https://www.youtube.com/watch?v=LRzOwf8k3NM

JSP implicit objects

 JSP implicit objects are created by JSP Engine during translation phase (while translating JSP to Servlet). They are being created inside service method so we can directly use them within Scriptlet without initializing and declaring them. There are total 9 implicit objects available in JSP.

Implicit Objects and their corresponding classes:

IMPLICIT OBJECTCLASS
outjavax.servlet.jsp.JspWriter
requestjavax.servlet.http.HttpServletRequest
responsejavax.servlet.http.HttpServletResponse
sessionjavax.servlet.http.HttpSession
applicationjavax.servlet.ServletContext
exceptionjavax.servlet.jsp.JspException
pagejava.lang.Object
pageContextjavax.servlet.jsp.PageContext
configjavax.servlet.ServletConfig

9 Implicit objects in JSP

  1. Out: This is used for writing content to the client (browser). It has several methods which can be used for properly formatting output message to the browser and for dealing with the buffer.
    Read full article here » OUT implicit object with examples.
  2. Request: The main purpose of request implicit object is to get the data on a JSP page which has been entered by user on the previous JSP page. While dealing with login and signup forms in JSP we often prompts user to fill in those details, this object is then used to get those entered details on an another JSP page (action page) for validation and other purposes.
    Read full article here » Request implicit object with examples.
  3. Response: It is used for modifying or dealing with the response which is being sent to the client(browser) after processing the request.
    Read full article here » Response implicit object with examples.
  4. Session: It is most frequently used implicit object, which is used for storing the user’s data to make it available on other JSP pages till the user session is active.
    Read full article here » Session implicit object with examples.
  5. Application: This is used for getting application-wide initialization parameters and to maintain useful data across whole JSP application.
    Read full article here » Application implicit object with examples.
  6. Exception: Exception implicit object is used in exception handling for displaying the error messages. This object is only available to the JSP pages, which has isErrorPage set to true.
    Read full article here » Exception implicit object with examples.
  7. Page: Page implicit object is a reference to the current Servlet instance (Converted Servlet, generated during translation phase from a JSP page). We can simply use this in place of it. I’m not covering it in detail as it is rarely used and not a useful implicit object while building a JSP application.
  8. pageContext: It is used for accessing page, request, application and session attributes.
    Read full article here » pageContext implicit object with examples.
  9. Config: This is a Servlet configuration object and mainly used for accessing getting configuration information such as servlet context, servlet name, configuration parameters etc.
    Read full article here » Config implicit object with examples.

What is JSP Implicit object?

  • JSP implicit objects are created during the translation phase of JSP to the servlet.
  • These objects can be directly used in scriplets that goes in the service method.
  • They are created by the container automatically, and they can be accessed using objects.

How many Implicit Objects are available in JSP?

There are 9 types of implicit objects available in the container:

  1. Out
  2. Request
  3. Response
  4. Config
  5. Application
  6. Session
  7. PageContext
  8. Page
  9. Exception

Lets study One By One

Out

  • Out is one of the implicit objects to write the data to the buffer and send output to the client in response
  • Out object allows us to access the servlet’s output stream
  • Out is object of javax.servlet.jsp.jspWriter class
  • While working with servlet, we need printwriter object

Example:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Implicit Guru JSP1</title>
</head>
<body>
<% int num1=10;int num2=20;
out.println("num1 is " +num1);
out.println("num2 is "+num2);
%>
</body>
</html>

Explanation of the code:

Code Line 11-12– out is used to print into output stream

When we execute the above code, we get the following output:

JSP Actions: JSP Implicit objects,Page directive, Include directive, Taglib Directive

Output:

  • In the output, we get the values of num1 and num2

Request

  • The request object is an instance of java.servlet.http.HttpServletRequest and it is one of the argument of service method
  • It will be created by container for every request.
  • It will be used to request the information like parameter, header information , server name, etc.
  • It uses getParameter() to access the request parameter.

Example:

Implicit_jsp2.jsp(form from which request is sent to guru.jsp)

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Implicit Guru form JSP2</title>
</head>
<body>
<form action="guru.jsp">
<input type="text" name="username">
<input type="submit" value="submit">
</form>
</body>
</html>

Guru.jsp (where the action is taken)

JSP Actions: JSP Implicit objects,Page directive, Include directive, Taglib Directive

Explanation of code:

Code Line 10-13 : In implicit_jsp2.jsp(form) request is sent, hence the variable username is processed and sent to guru.jsp which is action of JSP.

Guru.jsp

Code Line10-11: It is action jsp where the request is processed, and username is taken from form jsp.

When you execute the above code, you get the following output

Output:

JSP Actions: JSP Implicit objects,Page directive, Include directive, Taglib Directive

When you write test and click on the submit button, then you get the following output “Welcome Test.”

JSP Actions: JSP Implicit objects,Page directive, Include directive, Taglib Directive

Response

  • “Response” is an instance of class which implements HttpServletResponse interface
  • Container generates this object and passes to _jspservice() method as parameter
  • “Response object” will be created by the container for each request.
  • It represents the response that can be given to the client
  • The response implicit object is used to content type, add cookie and redirect to response page

Example:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Implicit Guru JSP4</title>
</head>
<body>
<%response.setContentType("text/html"); %>
</body>
</html>

Explanation of the code:

Code Line 11: In the response object we can set the content type

Here we are setting only the content type in the response object. Hence, there is no output for this.

Config

  • “Config” is of the type java.servlet.servletConfig
  • It is created by the container for each jsp page
  • It is used to get the initialization parameter in web.xml

Example:

Web.xml (specifies the name and mapping of the servlet)

JSP Actions: JSP Implicit objects,Page directive, Include directive, Taglib Directive

Implicit_jsp5.jsp (getting the value of servlet name)

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Implicit Guru JSP5</title>
</head>
<body>
<% String servletName = config.getServletName();
out.println("Servlet Name is " +servletName);%>
</body>
</html>

Explanation of the code:

In web.xml

Code Line 14-17: In web.xml we have mapping of servlets to the classes.

Implicit_jsp5.jsp

Code Line 10-11: To get the name of the servlet in JSP, we can use config.getServletName, which will help us to get the name of the servlet.

When you execute the above code you get the following output:

JSP Actions: JSP Implicit objects,Page directive, Include directive, Taglib Directive

Output:

  • Servlet name is “GuruServlet” as the name is present in web.xml

Application

  • Application object (code line 10) is an instance of javax.servlet.ServletContext and it is used to get the context information and attributes in JSP.
  • Application object is created by container one per application, when the application gets deployed.
  • Servletcontext object contains a set of methods which are used to interact with the servlet container.We can find information about the servlet container

Example:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Guru Implicit JSP6</title>
</head>
<body>
<% application.getContextPath(); %>
</body>
</html>

Explanation of the code:

  • In the above code, application attribute helps to get the context path of the JSP page.

Session

  • The session is holding “httpsession” object(code line 10).
  • Session object is used to get, set and remove attributes to session scope and also used to get session information

Example:

Implicit_jsp7(attribute is set)

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Implicit JSP</title>
</head>
<body>
<% session.setAttribute("user","GuruJSP"); %>
<a href="implicit_jsp8.jsp">Click here to get user name</a>
</body>
</html>

Implicit_jsp8.jsp (getAttribute)

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>implicit Guru JSP8</title>
</head>
<body>
<% String name = (String)session.getAttribute("user");
out.println("User Name is " +name);
%>
</body>
</html>

Explanation of the code:

Implicit_jsp7.jsp

Code Line 11: we are setting the attribute user in the session variable, and that value can be fetched from the session in whichever jsp is called from that (_jsp8.jsp).

Code Line 12: We are calling another jsp on href in which we will get the value for attribute user which is set.

Implicit_jsp8.jsp

Code Line 11: We are getting the value of user attribute from session object and displaying that value

When you execute the above code, you get the following output:

JSP Actions: JSP Implicit objects,Page directive, Include directive, Taglib Directive

When you click on the link for the username. You will get the following output.

JSP Actions: JSP Implicit objects,Page directive, Include directive, Taglib Directive

Output:

  • When we click on link given in implicit_jsp7.jsp then we are redirected to second jsp page, i.e (_jsp8.jsp) page and we get the value from session object of the user attribute (_jsp7.jsp).

PageContext

  • This object is of the type of pagecontext.
  • It is used to get, set and remove the attributes from a particular scope

Scopes are of 4 types:

  • Page
  • Request
  • Session
  • Application

Example:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Implicit Guru JSP9</title>
</head>
<body>
<% pageContext.setAttribute("student","gurustudent",pageContext.PAGE_SCOPE);
String name = (String)pageContext.getAttribute("student");
out.println("student name is " +name);
%>
</body>
</html>

Explanation of the code:

Code Line 11: we are setting the attribute using pageContext object, and it has three parameters:

  • Key
  • Value
  • Scope

In the above code, the key is student and value is “gurustudent” while the scope is the page scope. Here the scope is “page” and it can get using page scope only.

Code Line 12: We are getting the value of the attribute using pageContext

When you execute the above code, you get the following output:

JSP Actions: JSP Implicit objects,Page directive, Include directive, Taglib Directive

Output:

  • The output will print “student name is gurustudent”.

Page

  • Page implicit variable holds the currently executed servlet object for the corresponding jsp.
  • Acts as this object for current jsp page.

Example:

In this example, we are using page object to get the page name using toString method

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Implicit Guru JSP10</title>
</head>
<body>
<% String pageName = page.toString();
out.println("Page Name is " +pageName);%>
</body>
</html>

Explanation of the code:

Code Line 10-11: In this example, we are trying to use the method toString() of the page object and trying to get the string name of theJSP Page.

When you execute the code you get the following output:

JSP Actions: JSP Implicit objects,Page directive, Include directive, Taglib Directive

Output:

  • Output is string name of above jsp page

Exception

  • Exception is the implicit object of the throwable class.
  • It is used for exception handling in JSP.
  • The exception object can be only used in error pages.Example:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" isErrorPage="true"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Implicit Guru JSP 11</title>
</head>
<body>
<%int[] num1={1,2,3,4};
out.println(num1[5]);%>
<%= exception %>
</body>
</html>

Explanation of the code:

Code Line 10-12 – It has an array of numbers, i.e., num1 with four elements. In the output, we are trying to print the fifth element of the array from num1, which is not declared in the array list. So it is used to get exception object of the jsp.

Output:

JSP Actions: JSP Implicit objects,Page directive, Include directive, Taglib Directive

We are getting ArrayIndexOfBoundsException in the array where we are getting a num1 array of the fifth element.

https://www.guru99.com/jsp-implicit-objects.html

CORBA Java Tutorial using Netbeans and Java 8.

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