我有一个 servlet,它从请求中获取一个参数并发送一个JsonObject作为响应。
我的问题是,每次我尝试使用该类PrintWriter(我什至尝试打印简单的字符串作为测试)时,servlet 都会启动但永远不会返回响应。我的第二个问题是我得到以下异常:
java.lang.ClassNotFoundException: org.json.JSONObject
Run Code Online (Sandbox Code Playgroud)
什么包包含 JsonObject?
servlet 代码:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String user = request.getParameter("param1"); // I need this later ...
System.out.println("do get called ");
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
org.json.JSONObject json = new org.json.JSONObject();
try {
json.put("Mobile",123);
json.put("Name", "ManojSarnaik");
out.print(json.toString());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
out.flush();
} finally{
out.flush();
}
}
Run Code Online (Sandbox Code Playgroud)