Tomcat Publishing Web Service tutorial

Source: Internet
Author: User

http://blog.csdn.net/fengqiuzhihua/article/details/8783878

1. Download Jax-WS Dependency Package

Because Tomcat does not have the dependent environment required by JAX-WS, the first step is to download jax-ws RIS, the JAX-ws reference Implemantation, Address: http://jax-ws.java.net.

2. Installing JAX-ws RIS to the Tomcat server

First download Ant and Tomcat, set the environment variables Ant_home and Catalina_home, and then introduce the respective bin directory under path to open a command prompt and run the ant install in the directory of the JAX-ws RIS package.

This command will directly import the required package into the ${tomcat}\shared\lib directory, in fact, the Jaxws RI Lib package is copied to the Tomcat installation directory shared\lib inside.

3. Set up Tomcat in eclipse

Since eclipse is a tomcat configuration file of its own definition, you need to add something, add the shared\lib, and open the Ctalina.properties file.

After opening for (excerpt):

[HTML]View PlainCopy
  1. # Licensed to the Apache software Foundation (ASF) under one or more
  2. # Contributor license agreements. See the NOTICE file distributed with
  3. # This work for additional information regarding copyright ownership.
  4. # The ASF licenses this file to you under the Apache License, Version 2.0
  5. # (the "License"); Except in compliance with
  6. # The License. Obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # unless required by applicable or agreed to writing, software
  11. # Distributed under the License is distributed on a "as is" BASIS,
  12. # without warranties or CONDITIONS of any KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # Limitations under the License.
  15. #
  16. # List of comma-separated packages that start with or equal this string
  17. # would cause a security exception to being thrown when
  18. # passed to Checkpackageaccess unless the
  19. # corresponding Runtimepermission ("Accessclassinpackage.") +package) has
  20. # been granted.
  21. package.access=Sun.,org.apache.catalina.,org.apache.coyote.,org.apache.tomcat.,org.apache.jasper.
  22. #
  23. # List of comma-separated packages that start with or equal this string
  24. # would cause a security exception to being thrown when
  25. # passed to Checkpackagedefinition unless the
  26. # corresponding Runtimepermission ("Defineclassinpackage.") +package) has
  27. # been granted.
  28. #
  29. # By default, no packages is restricted for definition, and none of
  30. # The class loaders supplied with the JDK call checkpackagedefinition.
  31. #
  32. package.definition=Sun.,java.,org.apache.catalina.,org.apache.coyote.,org.apache.tomcat.,org.apache.jasper  .
  33. #
  34. #
  35. # List of comma-separated paths defining the contents of the "common"
  36. # ClassLoader. Prefixes should was used to define, what's the repository type.
  37. # path is relative to the Catalina_home or catalina_base path or absolute.
  38. # If left as blank,the JVM system loader would be used as Catalina ' s "common"
  39. # loader.
  40. # Examples:
  41. # "Foo": Add this folder as a class repository
  42. # "Foo/*.jar": Add all the JARs of the specified folder as Class
  43. # repositories
  44. # "Foo/bar.jar": Add Bar.jar as a class repository
  45. common.loader=${catalina.home}/shared/lib/*.jar,${catalina.home}/shared/lib,${catalina.base}/lib,${ Catalina.base}/lib/*.jar,${catalina.home}/lib,${catalina.home}/lib/*.jar

Locate the Common.loader configuration item to add ${catalina.home}/shared/lib/*.jar,${catalina.home}/shared/lib both paths

4. Set up the project

Create a new Web project, Webservice_web, with the following directory structure

Helloservice.java is an interface that provides a Web service with the following code:

[Java]View PlainCopy
    1. Package com.zxuqian.webservice;
    2. Import Javax.jws.WebMethod;
    3. Import Javax.jws.WebService;
    4. @WebService
    5. Public interface HelloService {
    6. @WebMethod
    7. String Greetings (string name);
    8. }

Helloserviceimpl.java is the implementation class with the following code:

[Java]View PlainCopy
  1. Package Com.zxuqian.webservice.impl;
  2. Import Javax.jws.WebService;
  3. Import Com.zxuqian.webservice.HelloService;
  4. @WebService (Endpointinterface = "Com.zxuqian.webservice.HelloService")
  5. Public class Helloserviceimpl implements HelloService {
  6. @Override
  7. Public string Greetings (string name) {
  8. return "Hello:" + name;
  9. }
  10. }

5. Add Sun-jaxws.xml

Sun-jaxws.xml is a Web service application that publishes a profile with the following content:

[HTML]View PlainCopy
  1. <? XML version= "1.0" encoding="UTF-8"?>
  2. <endpoints xmlns="Http://java.sun.com/xml/ns/jax-ws/ri/runtime" version="2.0 " >
  3. <endpoint name="HelloWorld" implementation=" Com.zxuqian.webservice.impl.HelloServiceImpl "
  4. url-pattern="/hello" />
  5. </Endpoints>

For specific instructions on each node, please refer to the Docs document in the Jaxws RI package that you downloaded, and here is a brief explanation that endpoint needs to specify
The interface and implementation class for the Web Service service, and its URL-relative path

6. Configure Web. xml

The contents are as follows:

[HTML]View PlainCopy
  1. <? XML version= "1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance " xmlns=" Http://java.sun.com/xml/ns/javaee " xmlns:web=" http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd " xsi: Schemalocation= "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ Ns/javaee/web-app_2_5.xsd " id=" webapp_id " < span class= "attribute" >version= "2.5" >&NBSP;&NBSP;
  3. <display-name>webservice_web</display-name>
  4. <listener>
  5. <listener-class>com.sun.xml.ws.transport.http.servlet.wsservletcontextlistener</ listener-class>
  6. </Listener>
  7. <servlet>
  8. <servlet-name>hello</servlet-name>
  9. <servlet-class>com.sun.xml.ws.transport.http.servlet.wsservlet</servlet-class >
  10. <load-on-startup>1</load-on-startup>
  11. </servlet>
  12. <servlet-mapping>
  13. <servlet-name>hello</servlet-name>
  14. <url-pattern>/hello</url-pattern>
  15. </servlet-mapping>
  16. </Web-app>

7. Testing

Start Tomcat, enter the Web service address in the browser Http://localhost:8088/webservice_web/hello my Tomcat's port number is 8088, and you make the appropriate changes based on your own port number.

8. Reference documents

http://www.mkyong.com/webservices/jax-ws/deploy-jax-ws-web-services-on-tomcat/

Tomcat Publishing Web Service tutorial

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.