简体   繁体   中英

Second servlet not calling WebService

I'm working with Java and using NetBeans w/ Glashfish. I am writing a simple program that resembles a phonebook I have two servlets and a WebService. The first servlet creates the form, prompting the user for input and sending said input to the second servlet.

The second servlet then calls a WebService from another project, getting the corresponding phone number related to the name that was entered by the user. But how can I get the second servlet to call the WebService?

Second servlet:

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>
}

Webservice:

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;
    }
}

You should be able to generate a service client with netbeans.

Independently for your IDE you can use Axis2 for client generation.

The tools will usually generate some easy to use java classes for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM