Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 4.0

...

Now we need to add a business method and mark the interface as a remote one with @Remote annotation.

Code Block
javajava
borderStylesolid
titleRemoteBusinessInterface.java
java
package sampleear;

import javax.ejb.Remote;

@Remote
public interface RemoteBusinessInterface {
    public String sayHello(String name);
}

...

Implement the business method sayHello and mark the class as a stateless session bean with the @Stateless annotation.

Code Block
javajava
borderStylesolid
titleMyStatelessSessionBean.java
java
package sampleear;

import javax.ejb.Stateless;

@Stateless
public class MyStatelessSessionBean implements RemoteBusinessInterface {

    public String sayHello(String name) {
        return getClass().getName() + " says hello to " + name + ".";
    }
}

...

  1. Right-click on the SampleWAR project and select New -> JSP. Name it index.jsp and click Finish.
  2. Change it so it executes the servlet upon form submission.
    Section
    Code Block
    htmlhtml
    borderStylesolid
    titleindex.jsp
    html
    <%@ 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>5-minute Tutorial on Enterprise Application Development with Eclipse and Geronimo</title>
      </head>
      <body>
        <form action="${pageContext.request.contextPath}/sayHello">
          <input type="text" name="name" /><input type="submit" value="Press me!" />
        </form>
      </body>
    </html>
    

...

MyServlet.java opens up automatically for editing after creation, update the servlet as shown below to call off the ejb when executed.

java
Code Block
java
borderStylesolid
titleMyServlet.java
java
package sampleear;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
    static final long serialVersionUID = 1L;

    @EJB
    RemoteBusinessInterface remoteBusinessIntf;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        if (name == null || name.length() == 0) {
            name = "anonymous";
        }
        response.getWriter().write(remoteBusinessIntf.sayHello(name));
    }
}

...

  1. Right-click on the SampleEAR project and select Run As -> Run on Server. When Run On Server popup window comes up, select the Always use this server when running this project checkbox. Leave the rest as is.



  2. Click Finish.
  3. The server's stopped so nothing happens (from a user's perspective at least). Open up the Servers tab and right-click on Apache Geronimo v2.1 Server at localhost and select Start.
  4. After a few seconds, Geronimo will be up and running with the enterprise application published. Open up the browser of your choice and go to http://localhost:8080/SampleWARImage Removed.



  5. Type in any name you want, e.g. John Doe and press Press me! button.