Unable to compile JSONObject in JSP

614 views Asked by At

please assist me anyway you can with the following code(using Intellij IDEA 14.0 demo , but I fire up Tomcat manually,using TOMCAT8.exe):

<%@ page language="java" %>
<%@ page import="org.apache.commons.lang3.*" %>
<%@ page import="org.apache.commons.beanutils.*" %>
<%@ page import="org.apache.commons.collections4.*" %>
<%@ page import="org.apache.commons.logging.*" %>
<%@ page import="net.sf.json.JSONObject" %>
<%@ page import="net.sf.json.JSONArray" %>


<%
JSONObject my_data = new JSONObject();
JSONObject row = new JSONObject();
JSONObject dataUsers = new JSONObject();

JSONArray rows     = new JSONArray();
JSONArray cells    = new JSONArray();

                    cells.add("x1");
                    cells.add("x2");
                    cells.add("x3");
                    cells.add("x4");

    row.put("id","id_val1");
    row.put("cells",cells);

    dataUsers.put("nume","total");
    dataUsers.put("salariu","1000");

    rows.add(row);

    my_data.put("rows",rows);
    my_data.put("dataUser",dataUsers);

    //out.print(my_data);
    out.print(my_data.toString(2));
%>

Allthough I included the dependency libs as specified http://json-lib.sourceforge.net/ in CATALINA_HOME/lib and in PROJECT_ROOT/lib I still get this:

The error log from Tomcat

09-Dec-2014 11:17:32.493 SEVERE [http-apr-9999-exec-3]     
org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet  
[jsp] in context with path [/SAN] threw exception [An exception occurred processing 
JSP page /db.jsp at line 53

53:     JSONObject my_data = new JSONObject();
54:     JSONObject row = new JSONObject();
55:     JSONObject dataUsers = new JSONObject();
56: 


Stacktrace:] with root cause
java.lang.ClassNotFoundException:     
org.apache.commons.lang.exception.NestableRuntimeException
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:455)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:367)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at org.apache.jsp.db_jsp._jspService(db_jsp.java:132)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:537)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1085)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:277)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2407)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2396)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)

Thank you in advance!

1

There are 1 answers

1
cнŝdk On BEST ANSWER

First of all, avoid using scriptlet in jsp page put this code in a servlet istead.

Then I think the javax.json API and specifically javax.json.JsonObject and javax.json.JsonArrayis a simplier solution to manage Json objects, and your code will be:

   JsonArrayBuilder cellsBuilder=Json.createArrayBuilder();
            cellsBuilder.add("x1");
        cellsBuilder.add("X2");
        cellsBuilder.add("X3");
        cellsBuilder.add("X3");

    JsonArray cells=cellsBuilder.build();

    JsonObject rows =Json.createObjectBuilder()
        .add("id", "id_val1")
        .add("cells", cells).build();

    JsonObject dataUsers =Json.createObjectBuilder()
        .add("nume","total")
        .add("salariu","1000").build();

    JsonObject my_data =Json.createObjectBuilder()
        .add("rows", "rows")
        .add("dataUser", dataUsers).build();