Reputation: 45
I am new in json and I am getting a request in json object format.
{"login_detail":[{"password":"world","username":"hello"}]}
I want to parse it by servlet and my code is as follow-
public class LoginServlet extends HttpServlet{
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
//String json=request.getParameter("x");
try {
String jsonreq=request.getParameter("x");
System.out.println(jsonreq);
JSONArray outerArray = (JSONArray) JSONSerializer.toJSON(jsonreq);
JSONObject json1 = (JSONObject) outerArray.get(0);
JSONArray jarray1 = json1.getJSONArray("login_detail");
for (int i = 0; i < jarray1.size(); i++)
{
JSONObject hotel = jarray1.getJSONObject(i);
String user = hotel.getString("pasword");
System.out.println(user);
String pass=hotel.getString("username");
System.out.println(pass);
}
out.print(jsonreq);
}catch(Exception e){
System.out.println("inside exception");
e.printStackTrace();
}
finally {
out.close();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
public String getServletInfo() {
return "Short description";
}}
But i am getting a exception
Sep 09, 2013 4:57:52 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet login threw exception
java.lang.ClassNotFoundException: org.apache.commons.lang.exception.NestableRuntimeException
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1387)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1233)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:1850)
at org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:890)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1354)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1233)
at com.serverside.LoginServlet.processRequest(LoginServlet.java:40)
at com.serverside.LoginServlet.doPost(LoginServlet.java:72)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
at java.lang.Thread.run(Thread.java:722)
Now i am using json-lib 2.2.2 jar file.There is no any issue in receiving request from android client . I cant understand if JSONObject parametrize constructor work perfectly in ANDROID then why it is not working for servlet.?? What is problem i can't understand. If anybody will help me i will be really thankful.
Upvotes: 0
Views: 846
Reputation: 45
finally i have solve my problem see the code
try {
String data=request.getParameter("x");
System.out.println("received data : "+data);
JSONObject myjson=new JSONObject(data);
JSONArray json_array=myjson.getJSONArray("login_detail");
// return simple array with [] bracket
System.out.println("json_array "+json_array);
int size=json_array.length();
ArrayList<JSONObject> arrays=new ArrayList<JSONObject>();
for(int i=0;i<size;i++){
JSONObject another_json_object=json_array.getJSONObject(i);
arrays.add(another_json_object);
}
JSONObject[] jsons=new JSONObject[arrays.size()];
arrays.toArray(jsons);
// return simple array with [] bracket
System.out.println("arrays :"+arrays);
// return simple array and filter [] when we call get(index)
System.out.println("0 index : "+arrays.get(0));
//System.out.println("1 index : "+arrays.get(1));
//here we are accessing value using key of the array at 0 index
// we can do same for next index --> get(index).get("key")
System.out.println(arrays.get(0).get("username")+"
"+arrays.get(0).getString("password"));
}catch(Exception e){
System.out.println("inside exception");
e.printStackTrace();
}
Upvotes: 1
Reputation: 535
confirm that you liability jars had been copied to WEB-INF/lib folder... I guess so.
Upvotes: 0
Reputation: 719279
The most likely explanation is that the JAR that defines the Apache NestableRuntimeException
is missing from your webapp's WAR file.
According to http://www.findjar.com, there are lots of JAR file that could supply that dependency, but the most likely one is one of the commons-lang-x.x.jar
files; e.g. commons-lang-2.3.jar
If you are building your WAR file using Maven, just add the relevant dependency to the POM file. Otherwise, download the JAR by hand and add it to the "/WEB-INF/lib" directory of your WAR file.
Upvotes: 0
Reputation: 11487
You can download the Apache Commons Lang and place the jar file under /WEB-INF/lib
.
It should solve the problem.
Upvotes: 0
Reputation: 15402
You have to all dependency libraries, If you miss these, you will get runtime exception. You are missing org.apache.commons
btw, change hotel.getString("pasword");
to hotel.getString("password");
Upvotes: 1