1

我正在使用 Java 并使用带有 Glashfish 的 NetBeans。我正在编写一个类似于电话簿的简单程序,我有两个 servlet 和一个 WebService。第一个 servlet 创建表单,提示用户输入并将所述输入发送到第二个 servlet。

然后第二个 servlet 从另一个项目调用 WebService,获取与用户输入的姓名相关的相应电话号码。但是我怎样才能让第二个 servlet 调用 WebService?

第二个小服务程序:

package com.temp;

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;


@WebServlet(name = "ResultServlet", urlPatterns = {"/ResultServlet"})
public class ResultServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse   response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here. You may use following sample code. */
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet ResultServlet</title>");            
            out.println("</head>");
            out.println("<body>");

            out.println("<h1>Phone Number:</h1>");

            String person = request.getParameter("userName");


            //out.println(person);  --> TEST to see if ResultServlet can get "string" from FormServlet.... IT CAN!! :)


            String phone = getPerson(person); //THIS IS WHAT CANNOT BE ACCOMPLISHED!!!

            //out.println(phone);


            out.println("</body>");
            out.println("</html>");
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}

网络服务:

package com.temp;

import com.sun.xml.ws.developer.servlet.HttpSessionScope;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;


@HttpSessionScope
@WebService(serviceName = "Lookup")
public class Lookup {

    String phoneNum="";

    private HashMap<String, String> theBook = new HashMap<String, String>();

        public Lookup() {
        theBook.put("person1", "941-111-1111");
        theBook.put("person2", "941-222-2222");
        theBook.put("person3", "941-333-3333");
        theBook.put("person4", "941-444-4444");
        theBook.put("person5", "941-555-5555");

    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "getPerson")
    public String getPerson(String personName) {
        phoneNum = "";
        if (theBook.containsKey(personName)) {
            phoneNum = theBook.get(personName);
        }

        return phoneNum;
    }
}
4

1 回答 1

0

您应该能够使用 netbeans 生成服务客户端。

独立于您的 IDE,您可以使用Axis2来生成客户端。

这些工具通常会为您生成一些易于使用的 java 类。

于 2012-10-25T20:41:18.800 回答