J3 Limited
 
Google
WWW J3Ltd
 
Testing The Installation Part One

Introduction

Installing and configuring MySQL, Java JDK, JBoss, and Eclipse (including plug ins) required many steps.

During the installation, each individual component was installed and run. So it is known that each component runs OK. In this section, the components are tested together. In this way installation problems can be found and rectified, before a J2EE project is started in earnest.

The easiest way to test the installation, is to write a small project which uses all the components that have been installed.

A bonus of this step is that each component is used, and therefore some idea of how these components are used and configured begins to be learnt.

The source code for parts one and two of the installation tests can be downloaded as a zip archive from here.

Create The Project

Eclipse is started, the "New Project..." option of the file menu is selected. Which pops up the dialog shown below.

The EJB3.0 Project is chosen and the next button is pressed. In the next page of the wizard, the project name entered is "TestInstallation", then the "Next" button is pressed.

The next wizard page asks to select a JBoss configuration, as there are none, a new JBoss configuration needs to be created. The "Create a JBoss Configuration" button is pressed, which pops up a dialog as shown below.

The JBoss 4.0.x entry is double clicked, which causes the wizard to start the process of creating a new configuration.

The JBoss configuration being created here will be used again, when creating new JBoss Projects in Eclipse.

The name entered is " EJB 3". The browse button is used to set the home directory to where the JBoss server has been installed. And the configuration selected is "default" (the only one in the dropdown list).

The "Apply" button is pressed, then the "Close" button is pressed.

The configuration shown above is selected, and the "Finish" button is pressed.

The welcome window is closed, this displays the Eclipse environment that is going to be used to develop the application.

 

Eclipse provides many perspectives. The perspective shown above is the Java perspective, another one is the debug perspective, which is used to debug our Java applications. The Window->Perspectives menu entry allows a particular perspective to be selected.

Create A Stateless EJB

The simplest EJB is a stateless EJB. This is a good way to test that the EJB server, Eclipse and the Eclipse plug-in are all installed and configured correctly.

To create a new Stateless EJB, the "src" icon in the package explorer is right clicked, and the "New->Other..." entry is selected:

The Session Bean wizard is selected from the dialog, and next is pressed:

The bean's package com.j3ltd.test.ejbs and class name TestStateless are entered then finish is pressed:

The wizard has created two files: the bean implementation and the bean interface. The bean interface is double clicked to open it in the editor:

The method signature for testBean(); is added to the interface:

package com.j3ltd.test.ejbs;


import javax.ejb.Remote;


@Remote
public interface TestStateless {
	public String testBean();
}

 In the package explorer, the TestStatelessBean.java file is right clicked, and "Override/Implement Methods" is selected as shown below:

Eclipse pops up a dialog which allows us to select which method bodies should be generated. In this case the default presented is accepted by pressing OK.

With the above dialog, the methods defined in the interface are generated in the implementation class TestStatelessBean. The method testBean() is amended to return something useful:

public @Stateless class TestStatelessBean implements TestStateless {

	public String testBean() {
		return "the server returned this string";
	}

}

The stateless EJB is complete. WIth EJB 3, there is no need to code deployment descriptors or the home interface. The next step is to create some kind of client to test the EJB.

Write A Test Client

A test client can take many forms. It could be:

  • A Java application
  • A Servlet
  • A Java Server Page

A Servlet is a good place to start, it allows the EJB server, and the Web server (Tomcat) to be tested, without too much initial configuration.

Add Servlet Support To The Project

A servlet makes use of a Java library that is not already part of the project, the J2EE 14. library. To add this, the project icon in the package explorer is right clicked, and "Properties" is selected:

In the project properties' dialog, the "Java Build Path" is selected. The libraries tab is clicked, then the "Add Library" button is pressed. This brings up the dialog shown below. The library to add is the "J2EE Libraries (JBoss-IDE)":

Once the library is selected, Next, then "Finish" is pressed to dismiss the "Add Library" dialog. Subsequently "OK" is pressed on the project properties dialog to dismiss it.

Create The Servlet Class

In Eclipse a new class is created, called "TestStatelessEJBServlet", by right clicking on the TestInstallation project and selecting "New->Class".

The package name entered is com.j3ltd.test.web. The Name of the class is TestStatelessEJBServlet. For the superclass, the "Browse" button is pressed, this pops up the superclass selection dialog which makes finding the type much easier. By typing "HttpServ" in the type textfield, the selection shows HttpServlet. This appears because the J2EE library has previously been added to the project. It is selected, and all the dialogs dismissed.

The servlet is edited to look like this:

package com.j3ltd.test.web;


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

import com.j3ltd.test.ejbs.*;


public class TestStatelessEJBServlet extends HttpServlet {
	private TestStatelessEJB statelessBean;
	
	public void init() {
		try {
			Context context = new InitialContext();
			statelessBean = (TestStatelessEJB) c
ontext.lookup("TestStatelessBean/remote"); } catch (NamingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException) { PrintWriter writer = resp.getWriter(); writer.write("The stateless bean returned this string: " + statelessBean.testBean()); } }

The code shown above responds to an http request by calling on the stateless EJB, and displays the text the "testBean()" EJB method returns.

Web.xml Web Application Description File

The servlet class needs to be added to the web application deployment descriptor. The first step is to create the file called web.xml, as follows:

The src folder in the package explorer is right clicked, and the "New->Other" menu entry is selected:

The "Web Application 2.4 Deployment Descriptor" wizard is selected:

The wizard's defaults (filename web.xml) are fine, so the "Finish" button is clicked. This creates the file web.xml. The file is opened in the editor. The default servlet filters generated in the xml file are removed. An entry for the servlet is entered as shown below. The servlet is mapped to a url, so that it can be accessed from the browser using a simple url.

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="...
...
<servlet> <servlet-name>TestStatelessEJBServlet</servlet-name> <servlet-class>com.j3ltd.test.web.TestStatelessEJBServlet</servlet-class> </servlet>
<servlet-mapping> <Servlet-name>TestStatelessEJBServlet</servlet-name> <url-pattern>/testStatelessEJB</url-pattern> </servlet-mapping> </web-app>

Next the code needs to be packaged and deployed to the JBoss server.

Packaging The EJB

The stateless EJB needs to be packaged in a jar file. The project icon in the package explorer is right clicked and the "Properties..." entry is selected.

The packaging configuration is selected, and the "Add..." button button is pressed (as shown above). The archive name given is TestInstallation.ejb3. This archive will be used to store the EJB classes. The file name is case sensitive, hence it should end with lower case ".ejb3".

The package created is right clicked and "Add Folder" is selected:

In the dialog that pops up, the Project Folder button is clicked and the bin folder is selected:

The files to include from the selected folder are all the classes in the com.j3ltd.test.ejbs. This is done by entering **/ejbs/* in the "includes" textfield:

The jndi file that is in the project's src folder also needs to be added. This is done by right clicking the package, and selecting add file from the popup menu:

The "Project File" button is pressed, and the file called jndi.properties is selected.

That completes the packaging for the EJB.

Packaging The Web Application

The servlet needs to be packaged in a war file. The project icon in the package explorer is right clicked and the "Properties..." entry is selected.

The add button is clicked again to create an archive called TestInstallation.war. This archive will be used to store the servlet classes and the web.xml deployment descriptor.

The web application archive name is right clicked and the add folder popup menu item is selected.

The bin folder is added. In the includes textfield **/web/* is entered, this causes the archive to include all the classes in the com.j3ltd.web folder. The prefix WEB.INF/classes is also entered:

The web application archive name is right clicked and the add file popup menu item is selected:

The project file web.xml is selected, by pressing the "Project File..." button and selecting the file called web.xml in the src folder of the project. The prefix WEB-INF is entered in the "Prefix" textfield:

The apply button is pressed and then the OK button, that brings to an end the packaging configuration:

Building The Deployment Packages

It is important to have the "Enable Packaging" checkbox checked. The project icon in the package explorer is right clicked and the "Properties..." entry is selected to popup the dialog shown below:

Once the packages are configured, they can be built by selecting the "Run Packaging" menu option shown below:

Eclipse builds the packages and displays the "Build successful" message in the console pane.

The next step is to debug the application on the JBoss server.

Launching JBoss In Debug Mode

JBoss is launched using the debug option in Eclipse:

The debug dialog pops up, the EJB 3 Configuration is selected. The source tab is subsequently selected, and the add button is pressed, so that the path to the Java code can be set.

The project is selected:

Then all the dialogs are OK'd, and finally the last one the Apply button is pressed, then Debug is pressed to launch JBoss in the IDE.

Eventually, the console pane displays the status informing us that JBoss has started:

Deploying The Packages To JBoss

In a conventional JBoss server, deploying any packages is simple: the archive file to deploy is copied to the relevant server folder in JBoss. For example simply copying an archive file to the bin/jboss/server/default/deploy folder deploys the archive. If JBoss is running it detects the change and dynamically deploys the contents.

When programming using Eclipse, there is another way to deploy packages. From Eclipse, the packages to deploy are selected, and right clicked. The "Deploy To..."menu option is selected, as shown below:

The server deployment target is selected:

Once the packages are deployed to JBoss, the application is ready to be tested. In a web browser, the url http://localhost:8080/TestInstallation/testStatelessEJB is entered. The browser calls on the servlet, which in turn calls on the EJB. The browser displays the text returned by the EJB to the servlet, as shown below:

That completes the first part of the test. The next step is to test the database connectivity.

 

  Copyright © 2006 J3 Ltd Permission is granted to reproduce material on this page, on the condition that a reference to "WWW.J3Ltd.com" is given as the source of the material.