我正在尝试在不使用任何框架的情况下实现 REST 类型的架构。所以我基本上是从我的客户端调用一个 JSP,它正在doPost()
对提供服务的远程服务器进行操作。现在我能够以 JSON 格式将数据从客户端传递到服务器,但我不知道如何读取响应。有人可以帮我解决这个问题。
客户端:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
....
....
HttpPost httpPost = new HttpPost("http://localhost:8080/test/Login");
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
//Send post it as a "json_message" paramter.
postParameters.add(new BasicNameValuePair("json_message", jsonStringUserLogin));
httpPost.setEntity(new UrlEncodedFormEntity(postParameters));
HttpResponse fidresponse = client.execute(httpPost);
....
....
}
服务器端:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String jsonStringUserLogin = (String)request.getParameter("json_message");
....
....
request.setAttribute("LoginResponse", "hello");
// Here I need to send some string back to the servlet which called. I am assuming
// that multiple clients will be calling this service and do not want to use
// RequestDispatcher as I need to specify the path of the servlet.
// I am looking for more like return method which I can access through
// "HttpResponse" object in the client.
}
我刚开始使用 servlet,想自己实现一个 REST 服务。如果您有任何其他建议,请分享...谢谢,