Versions Compared

Key

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

...

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

Code Block
java
java
borderStylesolid
titleRemoteBusinessInterface.javajava
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
java
java
borderStylesolid
titleMyStatelessSessionBean.javajava
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
    html
    html
    borderStylesolid
    titleindex.jsphtml
    <%@ 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.

Code Block
java
java
borderStylesolid
titleMyServlet.javajava
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));
    }
}

...