一、Web services概念 Web services是客户端和服务端通过万维网的HTTP协议进行交互。
二、JAX-WS实现简单的Web services 2.1 建一个名为HelloServer的Web应用作为Webservice客户端 2.2 在HelloServer应用下新建一个类:
- package helloservice.endpoint;
- import javax.jws.WebMethod;
- import javax.jws.WebService;
- @WebService
- public class Hello {
- private String message = new String("Hello, ");
- public void Hello() {
- }
- @WebMethod
- public String sayHello(String name) {
- return message + name + ".";
- }
- }
2.3 在weblogic下发布HelloServer应用,应用名为WebRoot。 2.4 在IE里面打开http://localhost:7001/WebRoot/HelloService?wsdl 如果可以查看到wsdl的内容说明发布成功.比如:
- <?xml version="1.0" encoding="UTF-8" ?>
- -
-
- -
-
- - <definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
- xmlns:tns="http://endpoint.helloservice/"
- xmlns:xsd="http://www.w3.org/2001/XMLSchema"
- xmlns="http://schemas.xmlsoap.org/wsdl/"
- targetNamespace="http://endpoint.helloservice/"
- name="HelloService">
- - <types>
- - <xsd:schema>
- <xsd:import namespace="http://endpoint.helloservice/" schemaLocation="http://localhost:7001/WebRoot/HelloService?xsd=1" />
- </xsd:schema>
- </types>
- - <message name="sayHello">
- <part name="parameters" element="tns:sayHello" />
- </message>
- - <message name="sayHelloResponse">
- <part name="parameters" element="tns:sayHelloResponse" />
- </message>
- - <portType name="Hello">
- - <operation name="sayHello">
- <input message="tns:sayHello" />
- <output message="tns:sayHelloResponse" />
- </operation>
- </portType>
- - <binding name="HelloPortBinding" type="tns:Hello">
- <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />
- - <operation name="sayHello">
- <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="http://localhost:7001/WebRoot/HelloService" />
- </port>
- </service>
- </definitions>
2.5 运行wsimport wsimport是JDK1.6特有的,[JAVA_HOME]/bin下。 2.5.1 在E:\Program Files\PowerCmd>目录下,新建一个文件夹generate。 2.5.2 运行如下命令: wsimport -s generate http://localhost:7001/WebRoot/HelloService?wsdl 如果返回 parsing WSDL... generating code... 说明运行成功。 2.5.3 查看generate目录,可以看到生成了JAVA文件,与generate同级的目录下,还有class文件。(这里生成的JAVA文件,客户端需要用到) 生成的HelloService.java如下:
- package helloservice.endpoint;
- import java.net.MalformedURLException;
- import java.net.URL;
- import javax.xml.namespace.QName;
- import javax.xml.ws.Service;
- import javax.xml.ws.WebEndpoint;
- import javax.xml.ws.WebServiceClient;
- import javax.xml.ws.WebServiceFeature;
-
-
-
-
-
-
- @WebServiceClient(name = "HelloService", targetNamespace = "http://endpoint.helloservice/", wsdlLocation = "http://localhost:7001/WebRoot/HelloService?wsdl")
- public class HelloService
- extends Service
- {
- private final static URL HELLOSERVICE_WSDL_LOCATION;
- static {
- URL url = null;
- try {
- url = new URL("http://localhost:7001/WebRoot/HelloService?wsdl");
- } catch (MalformedURLException e) {
- e.printStackTrace();
- }
- HELLOSERVICE_WSDL_LOCATION = url;
- }
-
- public HelloService(URL wsdlLocation, QName serviceName) {
- super(wsdlLocation, serviceName);
- }
-
- public HelloService() {
- super(HELLOSERVICE_WSDL_LOCATION, new QName("http://endpoint.helloservice/", "HelloService"));
- }
-
-
-
-
-
- @WebEndpoint(name = "HelloPort")
- public Hello getHelloPort() {
- return (Hello)super.getPort(new QName("http://endpoint.helloservice/", "HelloPort"), Hello.class);
- }
-
-
-
-
-
-
-
- @WebEndpoint(name = "HelloPort")
- public Hello getHelloPort(WebServiceFeature... features) {
- return (Hello)super.getPort(new QName("http://endpoint.helloservice/", "HelloPort"), Hello.class, features);
- }
- }
2.6 建一个名为HelloClient的Web应用作为WebService客户端。 2.7 将3.5.3生成的JAVA文件复制到HelloClient的src下。 2.8 新建一个HelloServlet文件,如下:
- package webclient;
- import helloservice.endpoint.HelloService;
- import java.io.IOException;
- import java.io.PrintWriter;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.xml.ws.WebServiceRef;
-
- @WebServlet(name = "HelloServlet", urlPatterns = { "/HelloServlet" })
- public class HelloServlet extends HttpServlet {
- @WebServiceRef(wsdlLocation = "http://localhost:7001/WebRoot/HelloService?wsdl")
- private HelloService service;
-
-
-
-
- public HelloServlet() {
- super();
- }
-
-
-
- public void destroy() {
- super.destroy();
-
- }
-
-
-
-
-
-
-
-
-
-
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- processRequest(request, response);
- }
-
-
-
-
-
-
-
-
-
-
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- processRequest(request, response);
- }
-
-
-
-
-
- public void init() throws ServletException {
-
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- protected void processRequest(HttpServletRequest request,
- HttpServletResponse response) throws ServletException, IOException {
- response.setContentType("text/html;charset=UTF-8");
- PrintWriter out = response.getWriter();
- try {
- out.println("<html lang=\"en\">");
- out.println("<head>");
- out.println("<title>Servlet HelloServlet</title>");
- out.println("</head>");
- out.println("<body>");
- out.println("<h1>Servlet HelloServlet at "
- + request.getContextPath() + "</h1>");
- out.println("<p>" + sayHello("world") + "</p>");
- out.println("</body>");
- out.println("</html>");
- } finally {
- out.close();
- }
- }
-
-
- private String sayHello(java.lang.String arg0) {
- helloservice.endpoint.Hello port = service.getHelloPort();
- return port.sayHello(arg0);
- }
- }
2.9 配置HelloClient的Web.xml,增加如下代码:
- <servlet>
- <description>HelloServlet</description>
- <display-name>HelloServlet</display-name>
- <servlet-name>HelloServlet</servlet-name>
- <servlet-class>webclient.HelloServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>HelloServlet</servlet-name>
- <url-pattern>/servlet/HelloServlet</url-pattern>
- </servlet-mapping>
2.10 发布HelloClient应用。 2.11 在IE录入http://localhost:7111/servlet/HelloServlet 页面内容如下说明WebService调用成功!

原文链接:http://maymay.iteye.com/blog/1271139 【编辑推荐】
- JSP转译成Servlet详细过程
- 详细剖析JSP和BEAN
- jsp速度优化七种方法
- JSP动态网站环境搭建应用中的详细步骤
- 详细介绍JSP技术的两种架构模型
给力
(0票)
动心
(0票)
废话
(0票)
专业
(0票)
标题党
(0票)
路过
(0票)
游戏开源社区:www.admin19.com