Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Wiki Markup
{scrollbar}

This

...

application

...

will

...

take

...

you

...

through

...

the

...

basics

...

of

...

Stateless

...

Session

...

Bean.

...

This

...

application

...

will

...

demonstrate

...

how

...

annotations

...

like

...

@Stateless,

...

@Resource,

...

@PostConstruct,

...

@PreDestroy

...

are

...

used

...

in

...

an

...

EJB3

...

application.

...

The

...

application

...

walks

...

through

...

a

...

authentication

...

page,

...

where

...

the

...

user

...

has

...

to

...

authenticate

...

to

...

move

...

to

...

the

...

home

...

page

...

of

...

Apache

...

Geronimo.

...

In

...

case

...

a

...

new

...

user,

...

the

...

user

...

has

...

to

...

go

...

through

...

the

...

registration

...

process.

...

Later

...

the

...

user

...

will

...

be

...

directed

...

to

...

login

...

page

...

again

...

once

...

the

...

registration

...

is

...

done.

...

In

...

the

...

login

...

page

...

the

...

bean

...

class

...

will

...

check

...

for

...

the

...

username

...

and

...

password

...

entered

...

by

...

the

...

user

...

against

...

a

...

database.

...

To

...

run

...

this

...

tutorial,

...

as

...

a

...

minimum

...

you

...

will

...

be

...

required

...

to

...

have

...

installed

...

the

...

following

...

prerequisite

...

software.

...

  • Sun

...

  • JDK

...

  • 5.0+

...

  • (J2SE

...

  • 1.5)

...

  • Eclipse

...

  • 3.3.1.1

...

  • (Eclipse

...

  • Classic

...

  • package

...

  • of

...

  • Europa

...

  • distribution),

...

  • which

...

  • is

...

  • platform

...

  • specific

...

  • Web

...

  • Tools

...

  • Platform

...

  • (WTP)

...

  • 2.0.1

...

  • Data

...

  • Tools

...

  • Platform

...

  • (DTP)

...

  • 1.5.1

...

  • Eclipse

...

  • Modeling

...

  • Framework

...

  • (EMF)

...

  • 2.3.1

...

  • Graphical

...

  • Editing

...

  • Framework

...

  • (GEF)

...

  • 3.3.1

...

Details

...

on

...

installing

...

eclipse

...

are

...

provided

...

in

...

the

...

Development

...

environment

...

section.

...


This

...

tutorial

...

is

...

organized

...

in

...

the

...

following

...

sections:

...

Table of Contents

Creating a Stateless Session EJB project

  1. Right Click under the Project Explorer and Create a EJB project as shown.


    Image Added


  2. On the next screen give the project name as StatelessSessionEJB.


    Image Added


  3. Right Click on ejbModule and create a new Interface RegisterBeanRemote


    Image Added


  4. On the New Java Interface window give the package name as statelessejb and Interface name as RegisterBeanRemote


    Image Added


  5. Populate the RegisterBeanRemote interface with the following code.
    Code Block
    titleRegisterBeanRemote.java
    borderStylesolid
    
    package statelessejb;
    import javax.ejb.Remote;
    @Remote
    public interface RegisterBeanRemote 
    {
    public void register(String FirstName, String LastName, String Sex, String UserName, String Password);
    public boolean verify(String username, String password);
    }
    

...

  1. Right Click on StatelessSessionEJB project and create a new class RegisterBean as shown in the figure


    Image Added


  2. Populate the class RegisterBean with the following data
    Code Block
    titleRegisterBean.java
    borderStylesolid
    
    package statelessejb;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import javax.annotation.PostConstruct;
    import javax.annotation.PreDestroy;
    import javax.annotation.Resource;
    import javax.ejb.Stateless;
    import javax.sql.DataSource;
    @Stateless
    public class RegisterBean implements RegisterBeanRemote{
    	@Resource(name="jdbc/userds")
    	private DataSource dbsource;
    	private Connection dbconnect;
    	@PostConstruct
    	public void initialize()
    	{
    		try{
    			dbconnect= dbsource.getConnection();
    		}
    		catch(Exception e)
    		{
    			e.printStackTrace();
    		}
    	}
    	public void register(String FirstName, String LastName, String Sex, String UserName, String Password)
    	{
    		try
    		{
    			String insert="INSERT INTO USERINFO (" + "FIRSTNAME," + "LASTNAME," + "SEX," + "USERNAME," + "PASSWORD)" + " VALUES(?,?,?,?,?)";
    			PreparedStatement ps=dbconnect.prepareStatement(insert);
    			ps.setString(1, FirstName);
    			ps.setString(2, LastName);
    			ps.setString(3, Sex);
    			ps.setString(4, UserName);
    			ps.setString(5, Password);
    			int rs =ps.executeUpdate();
    		}
    		catch(Exception e)
    		{
    			e.printStackTrace();
    		}
    	}
    	public boolean verify(String username, String password)
    	{
    		boolean ret=false;
    		String select="SELECT * FROM USERINFO";
    		System.out.println(select);
    		try{
    		PreparedStatement ps=dbconnect.prepareStatement(select);
    		ResultSet rs= ps.executeQuery();
    		while(rs.next())
    		{
    			String str=(rs.getString("USERNAME")).trim();
    			String str1=(rs.getString("PASSWORD")).trim();
    			if (str.compareTo(username)==0)
    			{
    			if(str1.compareTo(password)==0)
    				ret=true;
    			else
    			    ret=false;
    			}
    			else
    				ret=false;
    		}
    		}
    		catch(Exception e)
    		{
    			e.printStackTrace();
    		}
    		return ret;	
    	}
    	@PreDestroy
    	public void destroy(){
    		try
    		{
    		dbconnect.close();
    		dbconnect=null;
    		}
    		catch(Exception e)
    		{
    			e.printStackTrace();
    		}
    
    	}
    }
    
    

...

  1. Next

...

  1. we

...

  1. will

...

  1. try

...

  1. to

...

  1. understand

...

  1. the

...

  1. code

...

This

...

completes

...

the

...

development

...

of

...

EJB

...

project.

...

Creating

...

a

...

database

...

using

...

Administrative

...

Console

...

  1. Start

...

  1. the

...

  1. server

...

  1. and

...

  1. Launch

...

  1. the

...

  1. Administrative

...

  1. Console

...

  1. using

...

  1. the

...

  1. URL

...

  1. http://localhost:8080/console

...

  1. .

...

  1. Enter

...

  1. default

...

  1. username

...

  1. and

...

  1. password.

...

  1. In

...

  1. the

...

  1. welcome

...

  1. page,

...

  1. Under

...

  1. Embedded

...

  1. DB,

...

  1. Select

...

  1. DB

...

  1. Manager.


    Image Added


  2. On the next page create a database userdbs and Select create.


    Image Added


  3. Once done you can see the userdbs database listed in DB Viewer portlet under Databases. This confirms that the database has been successfully created.


    Image Added


  4. As shown in the figure under Use DB, select userdbs from the dropdown box.


    Image Added


  5. Run the query as shown in the figure. This query will create table USERINFO with the columns FIRSTNAME, LASTNAME, SEX, USERNAME, PASSWORD.


    Image Added


    Code Block
    titleCreateTable.sql
    borderStylesolid
    
    CREATE TABLE USERINFO
    (
    FIRSTNAME

...

  1.  char(20),
    

...

  1. LASTNAME  char(20)

...

  1. ,

...

  1. 
    SEX       char(7),
    USERNAME  char(20),
    PASSWORD  char(20)
    )
    
  2. To verify the table creation is successful. Under View Tables for userdbs Database, Select Application.


    Image Added


  3. The next window will show the table USERINFO associated with userdbs Database.


    Image Added


Creating a datasource using Administrative Console

  1. Start the server and Launch the Administrative Console using the URL http://localhost:8080/console

...

  1. .

...

  1. Enter

...

  1. default

...

  1. username

...

  1. and

...

  1. password.

...

  1. Once

...

  1. in

...

  1. the

...

  1. welcome

...

  1. page.

...

  1. In

...

  1. console

...

  1. navigation,

...

  1. Under

...

  1. Services,

...

  1. Select

...

  1. Database

...

  1. Pools.


    Image Added


  2. On the next screen, Create a new database pool using Geronimo database pool wizard.


    Image Added


  3. On the next screen give the name as suggested in the figure. This will initiate the process to create a Derby Embedded XA datasource.


    Image Added


  4. Select the Driver jar and give the database name as userdbs(Remember this is the database we created in the previous step). Rest all fields can be set to default.


    Image Added


  5. Select Deploy to deploy the connector plan.


    Image Added


  6. Once done you can see the Database Pool jdbc/userds listed in the available database pools.


    Image Added


Creating application client

  1. Create a new dynamic Web Project with the name Application Client.
  2. Right click on WEB-INF and create the following jsp
    Code Block
    titlelogin.jsp
    borderStylesolid
    
    <%@ 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>Welcome to Apache Geronimo</title>
    </head>
    <body bgcolor="white">
    <form method="post" action="passCredentials.jsp">
    <h2 align="center"><font color="blue">Welcome to Apache Geronimo</font></h2>
    <h2 align="center"><font color="blue">Enter your credentials</font></h2>
    Enter your Username
    <input type="text" name="username" size=20><br>
    Enter your Password
    <input type="password" name="password" size=20><br>
    <input type="submit" value="Login">
    <a href="http://localhost:8080/ApplicationClient/register.jsp">NewUser</a>
    </form>
    </body>
    </html>
    

...

  1. Code Block
    titlepassCredentials.jsp

...

  1. borderStyle

...

  1. solid

...

  1. 
    <%@ page import="java.util.Properties,javax.naming.Context,javax.naming.InitialContext,statelessejb.RegisterBeanRemote" %>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body bgcolor="yellow">
    <%!boolean i; %>
    <%
    System.out.println("Enter");
    Properties prop=new Properties();
    prop.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.RemoteInitialContextFactory");
    	prop.put("java.naming.provider.url", "ejbd://localhost:4201");
    Context context = new InitialContext(prop);
    System.out.println(context);
    RegisterBeanRemote myejb=(RegisterBeanRemote)context.lookup("RegisterBeanRemote");
    String s= request.getParameter("username");
    String s1= request.getParameter("password");
    System.out.println("EJB");
    i=myejb.verify(s,s1);
    System.out.println(i);
    %>
    <% 
    	if (i==true){ 
    %>
    <jsp:forward page="/resources.jsp"></jsp:forward>
    <% 
    	} else {
    %>
    	<jsp:forward page="/login.jsp"></jsp:forward>
    <% 
    }
    %>
    </body>
    </html>
    

...

  1. Code Block
    titleresources.jsp

...

  1. borderStyle

...

  1. solid

...

  1. 
    <%@ 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>Welcome to Apache Geronimo</title>
    </head>
    <body bgcolor="white">
    <h3><font color="blue">Welcome to Apache Geronimo Resource Center</font></h3> 
    Apache Geronimo Home Page
    <a href="http://geronimo.apache.org">Apache Geronimo Home page</a><br>
    Join our mailing list
    <a href="http://geronimo.apache.org/mailing-lists.html">Apache Geronimo Mailing List</a><br>
    Come and Contribute to Apache Geronimo V2.1 Documentation
    <a href="http://cwiki.apache.org/GMOxDOC21/">Apache Geronimo V2.1 Documentation</a>
    </body>
    </html>
    

...

  1. Code Block
    titleregister.jsp

...

  1. borderStyle

...

  1. solid

...

  1. 
    <%@ 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>Welcome to Apache Geronimo</title>
    </head>
    <body bgcolor="white">
    <script type="text/javascript">
    function valForm()
    {
    	if (this.document.form1.username.value=="" || this.document.form1.username.value=="")
    	{
    		alert("You cannot leave the field blank");
    		this.document.form1.firstname.focus();
    		return false;
    	}
    	else
    	{
    		return true;
    	}
    }
    </script>
    <h2 align="center"><font color="blue">Welcome to Apache Geronimo User Registration</font></h2>
    <form method="post" name="form1" action="passVariables.jsp" onSubmit=" return valForm();">
    FirstName
    <INPUT type="text" name="firstname" SIZE=20><br>
    LastName
    <INPUT type="text" name="lastname" SIZE=20 ><br>
    Sex<br>
    <input type="radio" name="sex" value="male"> Male
    <br>
    <input type="radio" name="sex" value="female"> Female
    <br>
    Select a UserName<br>
    <input type="text" name="username" size=20><br>
    Select a Password<br>
    <input type="password" name="password"  size=20><br>
    <br>
    <input type="submit" value="Submit">
    </form>
    </body>
    </html>
    

...

  1. Code Block
    titlepassVariables.jsp

...

  1. borderStyle

...

  1. solid

...

  1. 
    <%@ page import="java.util.Properties,javax.naming.Context,javax.naming.InitialContext,statelessejb.RegisterBeanRemote" %>
    
    <html>
    <head>
    
    <meta http-equiv="Refresh" content="5;URL=http://localhost:8080/ApplicationClient/login.jsp">
    <title>Insert title here</title>
    </head>
    
    <%
    Properties prop=new Properties();
    prop.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.RemoteInitialContextFactory");
    	prop.put("java.naming.provider.url", "ejbd://localhost:4201");
    Context context = new InitialContext(prop);
    RegisterBeanRemote myejb=(RegisterBeanRemote)context.lookup("RegisterBeanRemote");
    String s= request.getParameter("firstname");
    String s1= request.getParameter("lastname");
    String s2= request.getParameter("sex");
    String s3= request.getParameter("username");
    String s4= request.getParameter("password");
    myejb.register(s,s1,s2,s3,s4);
    %>
    
    <h3 align="center"><font color="blue">Thank you for registering with Apache Geronimo</font></h3>
    <h3 align="center"><font color="blue">Redirecting to Login Page</font></h3>
    </html>
    

...

  1. Code Block
    titleindex.jsp

...

  1. borderStyle

...

  1. solid

...

  1. 
    <%@ 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>
    <body>
    <jsp:forward page="/login.jsp" />
    </body>
    </html>
    

...

  1. Few more configurations

  1. In the EJB Project. Under META-INF,

...

  1. Edit

...

  1. openejb-jar.xml

...

  1. and

...

  1. add

...

  1. the

...

  1. following

...

  1. Code Block

...

  1. title

...

  1. datasource

...

  1. dependency

...

  1. borderStyle

...

  1. solid

...

  1. 
    <sys:dependencies>
                <sys:dependency>
                    <sys:groupId>console.dbpool</sys:groupId>
                    <sys:artifactId>jdbc%2Fuserds</sys:artifactId>
                </sys:dependency>        
    </sys:dependencies>
    

...

  1. Info
    titleWhere did the above dependencies come from??

    To make the datasource visible to EJB we need to add a dependency to the EJB deployment plan that is openejb-jar.xml.

...

  1. The

...

  1. above

...

  1. element

...

  1. can

...

  1. be

...

  1. obtained

...

  1. automatically

...

  1. from

...

  1. Geronimo

...

  1. Database

...

  1. Pool

...

  1. wizard.

...

  1. Select

...

  1. usage

...

  1. against

...

  1. the

...

  1. database

...

  1. pool

...

  1. jdbc/userds

...

  1. In the WEB Project.

...

  1. Under

...

  1. WEB-INF,

...

  1. Edit

...

  1. geronimo-web.xml

...

  1. and

...

  1. add

...

  1. the

...

  1. following

...

  1. Code Block
    titleEJB dependency
    borderStylesolid
    
    <sys:dependencies>
           <sys:dependency>
                <sys:groupId>default</sys:groupId>
          		<sys:artifactId>StatelessSessionEJB</sys:artifactId>
          		<sys:version>1.0</sys:version>
          		<sys:type>car</sys:type>
           </sys:dependency>        
    </sys:dependencies>
    

...