This article mainly introduces the use of service, complex type parameters, or return value and the type of service-oriented / document, while also simply refers to the session management and security issues of Web services.
Summary My article "Application AXIS Start Web Service" introduces how to implement Web services through Axis projects. This article mainly introduces the structure of Axis. How to develop a simple web service in the way, and use a big segment to introduce the client programming of the web service, it should be said to be introduced using the AXIS development web service, this article assumes You have seen "Applying Axis Start Web Service" and has a certain foundation for Axis. On this basis, there are several aspects of the content we will introduce include web services, complex type parameters, or return values and facing The service type of the message / document will also briefly mention the session management and security issues of the web service.
Before starting our article, we also need to build an environment. We need a web application that supports web services and assumes that the name is AXIS, how to establish an introduction to the "Apply Axis Start Web Service" article.
Write Web services using custom release
Writing a web service using JWS files is convenient, fast, which can quickly release your existing classes into web services. But this is not a good idea because the problem triggered in this approach is that we have to release the source code of the existing class, because we don't want to do this; although you can use it first Tool development and debugging a Java file after renamed JWS, but this is a bit twisted, and it is not all methods in the class. You want to release it through web services, you must use these methods. The modifier is changed to not public, which is not synchronized with your original class, and later changes will be more troublesome.
Here I call the custom release method as a service method, as if the JSP's appearance does not cause the servlet to be out of favor, there is JWS, service method or its use of martial arts, and it is a lot of color. WEB services that publish a service method require two parts: class files and web services release description files. Below we use a simple example to tell this process.
First we need a Service class, this class does not have any difference with ordinary classes. Below is the class we implemented a city convenience service. We need two methods for the CityService class getzip and gettel to Web services, compile the file and put Class files are copied to
Package lius.axis.deemo;
/ **
* This class implements urban services to publish as web services
* @Author liudong
* /
Public class cityService {
/ **
* Get the zip code of the designated city
* @Param City
* @Return
* /
Public String getzip (string city) {
RETURN "510630";
}
/ **
* Get the long-distance area number of the designated city
* @Param City
* @Return
* /
Public String gettel (String City) {
Return "020";
}
}
The program has been completed, the following is to publish this web service. Open
XML Version = "1.0" encoding = "UTF-8"?>
handler>
handler>
requestflow>
globalconfiguration>
service>
requestflow>
transport>
responseflow>
transport>
deployment>
The part of the crude body is the configuration information we serve, start Tomcat and open the browser access address: http: // localhost: 8080 / axis / service / city? WSDL, below is the browser displays WDSL data for our web service.
Of course, this process is slightly troublesome compared to JWS mode, you can imagine you to publish a servlet, create a servlet class and add configuration information in Web.xml.
Handle complex type parameters and return values
The presenter we did is very simple. The parameters of the method and the return value are simple types of data, but it is often not so simple in the actual application process. When using an object-oriented programming language, we will want the data type to be an object. All content includes senders, recipients, send time, priority, information content, and more, if we make each content into a parameter, the parameters of this interface may be very much. Therefore, it is necessary to encapsulate objects.
The processing of complex type data is also very simple when using AXIS to write a Web service. Axis requires the writing of complex type objects to comply with JavaBean specification, simply that object's properties are accessed through the getter / setter method. Take a look at the WSDL information output by this simple example. For the sake of simplicity, we still use JWS to write, need to write three files: SMS.JWS, Message.java, response.java.
// File Name: SMS.JWS
Import lius.axis.deemo. *;
PUBLIC CLASS SMS {
/ **
* Short message sends a web service interface
* /
Public Response Send (Message MSG) throws exception {
System.out.println ("Content:" msg.getContent ());
Response res = new response ();
Res.SetMessage (MSG);
Res.setcode (0);
Res.SeterrorText ("");
Return res;
}
}
//Message.javaPackage lius.axis.de;
/ **
* Information to send
* @Author liudong
* /
Public class message {
PRIVATE STRING FROM;
PRIVATE STRING TO;
PRIVATE INT Priority;
Public string getContent () {
Return Content;
}
Public void setContent (string content) {
THIS.CONTENT = Content;
}
Public string getFrom () {
Return from;
}
Public void setfrom (string from) {
THIS.FROM = from;
}
Public int getPriority () {
Return priority;
}
Public void setpriority (int priority) {
this.priority = priority;
}
Public String Getto () {
Return TO;
}
Public void setto (string to) {
THIS.TO = TO;
}
}
//Response.javaPackage lius.axis.de;
/ **
* Information Send a response, here we have made a reference to the Message class
* @Author liudong
* /
Public class responsponse {
PRIVATE INT CODE;
// Send result code
Private string errortext;
PRIVATE MESSAGE MESSAGE
// Original information sent
Public int getcode () {
Return Code;
}
Public void setcode (int code) {
THIS.CODE = Code;
}
Public string geterortext () {
Return ErrorText;
}
Public void setErrorText (String ErrorText) {
THIS.ERRORTEXT = ErrorText;
}
Public message getMessage () {
Return Message;
}
Public void setmessage (message message) {
THIS.MESSAGE = Message;
}
}
Compile Message.java and Response.java and copy the compiled class file to the AXIS / Web-INF / CLASSES corresponding package, sms.jws copies to the Axis directory, access http: // localhost: 8080 / Axis / SMS .jws? WSDL can see WSDL information, which is different from the previously listed below (note the crudebed part content):
sequence>
complextype>
sequence>
complextype>
schema>
WSDL: TYPES>
Here, two types of Message and Response are the type of parameter type of our interface and the type of return value. Now use the WSDL2JAVA tool to generate the client Helper class to see what Axis has done? It will automatically help us generate two types of Message and Response, the package name and class name are consistent with our definitions, you can open and see how big is the difference between the classes we have just prepared? These two classes add a lot of methods such as GetTypedesc, GetSerializer, GetdeSerializer, and more. Now you can write a small program to test, we are not cumbersome. The processing of the service method web service is similar to JWS. Differently, the Service mode needs to be mapped in the Server-Config.WSDD to add types. The following example is given, and the reader can modify according to the actual situation.
Operation>
Encodingstyle = "http://schemas.xmlsoap.org/soap/encoding/" Qname = "ns: message" Serializer = "org.apache.axis.encoding.ser.beanserializerfactory" TYPE = "java: lius.axis.demo.message" xmlns: ns = "smsservice" /> Deserializer = "org.apache.axis.encoding.ser.beandeserializer" encodingstyle = "http://schemas.xmlsoap.org/soap/encoding/" Qname = "ns: response" Serializer = "org.apache.axis.encoding.ser.beanserializer" type = "java: lius.axis.demo.response" xmlns: ns = "smsservice" /> service> Other programming languages can also generate these complex types with tools included in the language itself. If you are troublesome, you can use XML to describe these complex types, which is much simpler. Web service type for message / document The service method we described earlier is based on RPC (remote procedure call) mode, which is also the most common way of web services. The type of message / document is different from the RPC that it provides a lower abstraction and requires more programming. The client can pass any XML document, and the resulting response does not necessarily be soapenvelope, which can return anything it needs, or even returns. Although this is very flexible for developers, this type of communication is not common in practical applications. WEB services for message / documents are mainly suitable for the following cases, such as batch processing, form-based data imports, require returning non-XML data, and require direct access to transport layers. For RPC types of services, you need to set a line Public element [] methodname (element [] Elems) Where MethodName is a method name for your own custom. MessageService.java can be found in the AXIS directory, which is a service class that completes this type, which is configured in WSDD as follows: XMLns: xsi = "http://www.w3.org/2000/10/xmlschema-instance"> service> deployment> Regardless of what is the web service, the client is the same, using WSDL2JAVA to generate the MessageService interface of the client Helper class: / ** * MessageService.java * * This file Was auto-generated from WSDL * by the apache axis wsdl2java emitter. * / Package liudong.axis.services.MessageService; Public interface messageservice extends java.rmi.remote { Public java.lang.object echoelements (java.lang.object part) throws java.rmi.remoteException; } Where can I get ... There is a sentence in the AXIS document. It is very interesting to the vast majority of "Where I can get ...", the answer is in the MessageContext class. With the MessageContext class, you can get the following content, an AxiSEngine instance reference; request, and information response; verification information, and instance references to the servlet specification, and so on. For example, when we need the client's IP address, we can get the following code snippet: / ** * Get client requests * @Return * / Private httpservletRequest getRequest () throws exception { MessageContext context = messageContext.getCurrentContext (); HTTPSERVLETREQUEST REQ = (httpservletRequest) Context.getProperty (httpconstants.mc_http_servletRequest); Return Req.getRemotehost (); } In class HTTPConstants, all constants starting with MC_ is the information you can get, such as the HTTP request in the corresponding servlet specification by MC_HTTP_SERVLETREQUEST. More detailed information can be obtained by querying the API document. Web service session management In Web services we can process session information with HTTP and HTTP cookies. As we introduce most of the management of Axis is done through the MessageContext instance. The following example first verifies the user's login account and password If it is correct, save the user's login information in the session, and provides the interface to obtain the password for the client. Import org.apache.axis.MessageContext; Import Org.apache.axis.Session.Session; Public class login { Public Boolean Login (String User, String Pass) { MessageContext mc = messageContext.getCurrentContext (); Session session = mc.getations (); session.set ("User", user); / / Save username and password Session.Set ("Pass", PASS); Return True; } Public string getpassword (string user) { MessageContext mc = messageContext.getCurrentContext (); Session session = mc.getations (); IF (user.equals (session.get ("User"))))) Return (String) session.get ("pass"); Return NULL; } } For the server, you only need to get the session object to be saved or read by the MessageContext instance, and you need to make a special setting for the client generated by the WSDL2JAVA tool through Axis, please see the following customers. End code snippet. Public static void main (string [] args) throws exception { LoginServiceLocator LSL = New loginserviceLocator (); Lsl.setMainTainSession (TRUE); Login login = lsl.getlogin (); IF (Login.login ("LD", "Haha")) System.out.println ("PWD:" Login.getPassword ("ld")); Else System.out.println ("Login Failed."); } The bold part in the code is to let AXIS help us automatically process the Cookie information returned by the server to ensure proper session. Protect web services The security problem of the network is always the first question, how can we make our web services more secure? For this purpose, the AXIS recommends that the following methods can be taken according to the actual needs. This manner needs to be configured on the web server while configuring the client support. This measure is effective to prevent data from being peeked during the network transmission. Rename some of AXIS existing names, such as Adminaservice, AxisServlet, delete some useless programs in the AXIS directory, such as HAPPYAXIS.JSP, and some useless JAR packets. Stop the functionality of all services by setting the value of Axis.EnableListQuery. It is forbidden to automatically generate WSDL functions using filters to add some verification capabilities, such as client addresses, and more. The most commonly used cases are not more than a few, as for more detailed information, refer to the detailed introduction of the DOCS / Reference.html file in the AXIS unzipped directory. Reference "Application AXIS Start Web Service" http://www-900.ibm.com/developerWorks/WebServices/ws-startaxis/index.shtml IBM Developer Site Web Service Zone http://www-900.ibm .com / webservices apache website AXIS project http://ws.apache.org/axis/ w3c web service http://www.w3.org/2002/ws/