JAX-WS learning 4: deploying to Web containers

Source: Internet
Author: User
JAX-WS learning 4: deploying to Web containers
    Blog type: 

  • JAX-WS
  • Java-related
 

In the previous introduction, web service is released by calling:

Java code
  1. Endpoint. publish ()

Method To start a java Embedded web container.

 

Here we will introduce how to deploy the generated web service to a web container.

 

Take the example of a calculator as an example. First, we use the wsgen tool to generate all the components required for deployment:

 

Java code
  1. Wsgen-wsdl-keep-d ../output-cp. test. CalculatorImpl

 

When running this command, I got an error message:

Java code
  1. The @ javax. jws. WebService. name element cannot be used in with @ javax. jws. WebService. endpointInterface element

This means that the name attribute and endpointInterface attribute cannot appear at the same time, so you have to delete the name attribute in CalculatorImpl first and check why this error occurs.

 

After running the above command line, we will get some new files:

  • CalculatorImplService. wsdl
  • CalculatorImplService_schema1.xsd
  • Add. java
  • AddResponse. java
  • Multi. java
  • MultiResponse. java

Copy them to the project first, where:

  1. All java files and go to the source code
  2. Create a new wsdl directory and put the wsdl and xsd files in this directory.

The rest is how to provide a web. xml so that url access can be processed by our web service implementation class.

 

Download:

Previously, it was implemented based on the built-in jdk api. Below we need to use some lib packages provided by Jax-ws. Therefore, we need to go to the next implementation of Jax-ws. For example, what I currently use 2.2.5 is:

Http://jax-ws.java.net/2.2.5/

 

Implementation Method:

Sun's Jax-ws implementation provides two classes for configuring web. xml of Web containers to implement class ing from URL to Web Service:

  • Listener class: com. sun. xml. ws. transport. http. servlet. WSServletContextListener
  • Servlet: com. sun. xml. ws. transport. http. servlet. WSServlet

 

Here wsservletcontextlistener will go to find another file sun-jaxws.xml in the same level directory as Web. XML, the flow chart of the whole process is about:

 

The schema of the sun-jaxws.xml file can be found (sun-jaxws.xsd) under the docs directory of the downloaded Jax-ws Ri ).

 

Here is a direct implementation of calculator web. xml and sun-jaxws.xml:

 

Sun-jaxws.xml code
  1. <? Xml version = "1.0" encoding = "UTF-8"?>
  2. <Endpoints xmlns = "http://java.sun.com/xml/ns/jax-ws/ri/runtime"
  3. Version = "2.0">
  4. <Endpoint name = "calculator" implementation = "test. CalculatorImpl"
  5. Url-pattern = "/calculator"/>
  6. </Endpoints>

 

 

 

Web. xml Code
  1. <? Xml version = "1.0" encoding = "UTF-8"?>
  2. <! DOCTYPE web-app PUBLIC "-// Sun Microsystems,
  3. Inc. // DTD Web Application 2.3 // EN"
  4. Http://java.sun.com/j2ee/dtds/web-app_2_3.dtd>
  5. <Web-app>
  6. <Listener>
  7. <Listener-class>
  8. Com. sun. xml. ws. transport. http. servlet. WSServletContextListener
  9. </Listener-class>
  10. </Listener>
  11. <Servlet>
  12. <Servlet-name> calculator </servlet-name>
  13. <Servlet-class>
  14. Com. sun. xml. ws. transport. http. servlet. WSServlet
  15. </Servlet-class>
  16. <Load-on-startup> 1 </load-on-startup>
  17. </Servlet>
  18. <Servlet-mapping>
  19. <Servlet-name> calculator </servlet-name>
  20. <Url-pattern>/calculator </url-pattern>
  21. </Servlet-mapping>
  22. <Session-config>
  23. <Session-Time Out> 120 </session-timeout>
  24. </Session-config>
  25. </Web-app>

 

According to the above flowchart, we can clearly understand:

 

During initialization, wsservletcontextlistener reads the WEB-INF and web. xml under the sun-jaxws.xml directory and binds the Web service implementation class to a URL, Which is indirectly carried out through a wsservlet class. Each time a request is sent, wsservletcontextlistener intercepts the request, finds the implementation of the corresponding web service based on the request URL, and calls other methods.

 

Dependency package

Obviously, the WSServletContextListener and WSServlet classes are not built by java by default. These classes are in the jar under the lib directory of the downloaded jax-ws RI. Therefore, to successfully run these web services, we also need to add the necessary dependency packages, including:

  • Gmbal-api-only.jar
  • Ha-api.jar
  • Jaxb-impl.jar
  • Jaxws-api.jar
  • Jaxws-rt.jar
  • Management-api.jar
  • Policy. jar
  • Stax-ex.jar
  • Streambuffer. jar
Package

The package structure is as follows:

 

 

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

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.