How to reference and find Web services in the Java EE component

Source: Internet
Author: User
Tags define config interface valid web services xmlns java web wsdl
J2ee|web|web Services This article discusses how to reference Web services in the Java EE component and find Web services through Jndi. On the content organization, a EJB2.1 stateless session Bean is first published as a Web service, then a Web service client based on JSP is developed, and finally, it focuses on how to reference the Web service in JSP deployment, and discusses how to find and invoke Web services through Jndi. Before you read this article, you need the following knowledge and tools: j2ee1.4 SDK, and initial use, mastering basic JAX-RPC Web Service development skills, understanding the basics of jndi, being able to use it for simple programming, and general Java programming knowledge. Web Service clients We know that there are several types of Jax-RPC Web service clients: stub based;
Based on dynamic agent;
Based on the dynamic Call Interface (DII).
In fact, the above three types of clients use service interfaces as their creation factories, and service interfaces define methods such as the following: Some methods in routine 1 service interfaces
Call CreateCall ();
Call CreateCall (QName portname,
String OperationName);
Remote Getport (QName portname,
Class serviceendpointinterface);
Remote Getport (Class serviceendpointinterface);
As can be seen from the service interface, the call object and the remote object can be created, and calls or remote objects are what is required to invoke the Web service. By using the service interface, we can implement the call on the Web service client in the following ways: Routine 2 invoking the Web service on the client
Creates a Servicefactory object.
Servicefactory servicefactory
= Servicefactory.newinstance ();
Through Servicefactory
object to create a service object that invokes the Web service.
Service service =
Servicefactory.createservice
(Taxwsdlurl,
New QName (NamespaceURI,
ServiceName)); Gets the service endpoint instance.
Taxservice myproxy =
(Taxservice) Service.getport
(New QName (NamespaceURI,
PortName),
Taxservice.class);
Invoke the Web service.
Double Result=myproxy.calculatetax (5000);
As you can see, when you create a service instance, you need to use the specified WSDL file location, a valid namespace consisting of a service name and a namespace URI, which makes it complicated to create this instance. The Jax-RPC specification recommends using Jndi to find the service interface. Jndi makes invoking Web services as simple as invoking EJBS. You can get a Web service interface in just two steps: Initialize a namespace context, and find the Web service in this context. For example, you can do this in the following ways: Routine 3 calls Web services through Jndi 1
InitialContext IC = new InitialContext ();
Service ABF = (service) ic.lookup ("Java:comp/env/service/addressbookservice");
The name of the Web Service Reference (addressbookservice) is specified at deployment time, the JAVA:COMP/ENV is the context of the Jndi, and the service is the sub contexts of the Web services. So the Jndi name of a Web service generally consists of the following parts:
Web Service jndi= Client environment context +
Service (sub context) + Services reference name
We saw that the service interface was found in routine 3, but in development we could use another form of service reference: Find the Web service interface directly, as shown in routine 4. Routine 4 calls Web Service 2 via Jndi
Context ic= new InitialContext ();
Helloserviceinterface Service =
(helloserviceinterface) Ic.lookup
("Java:comp/env/service/helloservice");
As you can see in the following example, this method simplifies the call in a single step. Here's an example that demonstrates how to reference a Web service on the client side of the Java Web Service, and then find the Web service through Jndi. Develop and deploy a Web service we develop a Web service that provides personal income tax calculations, using EJB as the service endpoint. First, define an interface, as shown in routine 5. Routine 5 defines a service interface
Package com.hellking.
Study.webservice.tax; Import Java.rmi.Remote;
Import java.rmi.RemoteException; /**
* Personal income Tax Web services.
*/
public interface Taxservice
Extends Remote
{
Public double Calculatetax
(double salary) throws
Java.rmi.RemoteException;
}
It provides a method of calculating personal income tax. Routine 6 EJB part code
Double base=1200;
Personal income tax base,
2003 10 Beijing was 1200 yuan. Business logic Code,
Implements the methods defined in the service endpoint interface.
Public double Calculatetax (double salary)
{
Return Gettax (salary-base); }
The following is a detailed calculation method. The formula is suitable for the current personal income tax system.
Private double Gettax (double tax_salary)
{
Double tax=0.0d;
if (0>tax_salary) tax=0;
else if (0<tax_salary&&tax_salary <=500)
tax=tax_salary*0.05-0;
else if (500<tax_salary&&tax_salary<=2000)
tax=tax_salary*0.10-25;
else if (2000<tax_salary&&tax_salary<=5000)
tax=tax_salary*0.15-125;
else if (5000<tax_salary&&tax_salary<=20000)
tax=tax_salary*0.20-375;
else if (20000<tax_salary&&tax_salary<=40000)
tax=tax_salary*0.25-1375;
else if (40000<tax_salary&&tax_salary<=60000)
tax=tax_salary*0.30-3375;
else if (60000<tax_salary&&tax_salary<=80000)
tax=tax_salary*0.35-6375;
else if (80000<tax_salary&&tax_salary<=100000)
tax=tax_salary*0.40-10375;
else if (100000<tax_salary)
tax=tax_salary*0.45-15375; return tax;
}
The following is a configuration file that generates a mapping descriptor between the WSDL and Jax-RPC through a configuration file. The configuration file is as follows: Routine 7 Config.xml
<?xml version= "1.0"
encoding= "UTF-8"?>
<configuration
Xmlns= "http://java.sun.com
/xml/ns/jax-rpc/ri/config ">
<service
Name= "Mytaxservice"
Targetnamespace= "Urn:tax"
Typenamespace= "Urn:tax"
Packagename= "com.hellking.
Study.webservice.tax ">
<interface name= "com.hellking.
Study.webservice.tax.TaxService "/>
</service>
</configuration>
Note that the name of the Web service here is Mytaxservice, the namespace is "Urn:tax", and the Service interface is "Com.hellking.study.webservice.tax.TaxService", which will be used in later programming. Generate a Mapping.xml mapping file with the following command:
Wscompile-define-d. -nd.
-classpath. -mapping Mapping.xml
Config.xml

The example of sending a client provides two different ways to reference a Web service, as shown in routine 8. Routine 8 finds Web service package Com.hellking.study.webservice.tax on the client via Jndi; Import javax.naming.*;
Import Javax.xml.rpc.Service;
Import Javax.xml.namespace.QName; /**
*web Service Customer Demo: Find Web services through Jndi.
*/
public class Taxbean
{
/**
* The first way to find a service,
Direct access to the Mytaxservice interface.
*/
Public double getTax1 (double sal)
{
Double ret=0;
Try
{
Context Ctx=new InitialContext ();
Mytaxservice Taxservice
= (Mytaxservice) ctx.lookup
("Java:comp/env/service/tax");
The Taxservice service endpoint interface is obtained through Mytaxservice.
Taxservice
Tax=taxservice.gettaxserviceport ();
Ret=tax.calculatetax (SAL);
}
catch (Exception e)
{
System.out.println (e);
}
return ret;
}
/**
* Another way to find a service,
Access to the service interface,
And then through this interface to obtain specific services.
*/
Public double getTax2 (double sal)
{
Double ret=0;
Try
{
Context Ctx=new InitialContext ();
Service service=
(Service) Ctx.lookup
("Java:comp/env/service/tax2");
QName portqname= New QName
("Urn:tax", "Taxservice");
When you use this approach to get the service endpoint interface,
You need to specify a namespace.
Taxservice tax= (Taxservice)
Service.getport (Portqname,
Com.hellking.study.
Webservice.tax.TaxService.class);
Ret=tax.calculatetax (SAL);
}
catch (Exception e)
{E.printstacktrace ();
System.out.println (e);
}
return ret;
}
}
As you can see, the first method looks for the Mytaxservice interface, and the second method looks for the service interface. The specifics of that approach are related to the deployment description, and the differences in deployment are described later. Finally, a JSP is developed as a test client, which invokes the Web service via JavaBean, as shown in routine 9. JSP for routine 9 test
<%@ page contenttype= "text/html;charset=gb2312"%>
<%@ page import= "com.hellking.study.webservice.tax.*,javax.naming.*"%>
<jsp:usebean id= "tax" class= "Com.hellking.study.webservice.tax.TaxBean"/>
<%
Double salary=0;
try{
Salary=double.parsedouble
((String) Request.getparameter
("salary"));
}
catch (Exception e) {}
%>
<title> invoke Web services through Jndi. </title>
<body>
<div align= "center" >
2003 <%
Out.println ("Personal income tax is:<br>");
Out.println (Tax.gettax1 (Salary));
Out.println ("<br> Another way to invoke a Web service,
Personal income tax is:<br> ");
Out.println (TAX.GETTAX2 (Salary));
%>
<form action= "/tax/tax" >
<table border=1>
<tr bgcolor=654321>
&LT;TD > Input Salary </td>
<td><input Type=text name=salary></td>
</tr>
&LT;TR&GT;&LT;TD Colspan=2><input
Type=submit value= View ></td>
</tr>
</table>
</form>
</div>
</body>
Finally look at the specific deployment descriptor. Refer to the Web service in the client's description to open the J2EESDK deployment tool (perform%j2eesdk_home%\\appserver\\bin\\deploytool.bat or $j2eesdk/appserver/bin/ deploytool.sh), create a new Web application and add the test JSP above. At deployment time, the Web application will contain the files shown in Figure 1.
Figure 1 files included in the Web application
Click on the Web application, click on the "Web Services Refs" tab on the right and click the "Add" button. You can now add a Web service reference. Add a Web service reference called Service/tax, as shown in Figure 2.
Figure 2 Adding a Web service reference
Note that the service interface above is Com.hellking.study.webservice.tax.MyTaxService. Then click on "Container Managed Ports" as shown in Figure 3.
Figure 3 Adding container management endpoints
One way to do this is to refer to a Web service. The service interface is directly the case of services, and a Web service reference is added, as shown in Figure 4.
Figure 4 Adding another service reference
Note that the Service interface above is Javax.xml.rpc.Service, and the namespace (Urn:tax) and local part (Mytaxservice) are specified. Similarly, add a container management endpoint as shown in Figure 3, and the endpoint interface name and port component name are consistent with Figure 3. After the deployment above, the following deployment descriptors were actually generated in Web.xml. Deployment descriptor generated by routine 10
<service-ref>
<service-ref-name>
Service/tax</service-ref-name>
<service-interface>com.hellking.
Study.webservice.tax.
Mytaxservice</service-interface>
<wsdl-file>web-inf/wsdl
/mytaxservice.wsdl</wsdl-file>
<jaxrpc-mapping-file>mapping.xml
</jaxrpc-mapping-file>
<port-component-ref>
<service-endpoint-interface>
Com.hellking.study.
Webservice.tax.TaxService
</service-endpoint-interface>
<port-component-link>
Taxserviceport</port-component-link>
</port-component-ref>
</service-ref>
<service-ref>
<service-ref-name>
Service/tax2</service-ref-name>
<service-interface>
Javax.xml.rpc.service</service-interface>
<wsdl-file>web-inf/wsdl
Mytaxservice.wsdl</wsdl-file>
<jaxrpc-mapping-file>mapping.xml
</jaxrpc-mapping-file>
<service-qname
xmlns:service-qname_ns__=
"Urn:tax" >service-qname_ns__:mytaxservice
</service-qname>
<port-component-ref>
<service-endpoint-interface>
Com.hellking.study.webservice.tax.TaxService
</service-endpoint-interface>
<port-component-link>
Taxserviceport</port-component-link>
</port-component-ref>
</service-ref>
The following explains the description of the payment. A reference to a Web service is specified by an element, that is, the reference name to be used in client programming, that is, the service interface, two types, is Javax.xml.rpc.Service and com.hellking.study.webservice.tax.MyTaxService; is the valid namespace for the service, if directly using Com.hellking.study.webservice.ta X.mytaxservice as a service interface, you do not need to specify an element, or a reference to a service endpoint that references a defined element in Webservices.xml. Used to link to the specified in Webservices.xml, and the names of the two are consistent. After the call test deployment is complete, enter in the browser: Http://127.0.0.1:8080/tax/tax will appear as shown in Figure 5.
Figure 5 Invoking a Web service
Summing up through the above introduction, I believe that the reader of Web services have a comprehensive understanding of the reference. We can see that by using Jndi, invoking a Web service in the Java EE component, like object-oriented programming, can even invoke Web services without understanding concepts such as WSDL or XML.

Related Article

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.