THIS IS A TEST INSTANCE. ALL YOUR CHANGES WILL BE LOST!!!!
...
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=UTF-8" pageEncoding="UTF-8"%> <!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=UTF-8"> <title>5-minute Tutorial on Enterprise Application Development with Eclipse and Geronimo</title> </head> <body> <form action="MyServlet"> <input type="text" name="name" /> <input type="submit" value="Press me!" /> </form> </body> </html>
...
- Right click on the SampleWAR project and select Properties. Go to Java Build Path and click "Project"item, then click "Add" button to select the "SampleEJB" project and click OK.
- Or select Properties, choose Deployment Assembly. Choose the Manifest Entries tab, then Add..., then Finish, Apply, OK.
- Or select Properties, choose Deployment Assembly. Choose the Manifest Entries tab, then Add..., then Finish, Apply, OK.
- Right click on the SampleWAR project and select New -> Servlet and fill it in with the following values:
Section - Java Package: sampleear
- Class name: MyServlet
- Click Next.
- Change the URL Mapping section so the servlet serves at /sayHello url mapping and click Finish.
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 borderStyle solid title MyServlet.javajava package sampleear; import java.io.IOException; import javax.ejb.EJB; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import sampleear.RemoteBusinessInterface; @WebServlet("/MyServlet") public class MyServlet extends HttpServlet { private 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)); } }
- Update web.xml under WEB-INF directory of SampleWAR
Code Block java java borderStyle solid title web.xmljava <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>SampleWAR</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <display-name>MyServlet</display-name> <servlet-name>MyServlet</servlet-name> <servlet-class>sampleear.MyServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>MyServlet</servlet-name> <url-pattern>/MyServlet</url-pattern> </servlet-mapping> </web-app>
- Check geronimo-web.xml under WEB-INF directory of SampleWAR
Code Block java java borderStyle solid title geronimo-web.xmljava <?xml version="1.0" encoding="UTF-8"?> <web:web-app xmlns:app="http://geronimo.apache.org/xml/ns/j2ee/application-2.0" xmlns:bp="http://www.osgi.org/xmlns/blueprint/v1.0.0" xmlns:client="http://geronimo.apache.org/xml/ns/j2ee/application-client-2.0" xmlns:conn="http://geronimo.apache.org/xml/ns/j2ee/connector-1.2" xmlns:dep="http://geronimo.apache.org/xml/ns/deployment-1.2" xmlns:ejb="http://openejb.apache.org/xml/ns/openejb-jar-2.2" xmlns:jaspi="http://geronimo.apache.org/xml/ns/geronimo-jaspi" xmlns:log="http://geronimo.apache.org/xml/ns/loginconfig-2.0" xmlns:name="http://geronimo.apache.org/xml/ns/naming-1.2" xmlns:pers="http://java.sun.com/xml/ns/persistence" xmlns:pkgen="http://openejb.apache.org/xml/ns/pkgen-2.1" xmlns:sec="http://geronimo.apache.org/xml/ns/security-2.0" xmlns:web="http://geronimo.apache.org/xml/ns/j2ee/web-2.0.1"> <dep:environment> <dep:moduleId> <dep:groupId>sampleear</dep:groupId> <dep:artifactId>sample-war</dep:artifactId> <dep:version>1.0</dep:version> <dep:type>war</dep:type> </dep:moduleId> </dep:environment> <web:context-root>/sayHello</web:context-root> </web:web-app>
...