Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/469.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 页面加载后的访问数据不是';不成功_Javascript_Jquery_Json_Jsp - Fatal编程技术网

Javascript 页面加载后的访问数据不是';不成功

Javascript 页面加载后的访问数据不是';不成功,javascript,jquery,json,jsp,Javascript,Jquery,Json,Jsp,我想在页面加载后访问数据,但它不起作用。它似乎没有在jsp中调用getConnection函数,也没有调用response Json。我不知道为什么它不起作用。我的代码有什么问题,如何修复?多谢各位 MyServlet.java import java.io.*; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.

我想在页面加载后访问数据,但它不起作用。它似乎没有在jsp中调用getConnection函数,也没有调用response Json。我不知道为什么它不起作用。我的代码有什么问题,如何修复?多谢各位

MyServlet.java

import java.io.*;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;

/**
 * Servlet implementation class MyServlet
 */
public class MyServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public MyServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        ArrayList<Dish> country=new ArrayList<Dish>();
        country=SQLConnection.getAllDish();

        Gson gson = new Gson();
        JsonElement element = gson.toJsonTree(country, new TypeToken<List<Dish>>() {}.getType());

        JsonArray jsonArray = element.getAsJsonArray();
        response.setContentType("application/json");
        response.getWriter().print(jsonArray);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

}
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.ArrayList;
import java.util.Properties;

public class SQLConnection {    
//  java.sql.Connection connection;
    private static Connection connection = null;
    static String url, driver, username, password;

    public static Connection getConnection() {
        if (connection != null)
            return connection;
        else {
            try {
                Properties prop = new Properties();
                InputStream inputStream = SQLConnection.class.getClassLoader().getResourceAsStream("/db.properties");
                prop.load(inputStream);
                driver = prop.getProperty("driver");
                url = prop.getProperty("url");
                username = prop.getProperty("user");
                password = prop.getProperty("password");
                Class.forName(driver);
                connection = DriverManager.getConnection(url, username, password);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return connection;
        }

    }

    public static ArrayList<Dish> getAllDish() {
        ArrayList<Dish> arrDish = null;
         try {
                //Creating a statement object
                Statement stmt = connection.createStatement();

                //Executing the query and getting the result set
                ResultSet rs = stmt.executeQuery("select * from dish");
                arrDish = new ArrayList<Dish>();
                //Iterating the resultset and printing the 3rd column
                while (rs.next()) {
                    Dish item = new Dish(rs.getInt(1), rs.getInt(2), rs.getString(3), rs.getDouble(4), rs.getString(5),
                            rs.getString(6), rs.getString(7), rs.getString(8), rs.getInt(9), rs.getInt(10), rs.getInt(11), rs.getInt(12));
                    arrDish.add(item);                  
                }
                //close the resultset, statement and connection.
                rs.close();
                stmt.close();
                connection.close();
                return arrDish;
            } catch (SQLException e) {
                e.printStackTrace();
            }
         return arrDish;
    }
}
import java.io.*;
导入java.util.ArrayList;
导入java.util.List;
导入javax.servlet.ServletException;
导入javax.servlet.http.HttpServlet;
导入javax.servlet.http.HttpServletRequest;
导入javax.servlet.http.HttpServletResponse;
导入com.google.gson.gson;
导入com.google.gson.JsonArray;
导入com.google.gson.JsonElement;
导入com.google.gson.reflect.TypeToken;
/**
*Servlet实现类MyServlet
*/
公共类MyServlet扩展了HttpServlet{
私有静态最终长serialVersionUID=1L;
/**
*@参见HttpServlet#HttpServlet()
*/
公共MyServlet(){
超级();
//TODO自动生成的构造函数存根
}
/**
*@参见HttpServlet#doGet(HttpServletRequest请求,HttpServletResponse响应)
*/
受保护的void doGet(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException{
//TODO自动生成的方法存根
ArrayList国家/地区=新ArrayList();
country=SQLConnection.getAllDish();
Gson Gson=新的Gson();
JsonElement元素=gson.toJsonTree(国家,新类型令牌(){}.getType());
JsonArray JsonArray=element.getAsJsonArray();
setContentType(“应用程序/json”);
response.getWriter().print(jsonArray);
}
/**
*@请参阅HttpServlet#doPost(HttpServletRequest请求,HttpServletResponse响应)
*/
受保护的void doPost(HttpServletRequest请求、HttpServletResponse响应)引发ServletException、IOException{
//TODO自动生成的方法存根
}
}
SQLConnection.java

import java.io.*;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;

/**
 * Servlet implementation class MyServlet
 */
public class MyServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public MyServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        ArrayList<Dish> country=new ArrayList<Dish>();
        country=SQLConnection.getAllDish();

        Gson gson = new Gson();
        JsonElement element = gson.toJsonTree(country, new TypeToken<List<Dish>>() {}.getType());

        JsonArray jsonArray = element.getAsJsonArray();
        response.setContentType("application/json");
        response.getWriter().print(jsonArray);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

}
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.ArrayList;
import java.util.Properties;

public class SQLConnection {    
//  java.sql.Connection connection;
    private static Connection connection = null;
    static String url, driver, username, password;

    public static Connection getConnection() {
        if (connection != null)
            return connection;
        else {
            try {
                Properties prop = new Properties();
                InputStream inputStream = SQLConnection.class.getClassLoader().getResourceAsStream("/db.properties");
                prop.load(inputStream);
                driver = prop.getProperty("driver");
                url = prop.getProperty("url");
                username = prop.getProperty("user");
                password = prop.getProperty("password");
                Class.forName(driver);
                connection = DriverManager.getConnection(url, username, password);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return connection;
        }

    }

    public static ArrayList<Dish> getAllDish() {
        ArrayList<Dish> arrDish = null;
         try {
                //Creating a statement object
                Statement stmt = connection.createStatement();

                //Executing the query and getting the result set
                ResultSet rs = stmt.executeQuery("select * from dish");
                arrDish = new ArrayList<Dish>();
                //Iterating the resultset and printing the 3rd column
                while (rs.next()) {
                    Dish item = new Dish(rs.getInt(1), rs.getInt(2), rs.getString(3), rs.getDouble(4), rs.getString(5),
                            rs.getString(6), rs.getString(7), rs.getString(8), rs.getInt(9), rs.getInt(10), rs.getInt(11), rs.getInt(12));
                    arrDish.add(item);                  
                }
                //close the resultset, statement and connection.
                rs.close();
                stmt.close();
                connection.close();
                return arrDish;
            } catch (SQLException e) {
                e.printStackTrace();
            }
         return arrDish;
    }
}
import java.io.FileNotFoundException;
导入java.io.IOException;
导入java.io.InputStream;
导入java.sql.*;
导入java.util.ArrayList;
导入java.util.Properties;
公共类SQLConnection{
//java.sql.Connection;
私有静态连接=null;
静态字符串url、驱动程序、用户名、密码;
公共静态连接getConnection(){
if(连接!=null)
回路连接;
否则{
试一试{
Properties prop=新属性();
InputStream InputStream=SQLConnection.class.getClassLoader().getResourceAsStream(“/db.properties”);
属性加载(输入流);
driver=prop.getProperty(“driver”);
url=prop.getProperty(“url”);
用户名=prop.getProperty(“用户”);
password=prop.getProperty(“密码”);
Class.forName(驱动程序);
connection=DriverManager.getConnection(url、用户名、密码);
}catch(classnotfounde异常){
e、 printStackTrace();
}捕获(SQLE异常){
e、 printStackTrace();
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
回路连接;
}
}
公共静态数组列表getAllDish(){
ArrayList arrDish=null;
试一试{
//创建语句对象
语句stmt=connection.createStatement();
//执行查询并获取结果集
ResultSet rs=stmt.executeQuery(“从培养皿中选择*);
arrDish=新的ArrayList();
//迭代结果集并打印第3列
while(rs.next()){
碟项=新碟(rs.getInt(1)、rs.getInt(2)、rs.getString(3)、rs.getDouble(4)、rs.getString(5),
rs.getString(6)、rs.getString(7)、rs.getString(8)、rs.getInt(9)、rs.getInt(10)、rs.getInt(11)、rs.getInt(12));
添加(项目);
}
//关闭结果集、语句和连接。
rs.close();
stmt.close();
connection.close();
回程盘;
}捕获(SQLE异常){
e、 printStackTrace();
}
回程盘;
}
}
hello.jsp

   <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <html>
    <head>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/style.css" />

    <title><fmt:message key="title" /></title>
    </head>
        <body bgcolor="#FEDDFF">
            <script
                src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
            <script type="text/javascript" src="js/slider.js"></script>

            <script>
                $(window).ready(function() {                
                        $.get('MyServlet',function(responseJson) {
                            if(responseJson!=null){
                                $("#dishes").find("tr:gt(0)").remove();
                                var table1 = $("#dishes");
                                $.each(responseJson, function(key,value) { 
                                       var rowNew = $("<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>");
                                       rowNew.children().eq(0).int(value['dishid']); 
                                       rowNew.children().eq(1).int(value['userid']); 
                                       rowNew.children().eq(2).text(value['dishName']); 
                                       rowNew.children().eq(3).double(value['numberOfPeople']); 
                                       rowNew.children().eq(4).text(value['dishImg']); 
                                       rowNew.children().eq(5).text(value['ingredient']);
                                       rowNew.children().eq(6).text(value['step']); 
                                       rowNew.children().eq(7).text(value['descOfDish']); 
                                       rowNew.children().eq(8).int(value['category1']); 
                                       rowNew.children().eq(9).int(value['category2']); 
                                       rowNew.children().eq(10).int(value['category3']); 
                                       rowNew.children().eq(11).int(value['rate']);
                                       rowNew.appendTo(table1);
                                });
                            }
                        });
                    });
            </script>

<div id="tabledish">
                            <table cellspacing="0" id="dishes">
                                <tr>
                                    <th scope="col">dishid</th>
                                    <th scope="col">userid</th>
                                    <th scope="col">dishName</th>
                                    <th scope="col">numberOfPeople</th>
                                    <th scope="col">dishImg</th>
                                    <th scope="col">ingredient</th>
                                    <th scope="col">step</th>
                                    <th scope="col">descOfDish</th>
                                    <th scope="col">category1</th>
                                    <th scope="col">category2</th>
                                    <th scope="col">category3</th>
                                    <th scope="col">rate</th>
                                </tr>
                            </table>
                        </div>
        </body>

$(窗口).ready(函数(){
$.get('MyServlet',函数(responseJson){
if(responseJson!=null){
$(“#碟”).find(“tr:gt(0)”.remove();
var表1=$(“#碟”);
$.each(responseJson,函数(键,值){
var rowNew=$(“”);
rowNew.children().eq(0.int)(值['dishid']);
rowNew.children().eq(1.int)(值['userid']);
rowNew.children().eq(2).text(值['dishName']);
rowNew.children().eq(3).double(值['numberOfPeople');
rowNew.children();
rowNew.children();
rowNew.children().eq(6).text(值['step']);
rowNew.children().eq(7).text(值['descOfDish']);
rowNew.children().eq(8).int(值['category1']);
rowNew.children().eq(9).int(值['cat