"Java EE 6 development with JBoss 7" Development Deployment call EJB

Source: Internet
Author: User
Tags jboss jboss server

Development tools: Eclipse LUMA (Requires Oracle Java EE 6 installed)
JBoss Version: Jboss-as-web-7.0.2.final

The following is a development step, and it is relatively straightforward to develop a deployment EJB in JBoss 7.
Readers are required to properly install the above software platform and have mastered the ability to create Web engineering and develop Servlets.

1. Create a Web project

Select Project, New, Dynamic Web Project
Create a Web project, create a project, write a servlet, and then use this servlet to invoke the EJB (EJB cannot be tested in the Main method)

2. Create an EJB project

Choose EJB Project, New, Project
Enter project name: TESTEJB
and select 3.1 at the EJB version
viewed two times next, here is important, we must generate Ejb-jar.xml
Tick the checkbox for generate Ejb-jar.xml

Click Finish to complete.

3. Creating an EJB interface

In the TESTEJB project, you create a helloremote interface and a class that inherits this interface and implements Hello,hello need to annotate the meta-annotations, the code is as follows:
Helloremote.java

package com.ex;publicinterface HelloRemote {    publicvoidsetName(String name);    publicsayHello();}

Hello.java

 PackageCom.ex;ImportJavax.ejb.Remote;Importjavax.ejb.Stateless;@Stateless(name="Hello")@Remote(Helloremote.class) Public  class Hello implements helloremote{    PrivateString name ="";@Override     Public void SetName(String name) {//TODO auto-generated method stub         This. name = name; }@Override     PublicStringSayHello() {//TODO auto-generated method stub        return "Hello,"+name; }}

The final TESTEJB organization is as follows:

Run on the JBoss server: Right-click Run as, run as server
Choose JBoss as 7.0, Next--Fisish
You can then see the output information in the console:

These are the names of the Jndi bindings, and we'll use one of these when we call this EJB with Jndi.

4. Invoking the EJB in the servlet

The EJB exported (export) into an EJB jar,
Then put this jar into the Webcontent/web-inf/lib folder in the WEB project you created in the first step,
Invoking EJBS through Jndi in a servlet, some of the code is as follows:

    protected void Doget(HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {//TODO auto-generated method stubHelloremote FIRSTEJB = Lookupremotestatelessejbbean (); Firstejb.setname ("World"); System. out. println (FIRSTEJB);        String s = Firstejb.sayhello (); System. out. println (s);        Writer writer = Response.getwriter ();    Writer.write (s); }Private StaticHelloremoteLookupremotestatelessejbbean() {//TODO auto-generated method stubFinal hashtable<string, string> jndiproperties =NewHashtable<string, string> (); Jndiproperties.put (Context.url_pkg_prefixes,"Org.jboss.ejb.client.naming"); Context context =NULL;Try{context =NewInitialContext (jndiproperties); }Catch(Namingexception E1) {//TODO auto-generated catch blockSystem. out. printf ("Error finding context");        E1.printstacktrace ();        } String namespace; namespace ="Java:global/test3/hello!com.ex.helloremote"; System. out. println (namespace);Try{return(helloremote) Context.lookup (namespace); }Catch(Namingexception e) {//TODO auto-generated catch blockE.printstacktrace (); }return NULL; }

In Web. Xml with this servlet URL, access this servlet and see the output as follows:

Both the page and the console output the Hello WORLD,EJB call success.

"Java EE 6 development with JBoss 7" Development Deployment call EJB

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.