Use Web Service in JSP program of WEB Project

Source: Internet
Author: User

. Create a web project testservice
The directory is as follows:
CN -- Test -- service -- impl

2. packages to be imported
Activation-1.0.2.jar
Commons-codec-1.3.jar
Commons-httpclient-3.0.jar
Commons-logging-1.0.4.jar
Jaxen-1.1-beta-8.jar
Jdom-1.0.jar
Log4j-1.2.x.jar
Mail-1.3.3_01.jar
Spring-1.2.x.jar
Stax-api-1.0.jar
Wsdl4j-1.5.2.jar
Wstx-asl-2.9.jar
Xbean-2.1.0.jar
Xbean-spring-2.2.jar
Xfire-all-1.0.jar
XmlSchema-1.0.jar

3. Data publishing end
(1) Compile the publishing Interface
Package CN. Test. Service;

Public interface adservice {
String [] [] getdatas ();
}

(2) Compile the implementation class
Package CN. Test. Service. impl;

Import CN. Test. Service. adservice;

Public class adserviceimpl implements adservice {

Public String [] [] getdatas (){

String ads [] [] = new string [3] [3];

For (INT I = 0; I <3; I ++ ){
For (Int J = 0; j <3; j ++ ){
ADS [I] [J] = string. valueof (I * j );
}
}
Return ads;
}

}

4. Add the xfire configuration file services. xml.
Add/src/META-INF/xfire/services. XML to the src directory
<Beans xmlns =" Http://xfire.codehaus.org/config/1.0 ">
<Service>
<Name> ad </Name> <! -- This name is important. It is used for calling. -->
<Namespace> http: // localhost/adservice </namespace> // use this address to access
<Serviceclass> CN. Test. Service. adservice </serviceclass>
<Implementationclass>
CN. Test. Service. impl. adserviceimpl // implementation class
</Implementationclass>
<! -- This is a modifier. It seems that an error will be reported when spring integration is called.
<Inhandlers>
<Handler
Handlerclass = "org. codehaus. xfire. Demo. Handlers. checkversionhandler"/>
</Inhandlers>
-->
</Service>
</Beans>

5. Configure web. xml
Add the following content to Web. xml:
<Servlet>
<Servlet-Name> xfireservlet </servlet-Name> // use the built-in Servlet
<Display-Name> xfire servlet </display-Name>
<Servlet-class>
Org. codehaus. xfire. Transport. http. xfireconfigurableservlet
</Servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-Name> xfireservlet </servlet-Name>
<URL-pattern>/services/* </url-pattern> // filtered path
</Servlet-mapping>

 

6. Test whether xfire has been deployed successfully.
 Http: // localhost/testservice/services/ad? WSDL

7. Compile the client
Package CN. Test. Service;

Import java.net. malformedurlexception;
Import org. codehaus. xfire. xfire;
Import org. codehaus. xfire. xfirefactory;
Import org. codehaus. xfire. Client. xfireproxyfactory;
Import org. codehaus. xfire. Service. Service;
Import org. codehaus. xfire. Service. Binding. objectservicefactory;

Public class callservice {
Public String [] [] getdata (){
Service servicemodel = new objectservicefactory (). Create (adservice. Class); // note that the interface used here is the publish interface adservice. Class.

Xfire = xfirefactory. newinstance (). getxfire ();
Xfireproxyfactory factory = new xfireproxyfactory (xfire );

String serviceurl =" Http: // localhost/testservice/services/AD ";

Adservice client = NULL;
Try {
Client = (adservice) Factory. Create (servicemodel, serviceurl); // you can use the class name and address to create a class instance.
} Catch (malformedurlexception e ){
System. Out. println ("wsclient. callwebservice (): exception:" + E. tostring ());
}


String [] [] datas = NULL;
Try {
Datas = client. getdatas ();
} Catch (exception e ){
System. Out. println ("wsclient. callwebservice (): exception:" + E. tostring ());
}

// Print all the data one by one
Int Len = datas. length;
For (INT I = 0; I <Len; I ++ ){
Int len2 = datas [I]. length;
For (Int J = 0; j <len2; j ++ ){
System. Out. Print ("\ t" + datas [I] [J]);
}
System. Out. println ();
}

Return datas;
}
}

8. Write a JSP to call the client
<% @ Page Language = "Java" Import = "Java. util. *, CN. yicha. Service. *" pageencoding = "UTF-8" %>
 
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en">
<HTML>
<Head>
<Title> my JSP 'index. jsp 'starting page </title>
</Head>
<Body>
WebService test!

<%
Callservice cs = new callservice ();
CS. getdata ();

%>
</Body>
</Html>

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.