foo-ejb
module written in previous post.Steps 1 - 5 are the same as in previous post, except in step 2,
foo/Client.java
is not needed here.6. Servlet class
package foo;7. web.xml
import java.io.*;
import javax.ejb.EJB;
import javax.servlet.*;
import javax.servlet.http.*;
public class FooServlet extends HttpServlet {
@EJB(mappedName="foo.FooRemote")
private FooRemote foo;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet FooServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>FooRemote.echo returned: " + foo.echo("From FooServlet") + "</h1>");
out.println("</body>");
out.println("</html>");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>FooServlet</servlet-name>
<servlet-class>foo.FooServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FooServlet</servlet-name>
<url-pattern>/FooServlet</url-pattern>
</servlet-mapping>
</web-app>
8. Make sure
foo.FooRemote.class
is in the webapp's classpath. For example, C:\simple-ejb\build\classes
directory is included in the webapp's classpath.9. Build the webapp to produce a
simple-web.war
with the following content:index.jsp10. Make sure the ejb jar is already deployed (in step 5). Start glassfish server and deploy the war file:
WEB-INF/web.xml
WEB-INF/classes/foo/FooServlet.class
WEB-INF/classes/foo/FooRemote.class
set JAVAEE_HOME=C:\glassfishAnother way to deploy the war is using asadmin command:
%JAVAEE_HOME%\bin\asadmin.bat start-domain
C:\simple-web\dist> copy simple-web.war %JAVAEE_HOME%\domains\domain1\autodeploy
11. Run it by entering the following url in browser:
C:\simple-web\dist> %JAVAEE_HOME%\bin\asadmin deploy simple-web.war
http://localhost:8080/simple-web/FooServlet
Update: Starting from EJB 3.1, EJB classes can be packaged in WAR files. So if the target server supports EJB 3.1, the ejb jar file in this example can be merged into WAR (typically by putting all EJB classes under WEB-INF/classes).