Demo Calculator Add Subtract
=============PHẦN WEB SERVICE =============
@Path("/add") @GET @Produces("text/plain") public String add( @DefaultValue("5") @QueryParam("num1") double num1, @DefaultValue("3") @QueryParam("num2") double num2) { double result = num1 + num2; return "Ket qua phep cong: " + result; } @Path("/subtract") @GET @Consumes("text/plain") public String subtract( @QueryParam("num1") double num1, @QueryParam("num2") double num2) { double result = num1 + num2; return "Ket qua phep tru: " + result; }Clean and Build > Deploy > Run project Restful-001
http://localhost:8080/Restful-001/webresources/generic/add?num1=6&num2=8
http://localhost:8080/Restful-001/webresources/generic/subtract?num1=6&num2=8===============PHẦN WEB CLIENT==============
Tạo trang Calculator.jsp
Calculator.jsp
Java Web Service 2016
<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <h1>Hello World!</h1> <form action="CalculatorServlet"> Num1: <input type="text" name="num1" value="" /><br/> Num2: <input type="text" name="num2" value="" /><br/> <input type="submit" value="Add" name="action" /> <input type="submit" value="Sub" name="action"/> </form> </body> </html>Tạo Servlet CalculatorServlet
CalculatorServlet.java
Java Web Service 2016
package servlet; import com.sun.jersey.api.client.Client; import com.sun.jersey.api.client.WebResource; import com.sun.jersey.core.util.MultivaluedMapImpl; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.ws.rs.core.MultivaluedMap; /** * * @author Lonely */ public class CalculatorServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); String action = request.getParameter("action"); String num1 = request.getParameter("num1"); String num2 = request.getParameter("num2"); String uri = "http://localhost:8080/Restful-001/webresources/generic"; Client client = Client.create(); //Add jersey-client-1.19.jar client.setFollowRedirects(Boolean.TRUE); WebResource resource = client.resource(uri); MultivaluedMap<String, String> params = new MultivaluedMapImpl(); //Add jersey-core-1.19.jar params.add("num1", num1); params.add("num2", num2); if ("Add".equalsIgnoreCase(action)) { //res hiển thị nột dung bên trong method add của server String res = resource.path("add").queryParams(params).get(String.class); out.print("Add: "+num1+" + "+num2+" "+res +" <a href="+"Calculator.jsp"+"><input type="+"button"+" value="+"Calculator"+"></a>"); } else if ("Sub".equalsIgnoreCase(action)) { //res hiển thị nột dung bên trong method subtract của server String res = resource.path("subtract").queryParams(params).get(String.class); out.print("Add: "+num1+" - "+num2+" "+res +" <a href="+"Calculator.jsp"+"><input type="+"button"+" value="+"Calculator"+"></a>"); } } }
Add thư viện:
Clean and build > Deploy > Run test Restful-001-Client
0 nhận xét:
Post a Comment