从Servlet发送和接收JSON数据

时间:2012-08-15 10:35:00

标签: java json web-services servlets

如何将JSON数据发送到我的Servlet的doPost()方法并将其接收回来?我有一个生成并使用JSON的Web服务。

1 个答案:

答案 0 :(得分:3)

使用在servlet中传递给doPost方法的HttpServletRequest和HttpServletResponse参数

public class ExampServlet extends HttpServlet {
  public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException
  {
    // to accept the json data

    String jsonData = request.getParameter("json");  

    // to send out the json data

    response.setContentType("application/json");
    PrintWriter out = response.getWriter();
    out.println(jsonData) ; 
    out.close() /
   }