Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to publish ejb as web Service in J2EE web service Development

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today, the editor will show you how to publish ejb as a web service in J2EE web service development. The knowledge points in this article are introduced in great detail. Friends who feel helpful can browse the content of the article with the editor, hoping to help more friends who want to solve this problem to find the answer to the problem. Let's follow the editor to learn more about "how to publish ejb as a web service in J2EE web service development".

Publishing ejb as a web service is more complicated than JSE endpoints. And only stateless session Bean can be published as a Web service. One of the great benefits of taking advantage of ejb is container-managed transactions. But spring can do the same, which is no longer very attractive. Then the remaining benefits are estimated to be used in cases where ejb is necessary and web services are needed at the same time. EJB endpoints differ from JSE endpoints in that you do not need to configure servlet mapping in the web.xml file, but you need to configure the web service endpoint interface in the ejb-jar.xml file. Another difference is that there is no need for a concrete class that implements the web service interface. Jboss 4.04 version of the specific implementation.

Establish a stateless session bean

Package ejb; import javax.ejb.SessionBean; import javax.ejb.SessionContext; import javax.ejb.CreateException; public class WebServiceBean implements SessionBean {SessionContext sessionContext; public void ejbCreate () throws CreateException {} public void ejbRemove () {} public void ejbActivate () {} public void ejbPassivate () {} public void setSessionContext (SessionContext sessionContext) {this.sessionContext = sessionContext } public void hello () {System.out.println ("hello");}} package ejb; import javax.ejb.EJBObject; import java.rmi.RemoteException; public interface WebService extends EJBObject {public void hello () throws RemoteException;} package ejb; import javax.ejb.EJBHome; import javax.ejb.CreateException; import java.rmi.RemoteException Public interface WebServiceBeanHome extends EJBHome {public WebService create () throws CreateException, RemoteException;}

There is not much to say.

Define a Web service interface

Package ejb; import java.rmi.*; public interface WebServiceTest extends Remote {public void hello () throws RemoteException;}

Generate the configuration files required for the Web service:

< configuration xmlns="http://www.jboss.org/jbossws-tools" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.jboss.org/jbossws-tools http://www.jboss.org/jbossws-tools/schema/jbossws-tool_1_0.xsd">

< java-wsdl>

< service name="WebServiceTest" style="rpc" endpoint="ejb.WebServiceTest"/>

< namespaces target-namespace="http://ejb" type-namespace="http://ejb"/>

< mapping file="jaxrpc-mapping.xml"/>

< webservices ejb-link="WebServiceBean"/>

< /java-wsdl>

< /configuration>

Use the tools that come with Jboss to generate the deployment files needed for Web services. Wstools- cp classes-config wstools-config.xml

Place the generated deployment file in the META-INF folder instead of the web-INF folder at the JSE endpoint.

Modify the ejb-jar.xml file to add ejb.WebServiceTest elements, pay attention to the order. OK package and release.

Client calling program

Package ejb; import java.net.URL; import javax.xml.rpc.*; import javax.xml.namespace.QName; import javax.naming.*; import javax.rmi.PortableRemoteObject; import javax.naming.InitialContext; import java.util.Hashtable; public class ClientTest {private WebServiceTest getPort () throws Exception {ServiceFactory factory = ServiceFactory.newInstance (); URL wsdlURL = new URL ("http://hechang:8080/ejb/TestService?wsdl"); QName qname = new QName ("http://ejb"," WebServiceTest "); Service service = factory.createService (wsdlURL, qname); WebServiceTest port = (WebServiceTest) service.getPort (WebServiceTest.class); return port;} private WebService ejbTest () throws Exception {Hashtable environment = new Hashtable () Environment.put (Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory"); environment.put (Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces"); environment.put (Context.PROVIDER_URL, "jnp://localhost:1099"); Context context = new InitialContext (environment) Object ref = context.lookup ("WebServiceBean"); WebServiceBeanHome webServiceHome = (WebServiceBeanHome) PortableRemoteObject. Narrow (ref, WebServiceBeanHome.class); WebService webService = webServiceHome.create (); return webService;} public static void main (String [] args) throws Exception {ClientTest clienttest = new ClientTest (); WebServiceTest webService = clienttest.getPort (); webService.hello (); WebService webService2=clienttest.ejbTest (); webService2.hello () }} Thank you for reading, the above is the whole content of "how to publish ejb as a web service in J2EE web service development". Friends who learn it, hurry up and get started. I believe that the editor will certainly bring you better quality articles. Thank you for your support to the website!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report