Monday, July 18, 2005
j2ee - .NET interest article.
http://www.c-sharpcorner.com/Code/2003/March/J2EEtoDotNet.asp
Here are some Quick tips to get you started, in case you need to migrate a J2EE based application to a .NET based application.
We approach the migration tier wise. Firstly, a technology mapping between both the platforms
Service/Feature .NET J2EE
GUI WinForms SWING / AWT
Web GUI ASP/ASP.NET JSP
Web Scripting ISAPI, HttpHandler Servlet, Filter, HttpModule
Server Side Business Logic Component Serviced Component (COM+) EJB Session Beans
Server Side Data Component Server components with DB logic EJB BMP Entity Beans with DB Logic
Naming ADSI JNDI
Remote Invocation NET Remoting . RMI / RMI-IIOP
Data Access ADO.NET JDBC, SQL/J
Messaging MSMQ JMS
Transactions COM+ / MTS JTA
Highlights of the migration strategy
Presentation Tier JSP -> ASP.NET
Business Logic Tier EJB -> COM+ (.NET Enterprise Service)
Data Access Tier JDO/JDBC -> ADO.NET
1. Presentation tier migration
a). JSP -> ASP .NET (sample code snippets) :--
JSP Scriptlet :--
<%for (int i=0;i<3;i++){
out .println(i+”
" );
}
%>
ASP .NET scriptlet :--
<% for(int i=0; i<3; i++){
Response.Write(i +”
"); }
%>
JSP Expression :--
<%=new java.math.BigDecimal(10.1).negate()%>
ASP .NET Expression :--
<%=System.Decimal.Negate(new System.Decimal(10.1))%>
JSP Declaration :--
<%!
public String foo(){
return "foo";
}
%>
ASP .NET Declaration :--
b). Implicit objects :--
JSP ASP .NET
application Application
session Session
request Request
response Response
out Response.Write
c) Cookies :--
Creating a cookie and setting its expiry time.
Code in J2EE :--
<%
Cookie userCookie = new Cookie("user", "uid123");
userCookie.setMaxAge(60*60*24*365); // 1 year
response.addCookie(userCookie);
%>
Code in .NET :--
<% System.Web.HttpCookie userCookie = new System.Web.HttpCookie("user", "uid123");
System.DateTime dateTime = System.DateTime.Now;
System.TimeSpan timeSpan = new System.TimeSpan(0, 0, 60*60*24*365); // 1 year
cookie.Expires = dateTime.Add(timeSpan);
Response.Cookies.Add(userCookie);
%>
d). Beans :--
Java code :--
public class bean1 {
private String text = "Hello";
public bean1 (){
}
public String getText() {
return this.text;
}
public String setText(String text) {
this.text = text;
}
}
C# Code :--
using System;
public class bean1{
virtual public System.String Text {
get{
return this.text;
}
set{
this.text = value;
}
}
private System.String text = "Hello";
public bean1() {
}
}
e). converting Servlets to ASP .NET code behind :--
Servlet sample code :--
import javax.servlet.*;
import javax.servlet.http.*;
public class SimpleServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException,
java.io.IOException {
response.setContentType("text/html");
java.io.PrintWriter out = response.getWriter();
out.println("");
out.println("Simple Servlet Body");
out.println("");
out.close();
}
}
ASP .NET Code Behind sample :--
using System;
using System.Web;
using System.Web.UI;
public class SimpleServlet : System.Web.UI.Page{
private void Page_Load(object sender,
System.EventArgs
{
Response.ContentType="text/html";
Response.Write("");
Response.Write("Simple Servlet Body");
Response.Write("");
}
}
f). Tag Library :--
JSP ASP .NET
Tag Handler Class ASP.NET Web User Controls
JSP Tag Library Descriptor ASP.NET Web Custom Control
JSP taglib Directive
JSP tag lib Sample code :--
<%@ taglib uri="taglib.tld" prefix=“tags" %>
ASP .NET tag lib Sample code :--
<%@ Register TagPrefix="tags" TagName="sample" src=“ExampleTag.ascx" %>
g). Converting Java Applets to .NET Winforms control
Applet code :--
package HelloWorldPackage;
public class HelloWorld extends Applet
{
public void paint(Graphics g) {
g.drawString(getParameter("parameter1"), 25, 25);
}
public void init() {}
public void start() {}
public void stop() {}
}
.NET Winforms code :--
namespace HelloWorldPackage {
public class HelloWorld :System.Windows.Forms.UserControl{
String parameter1;
bool isActive;
public HelloWorld(){
init();
}
protected override void OnPaint(PaintEventArgs e){
e.Graphics.DrawString(parameter1,Font, new SolidBrush(ForeColor),25,25);
}
public void init(){
this.GotFocus += new System.EventHandler(this.helloWorldControl1_Start);
this.LostFocus += new System.EventHandler(this.helloWorldControl1_Stop);
}
}
2. Business tier migration
Migrating EJB to .NET Serviced Component
a). EJB code :-- (Session bean)
public class TellerBean implements SessionBean {
public void ejbCreate() {}
public void ejbRemove() throws
RemoteException {}
public void ejbActivate() throws RemoteException {}
public void ejbPassivate() throws RemoteException {}
public String getData()
}
.NET code :-- (serviced component corresponding to the java session bean)
[Transaction(TransactionOption.Required)]
public class TellerBean : System.EnterpriseServices.ServicedComponent
{
public void Create() {}
virtual public System.String Data
{ get{…} }
protected override void Activate(){}
protected override void Deactivate(){ }
protected void Remove {
this.Deactivate();
this.Dispose();
}
}
b). EJB Code :-- (Entity Bean)
public class AccountEntity implements EntityBean {
private string accountID;
private int balance;
public get_accountID() { … };
public get_balance() { … };
public set_balance(int amount) { … };
}
public class AccountProcess implements EntityBean {
public AccountEntity[] Inquiry() { … };
public void Insert(AccountEntity account) {
string strQuery = “SELECT * FROM tb_account”;
…
};
public void Update(AccountEntity account) { … };
public void Delete(string accountID) { … };
}
.NET Code :-- (serviced component corresponding to the java entity bean)
[Transaction(TransactionOption.Required)]
public struct AccountEntity {
private string accountID;
private int balance;
public AccountID { get{…}; }
public Balance { get{…}; set {…}; }
}
// dsAccount.xsd
public class dsAccount : System.Data.DataSet {
…
}
public class AccountProcess : System.EnterpriseServices.ServicedComponent {
public dsAccount Inquiry() { … };
public void Insert(AccountEntity account) {
DBAgent.ExecuteNonQuery(“sp_getAccount”, paramArray, …);
…
};
public void Insert(string accountID, int balance, … ){ … }; // Alternative
}
Create Procedure sp_getAccount ( @accountID char(8), @balance int , … )
c). Business Client Tier
Java Code :--
<%
try{
Context ctx = new InitialContext();
Object ref = ctx.lookup("TellerHome");
tellerHome = (TellerHome)
PortableRemoteObject.narrow(ref,
TellerHome.class);
teller = tellerHome.create();
out.println(teller.getData());
}
catch(Exception ex) {
out.println(ex.getMessage());
}
%>
.NET Code :--
<%
myPackage.TellerBean teller = new myPackage.TellerBean();
Response.Write(teller.Data);
%>
III) Data tier migration :--
a). Database connection :--
Java :-- (JDBC)
Class.forName("com.ms.jdbc.odbc.JdbcOdbcDriver");
dbUrl = "jdbc:odbc:ADOTEST";
conn = DriverManager.getConnection(dbUrl,"sa","");
.NET (ADO .NET)
dbUrl = "Provider=SQLOLEDB;Data Source=dbserver;Initial Catalog=Master;"
System.Data.OleDb.OleDbConnection temp_Connection;
temp_Connection = new System.Data.OleDb.OleDbConnection(dbUrl);
temp_Connection.Open();
conn = temp_Connection;
b). Statement object :--
Java :-- (JDBC)
Statement s = conn.createStatement();
createTableBooks = "SELECT count(au_lname) as nrows FROM authors";
ResultSet rs = s.executeQuery(createTableBooks);
.NET (ADO .NET)
System.Data.OleDb.OleDbCommand s = SupportClass.TransactionManager.manager.CreateStatement(conn);
createTableBooks = "SELECT count(au_lname) as nrows FROM authors";
ls.CommandText = createTableBooks;
System.Data.OleDb.OleDbDataReader rs = s.ExecuteReader();
Here are some Quick tips to get you started, in case you need to migrate a J2EE based application to a .NET based application.
We approach the migration tier wise. Firstly, a technology mapping between both the platforms
Service/Feature .NET J2EE
GUI WinForms SWING / AWT
Web GUI ASP/ASP.NET JSP
Web Scripting ISAPI, HttpHandler Servlet, Filter, HttpModule
Server Side Business Logic Component Serviced Component (COM+) EJB Session Beans
Server Side Data Component Server components with DB logic EJB BMP Entity Beans with DB Logic
Naming ADSI JNDI
Remote Invocation NET Remoting . RMI / RMI-IIOP
Data Access ADO.NET JDBC, SQL/J
Messaging MSMQ JMS
Transactions COM+ / MTS JTA
Highlights of the migration strategy
Presentation Tier JSP -> ASP.NET
Business Logic Tier EJB -> COM+ (.NET Enterprise Service)
Data Access Tier JDO/JDBC -> ADO.NET
1. Presentation tier migration
a). JSP -> ASP .NET (sample code snippets) :--
JSP Scriptlet :--
<%for (int i=0;i<3;i++){
out .println(i+”
" );
}
%>
ASP .NET scriptlet :--
<% for(int i=0; i<3; i++){
Response.Write(i +”
"); }
%>
JSP Expression :--
<%=new java.math.BigDecimal(10.1).negate()%>
ASP .NET Expression :--
<%=System.Decimal.Negate(new System.Decimal(10.1))%>
JSP Declaration :--
<%!
public String foo(){
return "foo";
}
%>
ASP .NET Declaration :--
b). Implicit objects :--
JSP ASP .NET
application Application
session Session
request Request
response Response
out Response.Write
c) Cookies :--
Creating a cookie and setting its expiry time.
Code in J2EE :--
<%
Cookie userCookie = new Cookie("user", "uid123");
userCookie.setMaxAge(60*60*24*365); // 1 year
response.addCookie(userCookie);
%>
Code in .NET :--
<% System.Web.HttpCookie userCookie = new System.Web.HttpCookie("user", "uid123");
System.DateTime dateTime = System.DateTime.Now;
System.TimeSpan timeSpan = new System.TimeSpan(0, 0, 60*60*24*365); // 1 year
cookie.Expires = dateTime.Add(timeSpan);
Response.Cookies.Add(userCookie);
%>
d). Beans :--
Java code :--
public class bean1 {
private String text = "Hello";
public bean1 (){
}
public String getText() {
return this.text;
}
public String setText(String text) {
this.text = text;
}
}
C# Code :--
using System;
public class bean1{
virtual public System.String Text {
get{
return this.text;
}
set{
this.text = value;
}
}
private System.String text = "Hello";
public bean1() {
}
}
e). converting Servlets to ASP .NET code behind :--
Servlet sample code :--
import javax.servlet.*;
import javax.servlet.http.*;
public class SimpleServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException,
java.io.IOException {
response.setContentType("text/html");
java.io.PrintWriter out = response.getWriter();
out.println("");
out.println("Simple Servlet Body");
out.println("");
out.close();
}
}
ASP .NET Code Behind sample :--
using System;
using System.Web;
using System.Web.UI;
public class SimpleServlet : System.Web.UI.Page{
private void Page_Load(object sender,
System.EventArgs
{
Response.ContentType="text/html";
Response.Write("");
Response.Write("Simple Servlet Body");
Response.Write("");
}
}
f). Tag Library :--
JSP ASP .NET
Tag Handler Class ASP.NET Web User Controls
JSP Tag Library Descriptor ASP.NET Web Custom Control
JSP taglib Directive
JSP tag lib Sample code :--
<%@ taglib uri="taglib.tld" prefix=“tags" %>
ASP .NET tag lib Sample code :--
<%@ Register TagPrefix="tags" TagName="sample" src=“ExampleTag.ascx" %>
g). Converting Java Applets to .NET Winforms control
Applet code :--
package HelloWorldPackage;
public class HelloWorld extends Applet
{
public void paint(Graphics g) {
g.drawString(getParameter("parameter1"), 25, 25);
}
public void init() {}
public void start() {}
public void stop() {}
}
.NET Winforms code :--
namespace HelloWorldPackage {
public class HelloWorld :System.Windows.Forms.UserControl{
String parameter1;
bool isActive;
public HelloWorld(){
init();
}
protected override void OnPaint(PaintEventArgs e){
e.Graphics.DrawString(parameter1,Font, new SolidBrush(ForeColor),25,25);
}
public void init(){
this.GotFocus += new System.EventHandler(this.helloWorldControl1_Start);
this.LostFocus += new System.EventHandler(this.helloWorldControl1_Stop);
}
}
2. Business tier migration
Migrating EJB to .NET Serviced Component
a). EJB code :-- (Session bean)
public class TellerBean implements SessionBean {
public void ejbCreate() {}
public void ejbRemove() throws
RemoteException {}
public void ejbActivate() throws RemoteException {}
public void ejbPassivate() throws RemoteException {}
public String getData()
}
.NET code :-- (serviced component corresponding to the java session bean)
[Transaction(TransactionOption.Required)]
public class TellerBean : System.EnterpriseServices.ServicedComponent
{
public void Create() {}
virtual public System.String Data
{ get{…} }
protected override void Activate(){}
protected override void Deactivate(){ }
protected void Remove {
this.Deactivate();
this.Dispose();
}
}
b). EJB Code :-- (Entity Bean)
public class AccountEntity implements EntityBean {
private string accountID;
private int balance;
public get_accountID() { … };
public get_balance() { … };
public set_balance(int amount) { … };
}
public class AccountProcess implements EntityBean {
public AccountEntity[] Inquiry() { … };
public void Insert(AccountEntity account) {
string strQuery = “SELECT * FROM tb_account”;
…
};
public void Update(AccountEntity account) { … };
public void Delete(string accountID) { … };
}
.NET Code :-- (serviced component corresponding to the java entity bean)
[Transaction(TransactionOption.Required)]
public struct AccountEntity {
private string accountID;
private int balance;
public AccountID { get{…}; }
public Balance { get{…}; set {…}; }
}
// dsAccount.xsd
public class dsAccount : System.Data.DataSet {
…
}
public class AccountProcess : System.EnterpriseServices.ServicedComponent {
public dsAccount Inquiry() { … };
public void Insert(AccountEntity account) {
DBAgent.ExecuteNonQuery(“sp_getAccount”, paramArray, …);
…
};
public void Insert(string accountID, int balance, … ){ … }; // Alternative
}
Create Procedure sp_getAccount ( @accountID char(8), @balance int , … )
c). Business Client Tier
Java Code :--
<%
try{
Context ctx = new InitialContext();
Object ref = ctx.lookup("TellerHome");
tellerHome = (TellerHome)
PortableRemoteObject.narrow(ref,
TellerHome.class);
teller = tellerHome.create();
out.println(teller.getData());
}
catch(Exception ex) {
out.println(ex.getMessage());
}
%>
.NET Code :--
<%
myPackage.TellerBean teller = new myPackage.TellerBean();
Response.Write(teller.Data);
%>
III) Data tier migration :--
a). Database connection :--
Java :-- (JDBC)
Class.forName("com.ms.jdbc.odbc.JdbcOdbcDriver");
dbUrl = "jdbc:odbc:ADOTEST";
conn = DriverManager.getConnection(dbUrl,"sa","");
.NET (ADO .NET)
dbUrl = "Provider=SQLOLEDB;Data Source=dbserver;Initial Catalog=Master;"
System.Data.OleDb.OleDbConnection temp_Connection;
temp_Connection = new System.Data.OleDb.OleDbConnection(dbUrl);
temp_Connection.Open();
conn = temp_Connection;
b). Statement object :--
Java :-- (JDBC)
Statement s = conn.createStatement();
createTableBooks = "SELECT count(au_lname) as nrows FROM authors";
ResultSet rs = s.executeQuery(createTableBooks);
.NET (ADO .NET)
System.Data.OleDb.OleDbCommand s = SupportClass.TransactionManager.manager.CreateStatement(conn);
createTableBooks = "SELECT count(au_lname) as nrows FROM authors";
ls.CommandText = createTableBooks;
System.Data.OleDb.OleDbDataReader rs = s.ExecuteReader();
