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

What is the Holder type?

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

Share

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

This article mainly introduces "what is the Holder type". In the daily operation, I believe many people have doubts about what the Holder type is. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts about "what is the Holder type?" Next, please follow the editor to study!

To figure out what the Holder type is, you have to understand the concepts of the IN parameter, the OUT parameter, and the INOUT parameter.

IN parameters are the parameters of JAVA, while OUT and INOUT parameters are not inherent to JAVA.

Copy delivery: IN parameter

The java programming language replicates all the original values passed as parameters to the method call, that is, the value passed is a copy of the variable used in the method call, not the original value itself. For example, define a method

Test (int intValue,Date dateValue) {intValue=5; dateValue=new Date (0);}

When making the following call

Int intValue=1; Date dateValue=new Date (); test (intVlaue,dateValue); System.out.println (intValue) / / print 1 System.out.pritnln (dateValue) / / print the current time.

However, the value of the parameter is modified in the test method. The actual parameter values have not changed.

Reference passing: INOUT parameter OUT parameter.

In the programming language that implements reference passing, if the two parameters of the test method are defined as reference passing, after the above test method is called, and then print the values of intValue and dateValue, you will find that these two values have changed. But the OUT parameter is more like a return value because the value is passed from the method rather than passed in. With the number of OUT parameters, the value of the variable is not accessed before the method is activated, that is, the value passed in is ignored.

Holder class:

The INOUT,OUT parameter is supported in JAX-RPC. Holder is an interface that simply provides a remedy to support reference passing in java. This makes it necessary to use java to communicate with web services written in programming languages that support INOUT,OUT parameters.

Map HOLDER types from WSDL:

Interface definition

Public interface HolderTestBeanInterface1 extends Remote {public void echoCalendar (CalendarHolder val, Date date) throws RemoteException; public void echoBeanHolder (BeanTestHolder beanTestHolder,BeanTest beanTest) throws RemoteException;}

WSDL document

< ?xml version="1.0" encoding="UTF-8"?>

< definitions name='HolderTestBeanInterface1' targetNamespace='http://holder' xmlns='http://schemas.xmlsoap.org/wsdl/' xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/' xmlns:tns='http://holder' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>

< message name='HolderTestBeanInterface1_echoCalendar'>

< part name='Calendar_1' type='xsd:dateTime'/>

< part name='Date_2' type='xsd:dateTime'/>

< /message>

< message name='HolderTestBeanInterface1_echoCalendarResponse'>

< part name='Calendar_1' type='xsd:dateTime'/>

< /message>

< operation name='echoCalendar' parameterOrder='Calendar_1 Date_2'>

< input message='tns:HolderTestBeanInterface1_echoCalendar'/>

< output message='tns:HolderTestBeanInterface1_echoCalendarResponse'/>

< /operation>

< /portType>

...

The part element marked Calendar_1 is in both the input message and the ouput message, and the jax-rpc compiler knows that it is an INOUT type and also requires a Holer type. If you define a part element in an output message, it means that it is a return value or an OUT parameter. If part is an OUT parameter, the part tag is listed in the parameterOrder attribute of the operation element. The out parameter also requires a Holder type.

A Holder class is provided in the Javax.xml.rpc.holders package for mapping to INOUT and OUT parameters of the java primitive type. Such as IntHolder, etc., but also provides a set of Holder types defined for nillable built-in types, such as IntegerWrapperHolder.

Customize the Holder type. The Holder flag interface must be implemented, the property variable name must be value, and an empty constructor must be defined like this:

Public class BeanTestHolder implements javax.xml.rpc.holders.Holder {public BeanTest value; public BeanTestHolder () {} public BeanTestHolder (BeanTest beanTest) {value=beanTest;}}

The specific implementation of the Holder type of Jboss Web Service, using Jboss version 4.04.

Define a bean class:

Public class BeanTest {private String beanName; public BeanTest () {} public String getBeanName () {return beanName;} public void setBeanName (String beanName) {this.beanName = beanName;}}

Customize a Holder for bean:

Package holder; public class BeanTestHolder implements javax.xml.rpc.holders.Holder {public BeanTest value; public BeanTestHolder () {} public BeanTestHolder (BeanTest beanTest) {value=beanTest;}}

Define an interface:

Public interface HolderTestBeanInterface1 extends Remote {public void echoCalendar (CalendarHolder val, Date date) throws RemoteException; public void echoBeanHolder (BeanTestHolder beanTestHolder,BeanTest beanTest) throws RemoteException;}

Implementation class of the interface:

Import javax.xml.rpc.holders.*; import java.util.Date; public class HolderTestBean implements HolderTestBeanInterface1 {public void echoCalendar (CalendarHolder val,Date date) {System.out.println ("echoCalendar:" + val.value.getTime ()); val.value.setTime (new Date ()); System.out.println (date);} public void echoBeanHolder (BeanTestHolder beanTestHolder,BeanTest beanTest) {BeanTest beantest=beanTestHolder.value System.out.println (beantest.getBeanName () + "holder"); beantest.setBeanName ("new name"); System.out.println (beantest.getBeanName () + "holder"); System.out.println (beanTest.getBeanName () + "bean"); beanTest.setBeanName ("new name too"); System.out.println (beanTest.getBeanName () + "bean");}}

Configuration file wstools-config.xml for the wstools tool that comes with jboss

< ?xml version="1.0" encoding="UTF-8"?>

< !-- wstools -cp classes -config wstools-config.xml -->

< 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="HolderTestBeanInterface1" style="rpc" endpoint="holder.HolderTestBeanInterface1"/>

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

< mapping file="HolderTestBeanInterface1.xml"/>

< webservices servlet-link="HolderTestBeanInterface1"/>

< /java-wsdl>

< /configuration>

Generate the required webservices.xml,jax-rpc mapping files and wsdl documents

Wstools-config wstools-config.xml

Put the generated wsdl folder, webservices.xml and mapping files in the web-inf directory.

Configure the web.xml file

< servlet>

< display-name>

HolderTestBeanInterface1 Servlet

< servlet-name>

HolderTestBeanInterface1

< servlet-class>

Holder.HolderTestBean

< /servlet>

< servlet-mapping>

< servlet-name>

HolderTestBeanInterface1

< url-pattern>

/ HolderTestBeanInterface1

< /servlet-mapping>

< servlet-mapping>

< servlet-name>

HolderTestBeanInterface1

< url-pattern>

/ services/*

< /servlet-mapping>

Package it into a war file and publish it

Calls from the client:

Import java.net.URL; import javax.xml.rpc.*; import javax.xml.namespace.QName; import java.util.*; import java.io.File; import javax.xml.rpc.holders.CalendarHolder; import org.jboss.ws.jaxrpc.ServiceFactoryImpl; public class ClientTest {private HolderTestBeanInterface1 getPort () throws Exception {ServiceFactoryImpl factory = new ServiceFactoryImpl () URL wsdlURL = new File ("holderTest/WEB-INF/wsdl/HolderTestBeanInterface1.wsdl"). ToURL (); URL mappingURL = new File ("holderTest/WEB-INF/HolderTestBeanInterface1.xml"). ToURL (); QName qname = new QName ("http://holder"," HolderTestBeanInterface1 "); Service service = factory.createService (wsdlURL, qname, mappingURL) HolderTestBeanInterface1 port = (HolderTestBeanInterface1) service.getPort (HolderTestBeanInterface1.class); return port;} public static void main (String [] args) throws Exception {ClientTest clienttest = new ClientTest (); HolderTestBeanInterface1 h=clienttest.getPort (); Date date=new Date (); System.out.println (date+ "date begin...) ); GregorianCalendar value = new GregorianCalendar (2006, 1, 1, 23, 59, 59); CalendarHolder holder = new CalendarHolder (value); System.out.println (holder.value.getTime () + "calendarHolder begin... "); h.echoCalendar (holder,date); System.out.println (holder.value.getTime () +" calendarHolder end... "); System.out.println (date+" date end... "); BeanTest beanTest=new BeanTest (); beanTest.setBeanName (" test "); BeanTest beanTest2=new BeanTest (); beanTest2.setBeanName (" test2 ") System.out.println (beanTest.getBeanName () + "holder begin..) "); System.out.println (beanTest2.getBeanName () +" bean begin.. "); BeanTestHolder beanTestHolder = new BeanTestHolder (beanTest); h.echoBeanHolder (beanTestHolder,beanTest2); System.out.println (beanTest2.getBeanName () +" bean end.. "); System.out.println (beanTestHolder.value.getBeanName () +" holder end.. " ");}} at this point, the study of" what is the Holder type "is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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