Posts Tagged J2EE

J2EE – JNDI

To understand JNDI, the only thing you need to learn is naming.

Very simply example,

DNS is a naming convention which is used to map the domain to IP. Such as www.test.com – 9.161.142.75.

Another difficult thing to understand is LDAP (Lightweight Directory Access Protocol). most likely you will see it from the product where you have database storing all the users and you also have a LDAP server mapping all ther users as well (very popular usage).

Thus the LDAP name fn=Shengjie Min, p=SiChuan, c=CN names an LDAP entry fn=Shengjie Min, relative to the entry P=SiChuan,which in turn, is relative to C= CN.

So , back to the JNDI topic. JNDI allows us to look up any java object or LDAP directory based on the context. Hmm… not following..

Ok, one step by another,

1. Bindings

The association of a name with an object is called a binding. For example, a file name is bound to a file.

The DNS contains bindings that map machine names to IP addresses. An LDAP name is bound to an LDAP entry.

2. Context

A context is a set of name-to-object bindings

Examples are better than anything :) That’s why i like them.

eg. you have a few database source(JDBC), let’s say they are a few databases. The context would be those databases entries. And to find one of the specific data source out of them, you will need to do a look up among those entries(context).

Given a code snippet to help out:

Context initCtx = new InitialContext(); // actually, to create a brand new initialContext, which can take hashmap as argument, you construct the hashmap to contain name-value pair. Just think of it as defining the scope of the look up

Context envCtx = (Context) initCtx.lookup(“java:comp/env”);

DataSource ds = (DataSource) envCtx.lookup(“jdbc/raDataSource”);

No Comments

J2EE – structure

J2EE components:

Application clients and applests components run on the client.

Servlet & JSP are web components run on the server

EJB components are business components run on the server

Generally, J2EE server is in Pink :)

Client <–> web tier(JSP/Servlet/Java Bean) <–>Business tier

More precisely,

Client <–> web tier(JSP/Servlet/Java Bean) <–>Business tier (Entity Bean/Session Bean/Message-Driven Bean) <–> DB Level

J2EE Containers:

Containers, no need to spend too much time here. Easy and Stupid.

  • Application client container
  • web container
  • EJB container

No Comments

J2EE – Http Session

Very Simple Example。Http Session is stateless, keep track what user is doing.

SessionServlet

public class SersionServlet extends HttpServlet implements Servlet {

HttpSerssions session = request.getSession(false); //takes any two params, false, if user has session, give it to me; true, if user has no session, create one

if(session == null) {

session = reuqest.getSession(true);

session.setAttribute(“count”, hitCount);

}

Integer hitCount = (Integer)session.getAttribte(“count”);

hitCount = new Integer(hitCount.intValue()) + 1;

response.getWriter().print(“Number of times at this page ” + hitCount.intValue() );

}

So, when user hit this servlet, it will be increased by 1 everytime user refreshes the page.

, , ,

No Comments