Entering the Web Service - Axis + Tomcat to simulate a bank deposit service (ZZ learning)

zhaozj2021-02-16  130

Download Apache AXIS to implement SOAP (Simple Object Access Protocol). http://ws.apache.org/axis/

The most stable version is currently 1.1

SOAP is a lightweight protocol for exchange information in a dispersed or distributed environment. SOAP is based on XML, consisting of three parts: a must-have SOAP package, an optional SOAP head and a must-have SOAP body.

Usually, SOAP = HTTP RPC XML. That is, SOAP uses HTTP as the underlying communication protocol, as an interactive mode, XML as the format of data transmission.

Web Service develops the skills that need to be mastered: Java, thread, synchronization, ClassLoader, Error Exclude, NPE (NullPointerexception) and other common errors, and can handle, servlet, how to release web applications to application servers (Tomcat, etc.)

TCP / IP, SOCKET API, XML ... more scary! To learn: (1 Installation Configuration AXIS ~~ Install Tomcat 4.x or above version (4.X version with XML parser) ~~ Unzip the axis1.1.zip package, find the Axis folder under the WebApps directory Copy the AXIS folder to the WebApps folder in Tomcat. ~~ Turn all the files in the web-inf / lib in the AXIS directory in Tomcat Copy to Tomcat's common / lib directory. (Wherever you want in Axis Add JAR files in the lib, you have to copy a copy to Tomcat's common / lib directory). ~~ My Computer -> Properties -> Advanced -> Environment Variables -> System Variables Add: AXIS_HOME% Tomcat_Home% / WebApps / AXIS (Tomcat catalog) AXIS_LIB% AXIS_HOME% / lib AXISCLASSPATH% AXIS_LIB% / AXIS.JAR;% AXIS_LIB% / Commons-Discovery.jar;% AXIS_LIB% / Commons-logging.jar;% AXIS_LIB% / jaxrpc.jar; % AXIS_LIB% / Saaj.jar;% AXIS_LIB% / LOG4J-

1.2.8

.jar;% AXIS_LIB% / XML-APIS.JAR;% AXIS_LIB% / XerceptImpl.jar Modify ClassPath, plus:% AXIS_LIB% / AXIS.jar;% AXIS_LIB% / Commons-Discovery.jar;% AXIS_LIB% / Commons-logging.jar;% AXIS_LIB% / jaxrpc.jar;% AXIS_LIB% / saaj.jar;% AXIS_LIB% / LOG4J-

1.2.8

.jar;% AXIS_LIB% / XML-APIS.JAR;% AXIS_LIB% / XERCESIMPL.JAR Note Check if the classpath is correct: (Some software will set a classpath in the user variable area, which will affect, such as: Xmlspy) in DOS mode Command: echo% classpath% ~~ Check configuration: Start Tomcat, access http: // localhost: 8080 / axis /,

Verify whether Axis works: http: // localhost: 8080 / axis / happyaxis.jsp If normal display is normal. In the figure, the last word "The Core Axis Libra ...." To ensure that Core Axis Libraries Are Present, if you have any core library is missing, find the corresponding JAR file on the appropriate link to which it provides, and COPY In the Axis folder web-inf / lib in the tomcat directory, don't forget that Tomcat's own common / lib is also copy. Until all Core Library arepent. It's basically configured. Now let's start making an example of a bit of WEB SERVICE. Step by step :) Target: Simulate a bank deposit system, the specific implementation of the deposit is used as a service to provide AXIS to provide two service release methods, one is instant deployment, one is custom release ( Custom deployment. 1. Use instant release Java Web Service (JWS) "for instant release is one of the features of Axis, using instant release to enable users to provide the source code of the Java class providing service, you can quickly release it into a web service. Whenever the user calls this type of service, AXIS will automatically compile, even if the server is restarted, it is not necessary to do anything, using very simple and fast. "Using instant release first needs a Java source file that implements service functions, will Its extension is changed to .jws (Java Web Service Abbreviation) and then put the file in the WebApps / Axis directory below Tomcat. But JWS web service release is a very simple web service release method, you can't use the package in the page, and because the code is compiled in the runtime, you are also difficult to find the error. It is not good, but we still come to see an example: Everyone finds the Calculate.jws file in Tomcat's WebApps / Axis, then opens. No need to compile. Publish: http: // localhost: 8080 / axis / distance .jws? WSDL If you see the following WSDL description means that you have been successful: How to use this service? Is there a CalcClient.class file in Tomcat 5.0 / WebApps / Axis / Web-INF / CLASS / SAMPLES / UserGuide / Example2? This file is an example of the client of the Calculate provided by the corresponding AXIS. In DOS mode, in Tomcat 5.0 / WebApps / Axis / Web-INF / CLASSES / Directory: Java Samples.userGuide.example2.calcclient Add 3 4 isn't you get 7? If you appear NodeFClass's error, use ECHO to check each environment variable. Generally is wrong here. The source code corresponding to Calculator is inside Samples / UserGuide / Example2 in the downloaded Axis.zip.

2. Using custom release Web Service Deployment Descriptor (WSDD) "Instant release is an exciting technology, it makes the development of web services so simple; however, it is not always the best choice, such as some applications The system is provided by the third party. We don't buy source code, only .class files, but we hope to release some of this application system into a web service, enable it to create a role in a larger range, this time is released Technology is powerful. In addition, instant issues are not flexible, and more service configuration cannot be made, which makes it unable to meet the needs of some specific systems. "Okay to us, I don't say anything good to make custom release. Everyone will look down, naturally, there will be experience :) First give our simulation bank access Java file: Account.java package com.duckur; public class? Acid {/ ******** *********************************************************** *** / * Get an account from the random number (one-time valid) * / public static int function (int) (java.lang.math.random () * 10000); // Simulation, everyone can read from the file Take data, or remove this Fund value from the database / ********************************************* ************************* * Check the validity of the input * / public boolean checkinput (int Money) // can only take the number {IF (Money> Fund ) {RETURN FALSE;} else {return true;}}

/ ************************************************** ************ / * Deposit * / public int Deposit (int Money) {fund? = Fund Money; returnif;

/ ************************************************** ************ / * withdrawal * / public int withdraw (int money) {if (checkinput (money)) {fund = fund;}

/ ************************************************** ************ / * Get the current account value * / public int getaccount () {return flag;}} then compile, the successful Class file should be placed under Tomcat WebApps / Axis / Web-INF / COM / DUCKUR / below. Hereinafter: WebApps / Axis / Web-INF / COM / DUCKUR / DCC under Tomcat, the name, entrance and other information about the service:

The display of the WSDD file has a problem download. Which is the name of the service name is Account, Java: RPC indicates a Java RPC service, and the class for this processing is org.apache.axis.Providers.java.rpcProvider. "We are through one

The label tells the RPC service to call the class, and another

The label tells the engine that can call any public method in this class. You can also specify that those methods can be called by using the namespace or some method you can call. "Of course, you can also use AXIS for a client management tool -AdminClient to complete the custom release of the service. Not saying :) Then on DOS to this directory, java org.apache.axis.client.adminClient deploy.wsdd If there is: Processing File Deploy.wsdd

DoneProcessing

This indicates that the Capacity service custom release is completed. Ok, now you can view the WSDL described by http: // localhost: 8080 / axis / service / account? WSDL. Web Service is released now. This is called now. Here is the class and method you need to use by one of the most basic calls: (Remove the current amount) ... // Newly built a service object service service = new service (); ?? / / Call the CREATECALL () method of the service object Returns a command call call = (call) service.createcall () ;?

?

// sets the address of the target service endpoint.

Call.SettargetendPointAddress (New Java.Net.URL ("http: // localhost: 8080 / axis / service / lxaccount");

//? Sets the name of the operation to be invoked using this call instances. INVOKED. - THIS CALL INSTANCE

Call.SetoperationName ("getaccount"); // convenience method to invoke a // method with a default (empty) namespace integer myfund = (integer) Call.Invoke (new object [] {}; ... detailed function can Refer to the development of the API in DOCS in the downloaded Axis.zip. The following is a detailed code: You can execute after compiling. Lxclient.java import javax.swing. *; Import java.awt. *; Import java.awt.event. *;

Import org.call; import org.apache.axis.client.Service; import org.apache.axis.encoding.xmltype; import org.apache.axis.utils.Options; import javax.xml.rpc .Parametermode; import javax.xml.namespace.qname;

public class LxClient extends JFrame {JTextField jTextField1 = new JTextField (); JLabel jLabel3 = new JLabel (); JLabel jLabel1 = new JLabel (); JButton jButton1 = new JButton (); JButton jButton2 = new JButton (); JButton jButton3 = new JButton (); jlabel jlabel2 = new jlabel (); jtextfield jtextfield2 = new jtextfield ();

Public lxclient () {Try {jbinit ();} catch (exception e) {E.PrintStackTrace ();}}

Public static void main (string args []) {lxclient myframe = new lxclient (); myframe.setsize (400, 200); myframe.setvisible (TRUE);

}

private void jbInit () throws Exception {this.getContentPane () setLayout (null);. jTextField1.setText ( ""); jTextField1.setBounds (new Rectangle (131, 51, 80, 24)); jLabel3.setText ( "A0317286 Li Xun "); Jlabel3.SetBounds (New Rectangle (102, 15, 108, 17)); Jlabel1.Settext (" Fill in the amount: "); Jlabel1.SetBounds (New Rectangle (64, 54, 55, 21))) ; jButton1.setBounds (new Rectangle (50, 96, 58, 23)); jButton1.setText ( "deposit"); jButton1.addActionListener (new LxClient_jButton1_actionAdapter (this)); jButton2.setBounds (new Rectangle (126, 96, 63 , 23)); JButton2.Settext ("withdrawal"); jButton2.addActionListener (new lxclient_jbutton2_actionadapter (this)); JButton3.SetBounds (New Rectangle (205, 94, 81, 24); jButton3.Settext ("View amount" ); jButton3.addActionListener (new LxClient_jButton3_actionAdapter (this)); jLabel2.setText ( "current amount:"); jLabel2.setBounds (new Rectangle (59, 136, 74, 21)); jTextField2.setEditable (false); jTextField2 .SETTEXT (""); jtextfield2.setboun DS (New Rectangle (127, 133, 85, 25); this.getContentPane (). add (jtextfield1, null); this.getContentPane (). add (jlabel3, null); this.getContentPane (). Add (jlabel1 , NULL); this.getContentPane (). add (jbutton2, null); this.getContentPane (). add (jbutton1, null); this.getContentPane (). add (jbutton3, null); this.getContentpane (). Add () (Jlabel2, NULL); this.getContentPane (). add (jtextfield2, null);}

/ ************************************************** ************************** Call WebService * / Deposit * / Void JButton1_ActionPerformed (ActionEvent E) {Try {String Endpoint = "http: // Localhost: 8080 / axis / services / lxaccount "; string method =" deposit "; integer i = integer.valueof (jtextfield1.gettext ()); service service = new service (); call call = (call) service.createCall ( ); call.SettargetendPointDress (new java.net.URL (endpoint); call.setoperationname (Method);

Integer myfund = (integer) call.invoke (new object [] {i}; // update data jtextfield2.settext (myfund.tostring ()); jlabel1.Updateui ();} catch (exception ex) {system.err } (} / ************************************************************************************** *********************************************** Withdrawal * / void jButton2_ActionPerformed (ActionEvent E) {Try { String endpoint = "http: // localhost: 8080 / axis / services / lxaccount"; string method = "withdraw"; integer i = integer.valueof (jtextfield1.gettext ()); service service = new service (); call call = (Call) service.createcall ();

Call.SetTargetendPointDress (new java.net.URL (endpoint)); call.setoperationname (Method);

Integer myfund = (integer) call.invoke (new object [] {i}; // update data jtextfield2.settext (myfund.tostring ()); jlabel1.Updateui ();} catch (exception ex) {system.err } (} / ************************************************************************************** ************************************* Show current amount * / void jbutton3_actionperformed (ActionEvent E) { Try {string endpoint = "http: // localhost: 8080 / axis / service / lxaccount"; string method = "getaccount"; service service = new service (); call call = (call) service.createCall ();

Call.SetTargetendPointDress (new java.net.URL (endpoint)); call.setoperationname (Method);

Integer myfund = (integer) Call.invoke (new object [] {}; // Update data jtextfield2.settext (myfund.tostring ()); jlabel1.Updateui ();} catch (exception ex) {system.err. PRINTLN (EX.TOSTRING ());}}}

Class lxclient_jbutton1_actionadapter import.Ava.awt.event.Adlistener {lxclient adaptee

LxClient_jbutton1_actionadapter (lxclient adaptee) {this.adaptee = adaptee;}

Public void actionperformed (ActionEvent E) {adaptee.jbutton1_actionperformed (e);}}

Class lxclient_jbutton2_actionAdapter Implements java.awt.event.ActionListener {lxclient adaptee

Lxclient_jbutton2_actionadapter (lxclient adaptee) {this.adaptee = adaptee;}

Public void actionperformed (ActionEvent E) {adaptee.jbutton2_actionperformed (e);}}

Class lxclient_jbutton3_actionAdapter Implements java.awt.Event.ActionListener {lxclient adaptee

LxClient_jbutton3_ActionAdapter (lxclient adaptee) {this.adaptee = adaptee;}

Public void ActionPerformed (ActionEvent E) {adaptee.jbutton3_actionperformed (e);}} Posted on 2004 August 06, 1:35 AM

转载请注明原文地址:https://www.9cbs.com/read-8591.html

New Post(0)