1. Eat stuffing in the first port, configure a web service, the simplest method
============================================================================================================================================================================================================= =================================================================================
In order to allow Web services to run, give the prototype of a web service to facilitate discussions.
We started from a simplest example and only give things.
The software you need:
Tomcat4.1.2
2. A Java Compiler, JDK, or JBuilder, etc., which is to compile our Java source program, regardless of Web services.
needed file:
Sayhello.java
2.web.xml
3.Server-config.xml
4.java packages: axis.jar, jaxrpc.jar, tt-bytecode.jar, wsdl4j.jar, XerceSimpl.jar, XML-Apis.jar
As for how Tomcat installed, I don't say it, there are many articles about Tomcat installation.
These six packages, can be obtained from IBM and Apache's website.
Just only need this, we can deploy your web service. .
Below is a directory structure:
WebApps / Test / Web-INF / Web.xml
WebApps / Test / Web-INF / Server-Config.wsdd
WebApps / test / web-inf / class / selfhello.class
WebApps / Test / Web-INF / LIB / XXX.JAR --- Six packages required
Web.xml
---------------------------------------------
Xml Version = "1.0" encoding = "ISO-8859-1"?>
servlet>
servlet-maping>
web-app>
---------------------------------------------
Server-config.wsdd
---------------------------------------------
XML Version = "1.0" encoding = "UTF-8"?>
service>
requestflow>
transport>
deployment>
---------------------------------------------
Sayhello.java
---------------------------------------------
Public Class Sayhello
{
Public String Sayhelloto (String Aname)
{
Return "How are you," aname;
}
}
---------------------------------------------
Suppose IP address 192.168.0.1, port number is 80, we enter the following URL get a list of service (of course there is only one):
http://192.168.0.1/test/services
If your port number is 8080, you should enter http://192.168.0.1:8080/test/services, followed by the same.
The browser shows:
----------------
| And now ... Some Services |
| SAYHELLOSERVICE (WSDL) |
| .SAYHELLOTO |
----------------
Sayhelloservice is our service name, right (WSDL) is a link to the WSDL document that point Hylloservice,
This document is generated automatically by AXIS.
Sayhelloto is of course our way. . .
Click (WSDL) to link or enter the URL below, get wsdl: http: //192.168.0.1/test/services/sayhelloservice? WSDL
The browser displays the WSDL document for Sayhelloservice:
XML Version = "1.0" encoding = "UTF-8"?>
wsdl: message>
wsdl: message>
wsdl: Operation>
wsdl: porttype>
wsdl: Input>
wsdl: Output>
wsdl: Operation>
wsdl: binding>
wsdl: port>
wsdl: service>
WSDL: Definitions>
We don't even need the client, you can view the service to deploy success and get the return result.
Use the GET method to get the SOAP stream, we want to use the following URL:
(Really calling the web service, use the post method, this will be explained later)
http://192.168.0.1/test/services/sayhelloservice?method =sehelloto&aname=EVERYBODY
The browser shows garbled, let's right click to view the source file, the result is as follows:
got response message p>
XML Version = "1.0" encoding = "UTF-8"?>
SayHLLOTORESPONSE>
soapenv: body>
soapenv: envelope>
Is this what we want? This is just a SOAP message sent back from the server side, but the results we want are in it. . .
In order to truly call our web service, a client is given below:
Import org.apache.axis.client.call;
Import org.apache.axis.client.service;
Import javax.xml.namespace.qname;
Public Class Test
{
Public static void main (string [] args)
{
Try {
String endpoint = "http://192.168.0.1/test/services/SAYHELLOSERVICE";
Service service = new service ();
Call call = (call) service.createcall ();
Call.SettargetendPointdaddress (New Java.net.URL (Endpoint));
Call.SetoperationName (New QName ("http:// SayhelloService", "Sayhelloto");
String Ret = (String) Call.invoke (New Object [] {Args [0]});
System.out.println (RET);
} Catch (exception e) {
E.PrintStackTrace ();
}
}
}
Note To configure the correct ClassPath, make sure that the compiler can find to axis.jar and jaxrpc.jar, otherwise compiling will not pass.
Run this Class with the following command line:
Java Test Everybody
We will get: how are you, everybody
This is what we really want. . .
2. Facing the bottom, our web service runs
============================================================================================================================================================================================================= ===================================================== A profile web.xml and server-config.wsdd may not be able to understand at once.
Let's review the mapping mode of the servlet.
We know that servlet is inherited from javax.servlet.http.httpservlet, and the server is loaded into the JVM execution, then outputs an HTML stream to the client.
Servlet's web.xml file (located in a webapps / foo / web-inflicity):
XML Version = "1.0" encoding = "UTF-8"?>
"http://java.sun.com/j2ee/dtds/web-app_2.2.dtd">
servlet-maping>
web-app>
Invoker servlet is actually: org.apache.catalina.servlets.invokerServlet
Provide a small service program by class name. For example, if you call foo / servlet / helloservlet,
The Invoker Servlet will be loaded into the Helloservlet (if it is in its class path) and is executed.
Looking at the web.xml, it seems to give only a servlet mapping without defining the Invoker Servlet.
In fact, Invoker Servlet is defined in Web.xml in Tomcat's confeding ::
Org.apache.catalina.servlets.invokerseervlet
servlet-class>
init-param>
servlet>
So, if you leave Tomcat_Home / conf / web.xml, we define a web.xml, which seems to be more clearly explained:
XML Version = "1.0" encoding = "UTF-8"?>
"http://java.sun.com/j2ee/dtds/web-app_2.2.dtd">
Org.apache.catalina.servlets.invokerseervlet
servlet-class>
init-param>
servlet>
servlet-maping>
web-app>
That is, all the URLs of all / servlet / * patterns will be handed over to org.apache.catalina.servlets.invokerseRVlets.
Or, all / servlet / * modes of the URL, actually call the InvokerServlet, and the InvokerServlet itself is also
A servlet, which is also inherited from httpservlet.
In this way, our own servlet can be performed through a specific URL, ie / servlet / ourservlet.
Of course, if you are happy, you can define any URL Pattern, not necessarily / servlet / *, this, just as we follow
The method of applying the AXIS handling the SOAP message.
Further, if you don't want the InvokerServlet in the middle, we can certainly define their own servlet directly:
XML Version = "1.0" encoding = "UTF-8"?>
"http://java.sun.com/j2ee/dtds/web-app_2.2.dtd">
com.foo.myservlet
servlet-clars> servlet>
servlet-maping>
web-app>
JSP is also the same, with the above analysis,
See the following statement in tomcat_home / conf / web.xml, you can handle the method of JSP, this is no longer nonsense:
....
init-param>
servlet>
servlet-maping>
....
Let's enter the topic.
Let's first see the Web.xml of the Web Service:
Xml Version = "1.0" encoding = "ISO-8859-1"?>
servlet>
servlet-maping>
web-app>
All / Services / * mode URL will be handed over to org.apache.axis.transport.http.axisservlet processing,
Axisservlets are of course also inherited from httpservlets. That's why we deploy the web service to be in the call.
The service name is added before adding services.
It can be said that AxisServlet is the entry of all web services calls.
So what work does AxisServlet did after the WEB service call? The client is used by call.invoke () calling the web service is POST, so the entrance is axisservlet.dopost ...
Not AxisServlet.Doget ...
Let's take a look at the Dopost function of AxisServlet. Here is only a key statement and comment:
/ **
* Process a post to the servlet by handing it offthing the axis engine.
* Here is where soap message is where soap message
* @Param Req Posted Request
* @Param res respose
* @Throws servletexception Trouble
* @Throws ioException Different Trouble
* /
Public void dopost (httpservletRequest Req, httpservletResponse res)
Throws ServleTexception, IOException
{
Msgcontext = CreateMessageContext (Engine, Req, Res); // Get customer request information
Engine.Invoke (MsgContext); // Call the service request from the client
Responsemsg = msgcontext.getResponseMessage (); // get the call back result
SendResponse (GetProtocolversion (REQ), ContentType, Res, Responsemsg); // Send the result to the client
}
In this way, the WEB service calls will be generally clear. . .
For high understanding of our three URLs
http://192.168.0.1/test/services
http://192.168.0.1/test/services/sayhelloservice?wsdl
http://192.168.0.1/test/services/sayhelloservice?method =sehelloto&aname=EVERYBODY
How to get the output result, then take a look at the AxisServlet's Doget function, only the process framework and comment are given here:
**
* Process Get Requests. Because Axis Does Not Support The Get-Style
* Pseudo Execution of Soap Methods, this Handler DEALS with queries
* Of Various Kinds, Not Real SOAP Actions.
*
* @Todo for Secure Installations, DONT Stack TRACE ON FAULTS
* @Param Request Request in
* @Param Response Request Out
* @Throws servletexception
* @Throws ioException
* /
Public void doget (httpservletRequest Req, httpservletResponse res)
Throws ServleTexception, IOException
{
// If the path is empty, such as http: // localhost / wstk / services or http: // localhost / wstk / services / *
IF ((pathinfo == null || Pathinfo.equals (")) &&! realpath.endswith (". jws ")) {
/ / List all deployed service information from the server-config.wsdd file, list all deployed services to the client.
/ / Includes a method of calling each service.
Else
// If the path is not empty, such as: http: // localhost / WSTK / Services / SayHelloService
IF (RealPath! = NULL)
{
// If you request WSDL, such as: http: // localhost / WSTK / Services / Sayhelloservice? WSDL
IF (WSDLREQUESTED)
{
// Create a WSDL file for SayHelloService and transfer to the client
Else
/ / Here is the entry using the URL calling the web service, such as http://192.168.0.1/test/services/sayhelloservice?method =Sayhellotouaname=EVERYBODY
IF (Req.getParameterNames (). HasmoreElements ())
{
// If the client calls correctly, AXIS will call the corresponding JavaBean and return the JavaBean's return.
// Package to the SOAP message stream to return to the client.
}
}
}
How do Axis find the Javabean we requested? The answer is a Server-config.wsdd file.
Server-config.wsdd
XML Version = "1.0" encoding = "UTF-8"?>
service>
requestflow>
transport>
deployment>
WSDD is an abbreviation for Web Service Deployment Descriptor.
The outermost
Then the
Treating its true class is org.apache.axis.providers.java.rpcProvider.
Then we want to tell RPCProvider in
"*" Means all public methods, we also list the method name list, which can be divided into spaces or commas.
(to be continued)......