





















Migration from J2EE to .NET
by vivek devarajan.
Hi all, 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 or Feature | .NET | J2EE |
| GUI | WinForms | SWING & AWT |
| Web GUI | ASP.NET | JSP |
| Web Scripting | ISAPI, HttpHandler, HttpModule | Servlet, Filter |
| Server Side Business Logic Component | Serviced Component (COM+) | EJB Session Beans |
| Server side Data component | Serviced Component (COM+) with DB Logic | EJB BMP Entity Beans |
| Server Side Data Component | Object Spaces | EJB CMP Entity Beans |
| Naming | ADSI | JNDI |
| Remote Invocation | .NET Remoting | RMI or RMI-IIOP |
| Data Access | ADO.NET | JDBC |
| Messaging | MSMQ | JMS |
| Transactions | COM+ or MTS | JTA |
Highlights of the migration strategy
i. Presentation tier migration
JSP scriptlet :--
<!--Scriptlet-->
<%
for(int i = 0; i < 3; i++) {
out.println(i + "<br>");
}
%>ASP .NET scriptlet :--<!--Scriptlet-->
<%
for(int i = 0; i < 3; i++) {
Response.Write(i + "<br>");
}
%>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 :--<script runat="server" language="c#">
public virtual string foo()
{
return "foo";
}
</script>
| JSP | ASP .NET |
| application | Application |
| session | Session |
| request | Request |
| response | Response |
| out | Response.Write |
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);
%>
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{
private string text = "Hello";
public Bean1() {}
public string Text
{
get { return text; }
set { text = value; }
}
}
Servlet sample code :--
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class SimpleServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, java.io.IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println("Simple Servlet Body");
out.println("</body></html>");
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, EventArgs args)
{
Response.ContentType = "text/html";
Response.Write("<html><body>");
Response.Write("Simple Servlet Body");
Response.Write("</body></html>");
}
}
| JSP | ASP .NET |
| Tag Handler Class JSP Tag Library Descriptor JSP Tag Library Directive |
ASP.NET Web User Controls ASP.NET Web Custom Control |
<!?JSP --> <!?the source file is specified in the TLD --> <%@ taglib uri="taglib.tld" prefix="tags" %> <tags:sample />ASP .NET tag lib Sample code :--
<!?ASP.NET --> <%@ Register TagPrefix="tags" TagName="sample" Src="ExampleTag.ascx" %> <tags:sample id="mySample" runat="server" />
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 EventHandler(this.helloWorldControl1_Start);
this.LostFocus +=
new EventHandler(this.helloWorldControl1_Stop);
}
}
ii. Business tier migration
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() {
return "some data";
}
}.NET code :-- (serviced component corresponding to the java session bean)[Transaction(TransactionOption.Required)]
public class TellerBean : System.EnterpriseServices.ServicedComponent
{
public virtual string Data
{
get { return "some data"; }
}
public void Create() {}
protected override void Activate() {}
protected override void Deactivate() {}
protected void Remove {
Deactivate();
Dispose();
}
}EJB Code :-- (Entity Bean)public class AccountEntity implements EntityBean {
private string accountID;
private int balance;
public getAccountID() { ?}
public getBalance() { ?}
public setBalance(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 PRODECURE sp_getAccount @accountID char(8), @balance int, ? AS ..
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
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;";
using(OleDbConnection conn = new OleDbConnection(dbUrl))
{
conn.Open();
...
}
Java :-- (JDBC)
Statement s = conn.createStatement(); createTableBooks = "SELECT count(au_lname) as nrows FROM authors"; ResultSet rs = s.executeQuery(createTableBooks);.NET (ADO.NET)
string cmdText = "SELECT count(au_lname) as nrows FROM authors";
using(OleDbCommand cmd = new OleDbCommand(cmdText, conn))
{
OleDbDataReader rs = cmd.ExecuteReader();
...
}
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。