Step 1: create a web applicationSelect File> new project, select Web in the middle of the pop-up interface, and then select Web application on the right ]. Enter the project name helloweb in the following interface and select next to go to the next interface. On this page, select the framework used by the web application. If not selected, click Finish ].
Step 2: import the EJB interface to be accessedThe process is the same as the process for importing interfaces to the application client: Select source packages, right-click, choose new> JAVA packges, and enter the package name session. hello, click Finish ]. Copy hellosessionremote. Java to the session. Hello package.
Step 3: Create a servlet to access the EJBCreate a servlet package, hello. right-click the package and choose new & quot; servlet & quot;. The following interface is displayed: Enter the class name helloservlet and click Finish To Go To The following configuration interface. You can use the default settings. Note: The servlet access path is/helloservlet, and then click Finish ]. The helloservlet code will be generated later. The generated code is as follows:/** helloservlet. java ** created on April 19, 2007, */package hello. servlet; import Java. io. *; import java.net. *; import javax. servlet. *; import javax. servlet. HTTP. *; /***** @ author administrator * @ version */public class helloservlet extends httpservlet {/** processes requests for both HTTP <code> Get </code> and <code> post </code> methods. * @ Param request Servlet Request * @ Param response servlet response */protected void processrequest (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {response. setcontenttype ("text/html; charset = UTF-8"); printwriter out = response. getwriter ();/* todo output your page here out. println ("<HTML>"); out. println ("Modify the generated codeRun the following code to inject EJB: @ ejbprivate hellosessionremote hello; you need to import EJB and hellosessionremote. The Code is as follows: Import javax. EJB. EJB; import session. hello. * write the Test Code. The Code is as follows: public void test (printwriter out) {out. println (hello. hello ();} Put the code in the processrequest method:/* todo output your page here out. println ("<HTML>"); out. println ("Deploy Web ApplicationsSelect a project, right-click, and select deploy project ].
Access web applicationsAddress: http: // localhost: 8080/helloweb/helloservlet Step 3: Create a jspjsp for accessing EJB and create a servlet. Use the Wizard to create a JSP file: Hello. jsp. The reference code is as follows: <% @ page contenttype = "text/html" %> <% @ page pageencoding = "gb2312" %> <% @ page import = "session. hello. *, javax. naming. * "%> <%! Private hellosessionremote hellosession = NULL; Public void jspinit () {try {initialcontext Ic = new initialcontext (); hellosession = (hellosessionremote) IC. lookup (hellosessionremote. class. getname ();} catch (exception ex) {system. out. println (ex. getmessage () ;}} public void jspdestroy () {hellosession = NULL ;} %> <HTML>