/* Title: Web service entry notes (7)
** Date: 2007/01/19
** Author: laomai
** URL: http://blog.csdn.net/laomai/
*/
VII. Command Line Development Process
Through the above learning, we know how to develop a simple WebService in the ant script and IDE environment.
However, as technical personnel, especially those engaged in C, the above process hides too many things,
For those who have the research spirit, we naturally want to figure out the process behind the script.
Suppose there is no IDE environment or ant tool on our machine,
How can we manually compile our own WebService?
This is what we will talk about in this section. We only use the command line tool to develop WebService.
Declare again:
This section mainly references
Http://blog.csdn.net/lin_bei/archive/2006/11/07/1371131.aspx
The translation of this brother is not fluent, So I adapted it according to my own understanding.
Example of hellowrold :-).
Others are original. Please indicate the source when reprinting.
0. Set Environment Variables
Set the JDK and javaee paths as follows:
D:/Java/jdk1.6.0
D:/Sun/SDK/bin added to the PATH variable of the system.
1. Create a project directory
First, create a project directory named webtest. The project directory contains three subdirectories.
Src/This directory is used to store the source code,
Build/This directory is used to store output files
Deploy/This directory is used to package servers
2. Compile server-class implementation files
Create Sub-directory endpoints under the src directory of the project,
Create a hello. Java file in the endpoint subdirectory. The content is as follows:
/* Src/Endpoint/Hello. Java File
Provides WebService server implementation classes
*/
Package endpoint;
Import javax. JWS. WebService;
Import javax. JWS. webmethod;
@ WebService
Public class hello
{
@ Webmethod
Public String gethello (string name)
{
Return "hello" + name + "! ";
}
}
3. Compile the server class
① Create a sub-directory classes under the build directory
② Execute the following command in the command line
CD webtest; enter the project directory
Javac-classpath D:/Sun/SDK/lib/javaee. jar-D./build/classes src/Endpoint/Hello. Java; Compile the server class
After execution, a build/classes/Endpoint/Hello. Class file is generated.
4. Generate a WSDL File
① Create a sub-directory generated under the build directory
② Generate the WSDL file and execute
Wsgen-CP./build/classes-keep-D./build/classes-R./build/generated-WSDL endpoint. Hello
After the execution is completed, two files are generated in./build/generated.
Helloservice. WSDL
Helloservice_schema1.xsd
Create a jaxws directory under/build/class/endpoint. There are four files
Gethello. Java
Gethello. Class
Gethelloresponse. Java
Gethelloresponse. Class
These files are used together with
D:/Sun/SDK/domains/domain1/generated/EJB/j2ee-modules/endpoint_hello/Endpoint/jaxws
The files under are the same
5. Package servers into war files
① Create the directory required for packaging
Create a sub-directory/WEB-INF under the deploy directory of the project,
Create two subdirectories under the WEB-INF subdirectory
Classes/used to store server classes
WSDL/used to store the WSDL File
② Copy each output file or directory to the corresponding directory
(1) copy the entire build/classes/Endpoint directory to deploy/WEB-INF/class directory
(2) Put the two files under the build/generated directory
Copy helloservice. WSDL and helloservice_schema1.xsd
Deploy/WEB-INF/WSDL directory
③ Create a new Web. xml file in the deploy/WEB-INF/directory, the content is
<? XML version = "1.0" encoding = "UTF-8"?>
<Web-app xmlns = "http://java.sun.com/xml/ns/javaee"
Xmlns: J2EE = "http://java.sun.com/xml/ns/javaee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" version = "2.5"
Xsi: schemalocation = "http://java.sun.com/xml/ns/javaee
Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd>
<Description> webtier for the hello service </description>
<Display-Name> hellowar </display-Name>
<Servlet>
<Description> endpoint for hello Web Service </description>
<Display-Name> hellowebservice </display-Name>
<Servlet-Name> Hello </servlet-Name>
<Servlet-class> endpoint. Hello </servlet-class>
<Load-on-startup> 0 </load-on-startup>
</Servlet>
<Servlet-mapping>
<Servlet-Name> Hello </servlet-Name>
<URL-pattern>/helloservice </url-pattern>
</Servlet-mapping>
<Session-config>
<Session-Timeout> 54 </session-Timeout>
</Session-config>
</Web-app>
④ Package and execute the service
(1) CD webtest/deploy; enter the packaging directory
(2) jar CVF hello. War *; pack all contents in the current directory into the hello. War file.
6. Publish the service class to the network server
① Start the sun Server
Method:
Start> program> Sun Microsystems> Application Server PE 9> Start Default Server
② Create a passwd file in the project directory, which contains the password of the Sun server admin user, such
As_admin_password = testtesttest
③ Publish hello. War to the server and execute
(1) CD webtest; enter the project directory
(2) release the server package. Note that this command is executed in one line,
I cut lines for ease of writing. The semicolon is followed by a comment
Asadmin deploy
-- User admin; administrator username
-- Passwordfile passwd; the password file name is the passwd file we just wrote.
-- Host localhost
-- Port 4848; Management port number
-- Contextroot myhello; Context root name
-- Upload = true
-- Target server
Deploy/Hello. War
④ Enter the address in the browser
Http: // localhost: 8080/myhello/helloservice? WSDL,
If the browser displays the correct content, it indicates that the operation is successful.
7. Generate the stub class of the Client
① Create a stub subdirectory under the build/classes directory of the project
② Execution
(1) CD webtest; enter the project subdirectory
(2) wsimport-keep-D./build/classes/stub http: // localhost: 8080/myhello/helloservice? WSDL
After the execution is complete, an endpoint directory is created under build/classes/stub. the following files are available:
Gethello. Java
Gethelloresponse. Java
Hello. Java
Helloservice. Java
Gethello. Class
Gethelloresponse. Class
Hello. Class
Helloservice. Class
Objectfactory. Class
Objectfactory. Java
Package-info.java
Package-info.class
These files are the same as the import execution results in the ant script.
8. Compile the client Test Program
Create a sub-Directory Client under the src directory of the project, and create a sub-Directory Client under the Directory
Client. Java file, content:
// Src/client. Java file call the WebService test class provided by the server
Package client;
Import javax. xml. ws. webserviceref;
Import endpoint. helloservice;
Import endpoint. Hello;
Public class client
{
@ Webserviceref (wsdllocation = "http: // localhost: 8080/myhello/helloservice? WSDL ")
Static helloservice service;
Public static void main (string [] ARGs)
{
Client client = new client ();
Client. dohello ();
}
Public void dohello ()
{
Try
{
Hello Port = service. gethelloport ();
String ret = port. gethello (system. getproperty ("user. Name "));
System. Out. println ("Hello result =" + RET );
}
Catch (exception E)
{
E. printstacktrace ();
}
}
}
9. Compile the client program
Run
(1) CD webtest; enter the project directory
(2) javac-classpath. /build/classes/stub; D:/Sun/SDK/lib/javaee. jar; D:/Sun/SDK/lib/appserv-ws.jar-D. /build/classes/stub src/client. java
After successful execution, a file is created in the F:/exercise/Java/webtest/build/classes directory.
Client Directory, which contains a client. Class File
10. Run the client program
(1) CD webtest/build/classes/stub; enter the parent directory of the Client
(2) Set appcpath =.; set the environment variable appcpath. Otherwise, a bunch of inexplicable errors may occur when you run the appclient program.
(3)> appclient client. client runs the test program and the result is
Hello result = Hello administrator!
Execution successful
/* Title: Web service entry notes (8)
** Date: 2007/01/19
** Author: laomai
** URL: http://blog.csdn.net/laomai/
*/
VIII. Summary
The basic steps for WebService development are as follows:
1. Compile the server. The key points are:
① Import the WebService package and webmethod package
Import javax. JWS. WebService;
Import javax. JWS. webmethod;
② Add the @ WebService symbol before the implemented service class
③ For code clarity, add the @ webmethod symbol before the public method provided by the class. This will not affect compilation if it is not written,
2. Compile the server. The key point is
① The classpath option of the javac command must contain the javaee. Jar path, as shown in
Javac-classpath D:/Sun/SDK/lib/javaee. jar-D./build src/Endpoint/Hello. Java
② Use the wsgen command to generate the WSDL file.
③ Package servers
Note that if the server is Sun, directly compile the service class
Javaee5 installation directory/domains/domain1/autodeploy, You can automatically complete ② and ③ work.
This is what we introduced with Sun's own getting started script.
3. The stub class is automatically generated on the client machine. The main point is
① JDK and javaee5 must also be installed on the client
② Use the wsimport tool to convert the WSDL files passed by the server to the stub class at a cost
4. Compile the client call code. Key points:
① Import the webserviceref package
Import javax. xml. ws. webserviceref;
② Import the locally generated stub class, as shown in figure
Import endpoint. helloservice;
Import endpoint. Hello;
③ Specify the server's WSDL path
@ Webserviceref (wsdllocation = "http: // localhost: 8080/myhello/helloservice? WSDL ")
④ Declare a static service object
Static helloservice service;
⑤ Declare a proxy object for the remote method to be called and call the real remote method through the proxy
Hello Port = service. gethelloport ();
String ret = port. gethello (system. getproperty ("user. Name "));
5. Compile the client call program. Note that the classpath parameter must contain
① Stub class path
② Javaee. Jar path
3 path of appserv-ws.jar
6. Use appclient to execute the client program. The key point is
① Enter the parent directory of the Client Program
② Set the value of appcpath to the current directory "."
③ The first parameter of appclient is the client program name,
The following parameters are the command line parameters that are passed to the client program itself.
/* Title: Web service entry notes (9)
** Date: 2007/01/19
** Author: laomai
** URL: http://blog.csdn.net/laomai/
*/
IX. Files used in this article
1. webtest project file list
Webtest/passwd: Save the password file and create it manually
The webtest/src subdirectory is manually created and the content is
Endpoint/Hello. Java Server class implementation file
Implementation file of client/client. Java client class
Webtest/build
Generated subdirectory, which is created manually and the content is
Helloservice. WSDL is generated by the wsgen command
Helloservice_schema1.xsd is generated by the wsgen command
Classes sub-Directory, which is created manually and the content is
The endpoint/Hello. Class is generated by the javac command.
The endpoint/jaxws subdirectory, which is automatically generated by the wsgen command. The content is
Gethello. Java
Gethelloresponse. Java
Gethello. Class
Gethelloresponse. Class
Stub sub-Directory, which is created manually with the following content:
The client/client. Class is generated by the javac command.
The endpoint subdirectory is automatically generated by the wsimport command. The content is:
Gethello. Java
Gethelloresponse. Java
Hello. Java
Helloservice. Java
Objectfactory. Java
Package-info.java
Package-info.class
Gethello. Class
Gethelloresponse. Class
Hello. Class
Helloservice. Class
Objectfactory. Class
The webtest/deploy subdirectory is manually created and the content is
Hello. War the file generated after packaging the WEB-INF subdirectory, generated by the jar command
WEB-INF package input directory, manually created. The content includes:
Web. xml manual Creation
The classes/Endpoint subdirectory is a copy of build/classes/endpoint.
The WSDL subdirectory, Which is copied by build/generated.
2. Content of the generated helloservice. WSDL File
<? XML version = "1.0" encoding = "UTF-8" standalone = "yes"?>
<Definitions targetnamespace = "http: // endpoint/" name = "helloservice" xmlns = "http://schemas.xmlsoap.org/wsdl/" xmlns: TNS = "http: // endpoint/" xmlns: XSD = "The http://www.w3.org/2001/XMLSchema" xmlns: Soap = "http://schemas.xmlsoap.org/wsdl/soap/">
<Types>
<XSD: schema>
<XSD: Import namespace = "http: // endpoint/" schemalocation = "helloservice_schema1.xsd"/>
</XSD: schema>
</Types>
<Message name = "gethello">
<Part name = "Parameters" element = "TNS: gethello"/>
</Message>
<Message name = "gethelloresponse">
<Part name = "Parameters" element = "TNS: gethelloresponse"/>
</Message>
<Porttype name = "hello">
<Operation name = "gethello">
<Input message = "TNS: gethello"/>
<Output Message = "TNS: gethelloresponse"/>
</Operation>
</Porttype>
<Binding name = "helloportbinding" type = "TNS: Hello">
<Soap: Binding transport = "http://schemas.xmlsoap.org/soap/http" style = "document"/>
<Operation name = "gethello">
<Soap: Operation soapaction = ""/>
<Input>
<Soap: Body use = "literal"/>
</Input>
<Output>
<Soap: Body use = "literal"/>
</Output>
</Operation>
</Binding>
<Service name = "helloservice">
<Port name = "helloport" binding = "TNS: helloportbinding">
<Soap: address location = "replace_with_actual_url"/>
</Port>
</Service>
</Definitions>
10. Thank you
When I learned about WebService, I got a lot of advice from ye Fengcheng, A csdn Java edition,
I would like to express my gratitude for the many useful materials provided by the brokers of the pie tree. This article is just for me.
A report and summary from friends.
(-- End
/* Title: Web service entry notes
** Date: 2007/01/23
** Author: laomai
** URL: http://blog.csdn.net/laomai /*/)