私は Java で作業しており、NetBeans と Glashfish を使用しています。電話帳に似た単純なプログラムを作成しています。2 つのサーブレットと WebService があります。最初のサーブレットはフォームを作成し、ユーザーに入力を求め、その入力を 2 番目のサーブレットに送信します。
次に、2 番目のサーブレットは別のプロジェクトから WebService を呼び出し、ユーザーが入力した名前に関連する対応する電話番号を取得します。しかし、2 番目のサーブレットで WebService を呼び出すにはどうすればよいでしょうか?
2 番目のサーブレット:
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;
}
}