2014-01-27 100 views
0
<%@page import="org.json.JSONObject"%> 

<%@page import="java.lang.Thread.State"%> 
<%@page import="net.sf.json.JSONArray"%> 
<%@ page language="java" import="java.io.*,java.sql.*" contentType="text/html; charset=ISO-8859-1" 


pageEncoding="ISO-8859-1"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Insert title here</title> 
</head> 
<body> 
<% 

try{ 


Class.forName("com.mysql.jdbc.Driver"); 
String mySqlUrl = "jdbc:mysql://localhost:3306/permit"; 
Connection con = DriverManager.getConnection(mySqlUrl ,"root","moodle123"); 
Statement st = con.createStatement(); 
ResultSet rs = st.executeQuery("select * from enterprisemaster where `enterpriseId=1001");` 
JSONArray respJson = new JSONArray(); 
java.sql.ResultSetMetaData rsmd = rs.getMetaData(); 
int numColumns = rsmd.getColumnCount(); 


while (rs.next()) { 

    JSONObject obj = new JSONObject(); 
    for (int i = 1; i < numColumns + 1; i++) { 

     String columnName = rsmd.getColumnName(i); 
     obj.put(columnName, rs.getString(columnName)); 

    } 
    respJson.put(obj); 


    } 
    respJson.toString(); 
    System.out.println(respJson); 
} 
catch(Exception e) 
{ 
System.out.println(e); 
} 
%> 
</body> 
</html> 

類型異常報告Java代碼來創建JSON數組

message An exception occurred processing JSP page /JsonCreation.jsp at line 36 

    description The server encountered an internal error that prevented it from fulfilling 

    this request. 

     exception 

    org.apache.jasper.JasperException: An exception occurred processing JSP page 

    /JsonCreation.jsp at line 36 

    33:    obj.put(columnName, rs.getString(columnName)); 

    34:   
    35:   } 
    36:   respJson.put(obj); 
    37:  } 
    38:  respJson.toString(); 
    39:  System.out.println(respJson); 

Stacktrace: 
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568) 
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:455) 
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390) 
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728) 
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51) 


root cause 

javax.servlet.ServletException: java.lang.NoClassDefFoundError: net/sf/ezmorph/Morpher 
    org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:912) 
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:841) 
    org.apache.jsp.JsonCreation_jsp._jspService(JsonCreation_jsp.java:119) 
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728) 
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432) 
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390) 
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728) 


    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51) 

root cause 

java.lang.NoClassDefFoundError: net/sf/ezmorph/Morpher 
    net.sf.json.JSONArray.put(JSONArray.java:1296) 
    org.apache.jsp.JsonCreation_jsp._jspService(JsonCreation_jsp.java:101) 
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728) 
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432) 
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390) 
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728) 
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51) 


root cause 

java.lang.ClassNotFoundException: net.sf.ezmorph.Morpher 
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1702) 
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1547) 
    net.sf.json.JSONArray.put(JSONArray.java:1296) 
    org.apache.jsp.JsonCreation_jsp._jspService(JsonCreation_jsp.java:101) 
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728) 
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432) 
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390) 
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728) 
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51) 


note The full stack trace of the root cause is available in the Apache Tomcat/7.0.47      logs. 
+0

javax.servlet.ServletException:java.lang.NoClassDefFoundError:net/sf/ezmorph/Morphe r 這是什麼課程?它是一種依賴性嗎?看起來像Java找不到它。 –

+0

實際上錯誤顯示在第36行jasonarrayobject.put的定義未被檢測到 –

+1

Nah,我敢肯定你的問題是它無法找到類net/sf/ezmorph/Morpher ... –

回答

0

<%@頁面進口= 「org.json.JSONArray 」%> <%@頁面進口=「 org.json.JSONObject」 %>我改變了JAR文件它工作正常現在

<%@頁面進口=「net.sf.json.JSONArray」%>與上述罐和淨衝突。* .... 罐子是腐敗

0

作爲每棧跟蹤,所述net/sf/ezmorph/Morpher類中找不到。

請確保您添加ezmorph.jar到類路徑即WEB-INF/lib文件夾

+0

我做了同樣的錯誤 –

+0

我複製了相同的代碼並運行在java類的主要方法中,然後它的工作和輸出得到了收到 –

+0

它的工作原理是因爲jar在構建路徑中可用(希望您使用的是eclipse) - 如果添加到lib。沒有其他的解決方案,我有... – gowtham