...
Now we need to add a business method and mark the interface as a remote one with @Remote
annotation.
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
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 | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
package sampleear; import javax.ejb.Stateless; @Stateless public class MyStatelessSessionBean implements RemoteBusinessInterface { public String sayHello(String name) { return getClass().getName() + " says hello to " + name + "."; } } |
...
- Right click on the SampleWAR project and select New -> JSP. Name it
index.jsp
and click Finish. - Change it so it executes the servlet upon form submission.
Section Code Block html html borderStyle solid title index.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 | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
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)); } } |
...