2012-10-25 35 views
1

我正在使用Java並使用NetBeans w/Glashfish。我正在編寫一個類似於電話本的簡單程序,我有兩個servlet和一個WebService。第一個servlet創建表單,提示用戶輸入並將所述輸入發送到第二個servlet。第二個servlet未調用WebService

第二個servlet然後從另一個項目調用WebService,獲取與用戶輸入的名稱相關的相應電話號碼。但是我怎樣才能得到第二個servlet來調用WebService?

第二個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; 
    } 
} 

回答

0

你應該能夠產生服務客戶端與NetBeans。您可以使用Axis2進行客戶端設計。

這些工具通常會爲您生成一些易於使用的java類。