Saturday, September 24, 2011

J2EE Introduction for beginners

J2EE - Java 2 Enterprise Edition is one of the 3 java platforms, the other being J2SE and J2ME. It is set of specifications or standards consisting of many APIs useful for building java based enterprise applications. At such it is not some software to be installed but is just a bundle of many technologies having set of APIs.


J2EE applications are java applications that span over LAN, WAN and broader. They are used when it comes to distributed environment and disparate systems and also when in need to make use of its technologies.


J2EE technologies are broadly classified into two categories:
1. Component technologies
2. Service technologies


Kinds of J2EE Components:
Client Side Comonents -> includes applets
Web Components -> includes servelets and JSPs
Business Components -> includes EJB
Web components and Business components run on Server Side...


Kinds of J2EE Services:
JNDI -> Java Naming and Directory service
JMS -> java Messaging Service
JTA -> Java Transaction API
JAAS -> Java Authentication and Authorization Service
Java Mail




J2EE is meant for building the 3 layers of java based enterprise application. The layers being:
1. Presentation Layer
2. Service/Business Layer
3. Data Access Layer



We have been talking on java based enterprise application... To clear you on it:
A computer application used to manage business services of an enterprise is called enterprise application and if the application is coded in java and is deployed into J2EE compliant application server then it becomes java based.


Any enterprise application generally covers 4 main tasks:
1. providing user interface(UI) to the user.
2. processing of data according to some business logic(rules)
3. interacting with database(data access)
4. storing of data
These tasks are logically divided into 4 layers
A. Presentation layer
B. Business/Service layer
C. Data Access Layer
D. Data Layer



J2EE developers are responsible for the first 3 layers while the data layer is the responsibility of database administrator(DBA). Hope you are clear on the layers... Remember they are just the logical division. Now coming to actual or physical division...


The physical partition of enterprise application is known as 'tier'. The enterprise application can be single-tier, two-tier, three-tier, n-tier or distributed-tier depending on number of computing systems on which the layers are installed.




Presentation Layer is the user interface part of the application. If the app is web based then Servelets and JSPs are used to build it on server side. The main design patterns used in this layer are Front Controller, Model-View-Controller(MVC), Composite View.


Business/Service Layer is nothing but a code that processes data according to business logic(rules). Some important design patterns used in this layer are Service Locator, Business Delegate, Session Facade. Job of service layer is to receive the business request from presentation layer or some other service layer, communicate with data access layer to get the enterprise data, process that data using defined business rules, return the result to the requested component


Data Access layer is again a code which communicates with the database. It performs CRUD(Create, Read, Update, Delete). Therefore it is just a code communicating with database specific APIs such as JDBC. The design patterns used are Data Access Object(DTO), Data Transfer Object(DTO), Value Object. Can you guess what its job is??? It receives request from service layer, communicates with the database to perform the requested operations and sends the result back to service layer.


So now you clear on J2EE basics.


We already know what is the job of service layer but in order for it to implement it, it needs some capabilities... like transactional capability, distributed computing capability, security implementation capability.
To have these capabilities we make use of J2EE technologies(both components and services). Just scroll up and read the first three paragraphs... This should make it clear for you.

Sunday, September 11, 2011

Tango dance


First time I saw it in the movie "the scent of a women". I was so much influenced by its music. It was like what I have been looking for since my whole life. It is one of the best dance. Although I never danced but I will surely learn it. This is some info regarding Tango if you like it too.

Tango is a dance that has influences from European and African culture.Dances from the candombe ceremonies of former slave peoples helped shape the modern day Tango. The dance originated in lower-class districts of Buenos Aires and Montevideo. The music derived from the fusion of various forms of music from Europe.The word "tango" seems to have first been used in connection with the dance in the 1890s. Initially it was just one of the many dances, but it soon became popular throughout society, as theatres and street barrel organs spread it from the suburbs to the working-class slums, which were packed with hundreds of thousands of European immigrants, primarily Italians, Spanish and French. (Source - Wikipedia)

Here are list of some songs that you can look for :
  1. Libertango
  2. Santa Maria by Project Gotan

Friday, May 27, 2011

JDBC and Microsoft Access

Howdy,

Here another most simple program.

How to make a database connection in Java class and Microsoft Access
There are following simple steps to do:

  1. To connect ms access with your program (of java) you need to create a database in ms access and then create a table in it. make sure that table name and database are different. so that you will not have confusion.
  2. Save the table and then database.
  3. Go to control panel.
  4. Go to Administrative Tools
  5. Click on Data Sources(ODBC)
  6. On User DSN(Data Source Name) tab you can easily see the MS Access Database in User Data Sources.
  7. Select the MS Access Database and click on configure (there will be ODBC Microsoft Access Setup open)
  8. Specify Data Source Name in Text field. The Description field is optional and you can leave it blank.
  9. In Database grid click on "select" and then go to path where database file is saved that was created by you.
  10. After this. Click on "Advanced " button and then specify a log in name and password and then click OK
  11. On System Database grid. click on none. and its done!! tadaaaaaaaa

I made an "employee" name database in MS Access and "emptable" named table which I am using here.
take a look at this.

This is the sample program that i had used for showing the database connectivity for a java program with Microsoft access.

import java.sql.*;
class Dbdemo
{
public static void main(String[] args)
{
try
{
try
{
/*Loading a database driver by calling Class.forName() with the Driver class name as an argument.*/
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(Exception x)
{
System.out.println("Unable to load driver class");
}
/*The getConnection method of DriverManager class returns an open connection you can use to create JDBC statements that pass your SQL statement to DBMS*/
Connection con =
DriverManager.getConnection("jdbc:odbc:MS Access Database","ravi","123");

String query = "select * from emptable";
/*Instantiating the object of Statement interface(as a reference) from Connection class object con by using createStatment() method. this statement object is used to send and execute SQL statements to a database.*/
Statement stm = con.createStatement();
/*executeQuery is a method of Statement Interface for Select queries. this method takes query as an argument.
Variable rs , which is an Instance of ResultSet, contains the details of Employees*/
ResultSet rs = stm.executeQuery(query);
System.out.println("Connection established");
/*ResultSet object maintains a cursor, which points to its current row of data. The next() method moves cursor to next row*/
while(rs.next())
{
/*The getXXX() method can get any any of the SQL types and they are automatically converted to a String.
In getXXX() method XXX represent many types that you can check by
"javap java.sql.ResultSet"*/
String id = rs.getString(1);
String nm = rs.getString(2);
String dept = rs.getString(3);
String sal = rs.getString(4);
System.out.println("epmloyee id: "+id+" employee name: "+nm
+" employee dept: "+dept+" employee salary: "+sal);
}
}catch(Exception e)
{
System.out.println(e);
}
}
}

Wednesday, May 25, 2011

Creating a Servlet without using IDE

Hi,

It is my first blog. My heart felt apologies for the mistakes in the blog
I am demonstrating a program to manually create and run a servlet. I hope the reader is aware of core java. So here we go..
We need following stuff for this :

1.JDK 1.5 or latest version, ( I am here using JDK 1.6)
2. Apache Tomcat (Here we are using Apache Tomcat 6.0.32)

The Following is the code for the TestServlet.java. It simply sends the message to the browser.
I apologise if the code here is not well managed, because i am not so handy with blogging.

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class TestServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throw IOException,ServletException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("Test Servlet ");
}

public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
doGet(request, response);
}
}



After this we need to put the information about our servlet in the web.xml file. Here is what you have to do.

compile your TestServlet.java and put the class file in the following location Apache Software Foundation\apache-tomcat-6.0.32\webapps\examples\WEB-INF\classes

Now update the file web.xml at location Apache Software Foundation\apache-tomcat-6.0.32\webapps\examples\WEB-INF with following information

<servlet>
<servlet-name>TestServlet</servlet-name>
<servlet-class>TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>



The above information maps our servlet class to a URL Pattern , Apache tomcat picks your class file this way.
Voila!!. You have just created a servlet. Now we need to start the tomcat server.

After starting the server, type the url : http://localhost:8080/examples/test

You will see the message on the browser, that we put in the servlet.

if this blogs need any modification, your views are always welcomed.
Ravi