Publish a WebService service to the server in Java

Source: Internet
Author: User


Java after the encoding completes the WebService server, you can run a Main method to publish the WebService service, but after the actual deployment of the service to the servers, must not also run the main method, so we need to launch the server when the service is published. and runs throughout the lifecycle of the server.


Main Method Publishing Service (available for test classes)

public static void Main (string[] args) {endpoint.publish ("http://172.18.100.52:9090/medical", new Medicalservicecontroller ()); System.out.println ("****[Server ready ... WebService released successfully. ]");}

Deploying to a server we typically publish a service when initialized in a servlet:

Package cn.sccl.mms.web;import java.io.ioexception;import javax.servlet.genericservlet;import  javax.servlet.servlet;import javax.servlet.servletconfig;import javax.servlet.servletexception ;import javax.servlet.servletrequest;import javax.servlet.servletresponse;import  javax.xml.ws.endpoint;/** *  is only used to publish the WebService interface service that is provided to 114 when the  spring container is initialized, the service is started  *   *  @author  lijianbo * */public class MedicalServlet implements  servlet  {@Overridepublic  void destroy ()  {// TODO Auto-generated  Method stub} @Overridepublic  servletconfig getservletconfig ()  {// TODO  Auto-generated method stubreturn null;} @Overridepublic  string getservletinfo ()  {// TODO Auto-generated method  Stubreturn null;} /** *  Release 114webService service  *  ("This servlet does not process any business logic, just to publish a Web service");  */@Overridepublic  void init (servletconfig arg0)  throws servletexception { Endpoint.publish ("Http://172.18.100.52:9090/medical",  new medicalservicecontroller ()); System.out.println ("****[ server ready ... WebService released successfully. ]");} @Overridepublic  void service (SERVLETREQUEST ARG0, SERVLETRESPONSE ARG1)  throws  servletexception, ioexception {system.out.println ("This servlet does not process any business logic, just to publish a Web service");}}

The configuration is also required in Web. XML:

<!--endpoint.publish 114webService--><servlet><servlet-name>medicalservlet</servlet-name ><servlet-class>cn.sccl.mms.web.medicalservlet</servlet-class><load-on-startup>1</ Load-on-startup></servlet><servlet-mapping><servlet-name>medicalservlet</servlet-name ><url-pattern>/servlet/medicalServlet</url-pattern></servlet-mapping>

Note: ① to set the load level to 1, which guarantees that the servlet will be initialized when the service is started.

<load-on-startup>1</load-on-startup>

This gives us the opportunity to launch the WebService service when we start the server.


This article from "Jianbo" blog, reproduced please contact the author!

Publish a WebService service to the server in Java

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.