使用JSP创建JSON数组时出错

时间:2014-05-26 07:31:17

标签: json jsp

我正在尝试使用以下代码创建JSON数组;我已经下载了gson-2.2.4.jar并在我的JSP文件所在的同一位置提取了com目录

<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.* " %>
<%@ page import="java.io.*" %> 
<%@page import="com.google.gson.JSONObject"%>
<%

try {
String driver = "org.postgresql.Driver";
String url = "jdbc:postgresql://localhost:5432/CATCHINFO";
String username = "postgres";
String password = "123";
String myDataField = null;
String category = null;
String myQuery = "SELECT * FROM cruce order by cruce_no";
Connection myConnection = null;
PreparedStatement myPreparedStatement = null;
ResultSet myResultSet = null;
Class.forName(driver).newInstance();
myConnection = DriverManager.getConnection(url,username,password);
myPreparedStatement = myConnection.prepareStatement(myQuery);
myResultSet = myPreparedStatement.executeQuery();
JSONObject json = new JSONObject();

while(myResultSet.next()){

category = myResultSet.getString("cruce_no");
json.put("city", category);
out.print(category);
}
}
catch(Exception e){
out.print(e);
}

%>"

但我收到错误:

org.apache.jasper.JasperException: Unable to compile class for JSP: 

An error occurred at line: 16 in the generated java file
Only a type can be imported. com.google.gson.JSONObject resolves to a package

An error occurred at line: 21 in the jsp file: /loadCruceNo.jsp
JSONObject cannot be resolved to a type
An error occurred at line: 21 in the jsp file: /loadCruceNo.jsp
JSONObject cannot be resolved to a type

请帮帮我。

1 个答案:

答案 0 :(得分:0)

如果您在我的JSP文件所在的同一位置提取了com目录并且#34;听起来不对。

jar应该位于Web应用程序的WEB-INF/lib目录中,或者如果您决定解压缩所有类(如您所述),则com目录应位于WEB-INF/classes目录中。