WEBSERVICE written English and Chinese bidirectional translation dictionary

WebService is Web Service is also called XML Web Service WebService is a kind of Receive requests from other systems on the Internet or Intranet, lightweight independent communication technology. It is a software service provided on the Web through SOAP. It is explained using WSDL files and registered through UDDI.

WebService is a package A good external program interface makes the outside world a black box state for its internal data, which makes the data have good security, and it is also cross-language. For example, the translation dictionary (WebService) here is written in C#, and I use Java to call this interface on the client side, so that multiple languages ​​can cooperate to realize system functions, and the advantages and disadvantages of the languages ​​complement each other.


1. First check its WSDL document


< /span>


2. Create a new Web project, use cmd command line window , Cd to the src directory of the project, use the following command to compile and generate the code of the WebService server for the client to call



3. Refresh the project and automatically generate the server code



4. Next, write the client to call and parse the external interface of the WebService.

index.jsp (input English words/Chinese, display the translation effect interface)< /p>

<%@page import="java.ut il.List"%><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>English translation dictionary

English translation dictionary


Please enter the word to be translated:
Translation results:
${infor}
< /html>


Translator.java (translation function)

package com.servlet;import java.io.IOException;import java.util.List;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 cn.com.webxml.ArrayOfString;import cn.com.webxml.TranslatorWebService;import cn.com.webxml. TranslatorWebServiceSoap;@WebServlet("/Translator")public class Translator extends HttpServlet {private static final long serialVersionUID = 1L; public Translator() {super(); // TODO Auto-generated constructor stub} protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stub doPost(request, response);} protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stub //Set coderequest.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); response.setHeader("Content-Type","text/html; charset=utf-8"); String word = request. getParameter("word"); TranslatorWebService factory = new TranslatorWebService(); TranslatorWebServiceSoap soap = factory.getTranslatorWebServiceSoap(); ArrayOfString wordse = soap.getEnCnTwoWayTranslator(word); List list = wordse.getString(); request.getSession( ).setAttribute("infor", list); request.getRequestDispatcher("index.jsp").forward(request, response); }}

5. Screenshot



< /span>

Summary:

WebService greatly reduces the coupling between software modules and satisfies the principle of dependency inversion. The two modules do not depend on each other, but Relying on abstraction and relying on interfaces greatly improves the maintainability and reusability of the software.

Leave a Comment

Your email address will not be published.