如何使用Web服务并将其从Java类返回到JSP

时间:2018-02-28 20:42:51

标签: java web-services jsp

我正在尝试开发一种小型软件,将来自表单的输入转换为使用Web Service转换器来处理温度的类。我只是可以得到如何调用函数以及将这些参数传递给WS函数的方法,以及我的表单是否正确完成。有人可以验证到目前为止我做了什么吗?

这是我的JSP表单:

<form name="converter" class="form-group" method="get" action="WSTutorial">
              <label>Input</label><br>
              <input type="text" name="temperature"/><br><br>
              <label>From:</label><br>
              <select name="fromUnit">
                  <option value="celsius">Celsius</option>
                  <option value="fahrenheit">Fahrenheit</option>
              </select><br>
              <label>To:</label><br>
              <select name="toUnit">
                  <option value="celsius">Celsius</option>
                  <option value="fahrenheit">Fahrenheit</option>
              </select><br><br>
              <input type="submit" value="Convert" class="btn btn-info"/>
          </form>

这是java类:

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class WSTutorial {


    private int temp;
    private String fromUnit;
    private String toUnit;


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        PrintWriter out = response.getWriter();
    response.setContentType("text/html");

        temp = Integer.parseInt(request.getParameter("temperature"));
    fromUnit = request.getParameter("fromUnit");
    toUnit = request.getParameter("toUnit");


    }

    private static double convertTemp(java.lang.String temperature, java.lang.String fromUnit, java.lang.String toUnit) {
        net.webservicex.ConvertTemperature service = new net.webservicex.ConvertTemperature();
        net.webservicex.ConvertTemperatureHttpGet port = service.getConvertTemperatureHttpGet();
        return port.convertTemp(temperature, fromUnit, toUnit);
    }



}

调用此功能的正确方法是什么?我的参数是否与所需的函数参数匹配?

谢谢

1 个答案:

答案 0 :(得分:1)

我想它看起来像这样:

表单操作会调用0 1 2 3 4 5 7 10 11

servlet

WSTutorial扩展了HttpServlet:

<form name="converter" class="form-group" method="get" action="/WSTutorial">
              <label>Input</label><br>
              <input type="text" name="temperature"/><br><br>
              <label>From:</label><br>
              <select name="fromUnit">
                  <option value="celsius">Celsius</option>
                  <option value="fahrenheit">Fahrenheit</option>
              </select><br>
              <label>To:</label><br>
              <select name="toUnit">
                  <option value="celsius">Celsius</option>
                  <option value="fahrenheit">Fahrenheit</option>
              </select><br><br>
              <input type="submit" value="Convert" class="btn btn-info"/>
          </form>

servlet在import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class WSTutorial extends HttpServlet { private int temp; private String fromUnit; private String toUnit; public void init() throws ServletException { // Do required initialization } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); response.setContentType("text/html"); temp = Integer.parseInt(request.getParameter("temperature")); fromUnit = request.getParameter("fromUnit"); toUnit = request.getParameter("toUnit"); out.println("<h1> Temp is " + convertTemp(temp ,fromUnit,toUnit) + "</h1>"); out.close(); } private static double convertTemp(java.lang.String temperature, java.lang.String fromUnit, java.lang.String toUnit) { net.webservicex.ConvertTemperature service = new net.webservicex.ConvertTemperature(); net.webservicex.ConvertTemperatureHttpGet port = service.getConvertTemperatureHttpGet(); return port.convertTemp(temperature, fromUnit, toUnit); } }

中定义
web.xml