我有一个 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)
您的代码应该可以正常工作,除了这个异常“ ClassNotFoundException ”,这意味着它们org.json.JSONObject
不在类路径中,并且在项目的构建中,请确保通过以下方式导入maven
:
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20150729</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
或JSONObject类的JAR 文件。
归档时间: |
|
查看次数: |
20423 次 |
最近记录: |