Eclipse Tutorial

Jeremy Skrdlant

2005

Table of Contents

Section 1:

Installation Of Eclipse

Installation Of Jboss IDE

Installation Of Jboss

Section 2:

Create a EJB 3.0 Project

Section 3:

Create an Entity Bean

Create a Session Bean

Section 4:

Create a Servlet

Section 5:

Package the Application and deploying it.

Section 1: Installation

Getting the Components

You can install eclipse by going to eclipse.org and selecting the Download Link. Choose Eclipse SDK 3.1 for this tutorial. Next go to the JBoss web site and choose the download link for version 1.5rc1. This is very important that it is this version or higher because the stable version does not have EJB 3.0 implemented yet.

Download Sites as of 12-6-2005

Eclipse

JBoss IDE

JBoss Application Server

Step 1: Put the Eclipse zip file and the JBoss IDE zip file in the same directory and unzip both to the base of that directory. Your finished directory should look like this.

Congratulations. You are done installing eclipse 

Step 2: Now you will need to go through a wizard to install JBoss 4.0.x onto your computer. I would recommend you use the Run Installer option on the website. Below is a picture of the website as of 12-6-2005 with the button mentioned above circled.

Remember the Directory that you installed JBoss to. You will need that information later on.

Section 2: Creating an EJB 3.0 Project

Start up eclipse by double clicking the executable located in the eclipse folder that you extracted the eclipse source to. It should be /eclipse/eclipse from where your zip files are.

The first thing that will happen is it will ask you for a directory to store all your projects in. This is not set in stone so it is not too big of a decision. Later on, if you want to change your workspace directory, simply go to File->Switch Workspace

There is also a Welcome screen at the beginning of operations. You can go ahead and close that out. It has some tutorials. It can always be accessed again by going to

Help->Welcome.

To create a new project, simply choose File->New->Project. A wizard will come up for you to choose which type of project.

You should see a list of projects you can create. Choose the EJB3 and click on Next.

The next step requires you to enter a name for the project. For this Tutorial, we will call it Tutorial1. After you have the name typed, press Next.

The next part of the wizard will ask you for a JBoss Configuration. Go ahead and choose the button that says Create a JBoss Configuration.

You will see a Debug screen. In the left pane, double click on JBoss 4.0.x. This will create a New_Configuration. Rename this configuration to JBoss4-Tutorial. The next step you will need to do is press the Browse button and select the folder that you chose for your JBoss Installation. Click OK on the Browse For folder pop up and try to press the Apply button.

Eclipse can be a tad buggy at this point. For some odd reason you need both JRE 1.4.2 and 1.5 . If you do not get a missing Jar file error then you can skip these next two steps about pointing eclipse to another JRE.

Click on the Little Right arrow until you see the JRE tab. (This arrow is circled by red in the Picture) Next press the Installed JREs button.

You will see a pop up that shows all the JRE’s that Eclipse found on your system which unfortunately is usually only 1 of them. Click on the Add button. You should see a small wizard that asks for the base direction of the other JRE. If you had 1.5 to start with, then choose the 1.4 and vice versa for the people with 1.4 to start with.

If the Project is accepted, click on the Close button to Close out the wizard.

Finally choose the Configuration you just created and press Finish.

Your Package Explorer should look like the one below when you have the folders expanded.

Section 3: Session Bean and Entity Beans

Right Click on the src folder and Choose New->Class.

Give the Class a name of User. Make sure the Package is src and press Finish.

Type in the following code into the User.java class. If it is not in the tabs, go to the package hierarchy and double click on User.java.

On the Right side in the Outline, highlight both the components of User. Then right click and choose Source->Generate Getter and Setters.

Make sure all the methods are selected and press Ok.

Finally do some typing to get your source code to look like the following.

//Beginning of source code.

package src;

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.GeneratorType;

import javax.persistence.Id;

@Entity

public class User {

private String username;

private String password;

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

@Id(generate = GeneratorType.AUTO)

@Column(name="userName")

public String getUsername() {

return username;

}

public void setUsername(String username) {

this.username = username;

}

}

//End of Source Code

Right Click on the src folder. Choose New->Other.

A New Popup will appear. Choose the EJB3.0->Session Bean. Then press the Next button.

Give the Bean a name of Users. Bean Package is src. (You can change that to whatever your heart desires.) Click the Finish button when you are through.

In the Package Explorer, Double click on Users.java to get a tab for it in the src editing window. Then put in the method outlined by comments. You can add more methods but for this tutorial we are going to keep it simple and just have the one method. When you press the floppy to save the changes, you will notice an error on the UsersBean.java tab. This is there because the Interface is different from the class. We will take care of this issue in the next step.

Press the UsersBean.java tab to get it to the front. Then right click in the blank space between the brackets. Choose Source->Override/Implement methods

An implementation box will pop up. Make sure Users is checked. Then press OK.

You will now notice that the loginUser method is in UsersBean.java. Before we can move on, we need to add some libraries.

Right click on the Tutorial1 folder and select Properties.

In the Properties Window, Select the Java Build Path in the left Frame. Then select the Libraries Tag.

Click the Add Library button. And choose J2EE 1.4 Libraries (JBoss IDE)

Then finally choose OK on the Main Properties window.

Make your UsersBean look like the code below.

//beginning of code

package src;

import java.util.ArrayList;

import javax.ejb.Stateless;

import javax.persistence.EntityManager;

import javax.persistence.Query;

import src.Users;

public @Stateless class UsersBean implements Users {

@Inject

private EntityManager manager;

public void createUser(String userName, String password) {

User temp = new User();

temp.setPassword(password);

temp.setUsername(userName);

}

public boolean loginUser(String userName, String password) {

ArrayList<User>users = new ArrayList<User>();

Query q = manager.createQuery("from User");

for (Object o : q.getResultList())

{

users.add((User)o);

}

if (users.size() > 0)

return true;

return false;

}

}

// end of Code.

Creating a Servlet.

Right click on the src folder and choose New->Class.

In the create new class window, set the package to src.web. In the name box choose UsersServlet. Finally fill in the javax.servlet.http.HttpServlet for the super class. You can use the browse button to help with the last one.

Now we are going to put in some of the usual methods. In the UsersServlet class, type doP and press Ctrl + Space Bar. A pop up of possible methods will appear.

Double click on the doPost method and wait for it to create. Next do the same for doG.

Double click on the doGet method and wait for it to create.

Once those methods are created, you will need to add a bunch of code. I have listed the code below.

// beginning of code

package src.web;

import java.io.IOException;

import javax.naming.Context;

import javax.naming.InitialContext;

import javax.naming.NamingException;

import javax.servlet.*;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import src.Users;

@SuppressWarnings("serial")

public class UsersServlet extends HttpServlet {

private Users usersBean;

private int state = 0;

public void init() throws ServletException {

try {

Context context = new InitialContext();

usersBean = (Users)context.lookup(Users.class.getName());

} catch (NamingException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

protected void doPost(HttpServletRequest req, HttpServletResponse resp)

throws ServletException, IOException

{

String userName = req.getParameter("userName");

String password = req.getParameter("password");

String result = checkPassword(userName, password);

resp.setContentType("text/html");

ServletOutputStream out = resp.getOutputStream();

out.println("<html>");

out.println("<body>");

out.println("<p>Username:");

out.println("<form action =\"users\" method=\"POST\">");

out.println("<INPUT TYPE=\"text\" NAME=\"userName\" VALUE=\"\" SIZE=44 MAXLENGTH=30>");

out.println("<p>Password<p>");

out.println("<INPUT TYPE=\"password\" NAME=\"password\" VALUE=\"\" SIZE=44 MAXLENGTH=30>");

out.println("<p<input type=\"submit\" value=\"checkPassword\"<p>");

out.println("<input type=\"submit\" value=\"createUser\"<p>");

out.println(result);

out.println("</form>");

out.println("</body>");

out.println("</html>");

}

protected void doGet(HttpServletRequest req, HttpServletResponse resp)

throws ServletException, IOException

{

doPost(req, resp);

}

void createUser(String un, String up)

{

System.out.println("CREATING USER");

if((un != null )& (up != null))

{

usersBean.createUser(un,up);

}

}

String checkPassword(String un, String up)

{

System.out.println("CHECKING PASSWORD");

if ((un == null)|| (up == null))

{

// do nothing.

}else

if ((usersBean.loginUser(un,up))|| (state == 1))

{

state = 1;

return "User Name and Password has been verified.";

}else

{state = 2;

if (state == 2)

{

return "Login Failure";

}}

return " ";

}}// end of code

Packaging and application deployment

The first step is to create a deployment descriptor. Right click on the src folder and choose New->Other. Then from the New menu, choose Web Application 2.4 Deployment Descriptor. Then press Next.

Next choose

Make sure it is named web.xml and click Finish..

Now we are going to edit the xml file. First we are going to remove the Filter Forms and leave just the servlets.

Fill in the Correct values for the servlet name, class , and mapping information.

Next we need to create the Packaging. Right click on the Tutorial1 folder and go to Properties. Click on the Packaging Configuration in the left pane.

Press Add. Then fill in the users-beans.ejb3 for the Name and press OK.

Press Add again and fill in users.war and press OK.

Right click on users-bean.ejb3 and choose Add Folder.

Press Project Folder and then Select the bin folder and press OK.

make sure to put the **/*Servlet in the Excludes section and press OK.

Right Click on the users.war and choose Add Folder.

Press the Project Folder. Choose the bin folder again and this time put the **/*Servlet in the Includes portion.

Right click on the users.war and choose Add File.

choose web.xml and press OK.

Type in WEB-INF for the Prefix and press OK.

Right click on the File under users.war. Choose Edit.

Set the prefix to WEB-INF/classes

Finally right click on Tutorial1 and press Run Packaging.

When it is finished creating the packages, you will see the following in your Console panel.

The Final Section.

Cross your fingers and hope it runs.

Press the little Bug at the top. Then choose the configuration you made. Finally press Debug.

You will see the JBoss Server putting a lot of text out to the console. If you notice blue errors that go back to Mbean, you might have the wrong Java version chosen. Click on the little arrow next to the bug and choose debug. Then Choose the configuration you had and push the little arrows until you see the JRE tab. Your JDK tab should look like this.

If you have JBoss successfully running, we will need to deploy the application to that server. Select both users.war and users-beans.ejb3. Right click on them and choose Deployment->Deploy To.

Finally you will need to choose The Server. Click on JBoss4 and press OK.

Open up a web browser and type in the URL You might have to worry about capitalization. You can check that in your web.xml document.

Have a great day.