我有一个servlet,它从请求中获取一个参数并发送JsonObject
作为响应。
我的问题是每次我尝试使用PrintWriter
类(我甚至尝试打印简单字符串作为测试)时,servlet启动但从不返回响应。我的第二个问题是我得到以下异常:
java.lang.ClassNotFoundException: org.json.JSONObject
什么包包含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();
}
}
答案 0 :(得分:4)
除了此异常“ClassNotFoundException”之外,您的代码应该正常工作,这意味着org.json.JSONObject
不在类路径和项目构建中,请确保按{maven
导入1}}:
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20150729</version>
</dependency>
课程JAR file的