如何使用Web服务并将其从Java类返回到JSP
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用Web服务并将其从Java类返回到JSP相关的知识,希望对你有一定的参考价值。
我正在尝试开发一种小型软件,将来自表单的输入转换为使用Web服务转换器来处理温度的类。我只是可以得到如何调用函数以及将这些参数传递给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);
}
}
调用此函数的正确方法是什么?我的参数是否与所需的函数参数匹配?
谢谢
答案
我猜它看起来像这样:
表单操作称为servlet
<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>
WSTutorial扩展了HttpServlet:
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);
}
}
servlet在qazxsw poi中定义
web.xml
以上是关于如何使用Web服务并将其从Java类返回到JSP的主要内容,如果未能解决你的问题,请参考以下文章