java web service 的部署过程见上一篇博客
新建index.html
,简单的表单,用POST方法提交参数,并返回xml格式数据。
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form method="post"
action="http://localhost:8080/WebserviceDemo/service/multiply">
number one:<br> <input type="text" name="one" id="one">
<br> number two:<br> <input type="text" name="two" id="two">
<br>
<br> <input type="submit" value="Submit">
</form>
</body>
</html>
数据处理Multiply.java
package king.service.com;
import java.io.IOException;
import java.net.URI;
import java.util.UUID;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
@Path("/multiply")
public class Multiply {
@POST
@Produces("application/xml")
public Response multiply(@FormParam("one") String one, @FormParam("two") String two) throws IOException{
if (one.trim().length() > 0 && two.trim().length() > 0) {
Double multi = Double.parseDouble(one) * Double.parseDouble(two);
return Response.created(URI.create( String.valueOf(UUID.randomUUID())))
.entity("<MultiService>"+"<num-one>"+ one +"</num-one>"+"<num-two>"+ two +"</num-two>"+ "<Multi>"+ multi +"</Multi>"+"</MultiService>").build();
}
return Response.status(Response.Status.PRECONDITION_FAILED).build();
}
@Path("/{c}/{d}")
@GET
@Produces("application/xml")
public String multi(@PathParam("c") Double c, @PathParam("d") Double d){
Double muti = c * d;
return "<multiService>"+"<num-one>"+ c +"</num-one>"+"<num-two>"+ d +"</num-two>"+ "<multi>"+ muti +"</multi>"+"</multiService>";
}
}
运行测试