2014-10-20 81 views
0

我是一個初學者,我試圖做一個簡單的應用程序來顯示從MySQL使用JavaScript和jQuery,json對象的表。這不是給我任何錯誤,但我失去了如何繼續。請提供您的反饋!使用jquery和ajax顯示來自mysql的數據

這是我books.java

import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.util.ArrayList; 

public class Books { 
    private int id; 
    private String title; 
    private String author; 
    private float price; 
    private int qty; 
    public int getId() { 
     return id; 
    } 
    public void setId(int id) { 
     this.id = id; 
    } 
    public String getTitle() { 
     return title; 
    } 
    public void setTitle(String title) { 
     this.title = title; 
    } 
    public String getAuthor() { 
     return author; 
    } 
    public void setAuthor(String author) { 
     this.author = author; 
    } 
    public float getPrice() { 
     return price; 
    } 
    public void setPrice(float price) { 
     this.price = price; 
    } 
    public int getQty() { 
     return qty; 
    } 
    public void setQty(int qty) { 
     this.qty = qty; 
    } 
    public Books(int id, String title, String author, float price, int qty) { 
     super(); 
     this.id = id; 
     this.title = title; 
     this.author = author; 
     this.price = price; 
     this.qty = qty; 
    } 

    public Books() { 

    } 
    public ArrayList <Books> getBooks(ArrayList<Books> bookList){ 

     Connection conn =null; 
     ResultSet rset = null; 

     try { 
      Class.forName("com.mysql.jdbc.Driver"); 
      conn = DriverManager.getConnection(
        "jdbc:mysql://localhost:3306/ebookshop", "root", "root"); 
      PreparedStatement pst = (PreparedStatement) conn 
        .prepareStatement("SELECT * from books"); 
      rset = pst.executeQuery(); 

      while (rset.next()){ 
       Books books = new Books(); 
       books.setId(rset.getInt("id")); 
       books.setAuthor(rset.getString("author")); 
       books.setTitle(rset.getString("title")); 
       books.setPrice(rset.getFloat("price")); 
       books.setQty(rset.getInt("qty")); 
       bookList.add(books); 

      } 
     } catch (SQLException e) { 
      System.out.println("Could not connect to DB" + e); 
     } catch (ClassNotFoundException classexpt){ 
      System.out.println("Couldn't find the class" + classexpt); 
     } 

     return bookList; 

    } 

} 

Servlet的文件:

 import java.io.IOException; 
     import java.util.ArrayList; 

     import javax.servlet.RequestDispatcher; 
     import javax.servlet.ServletException; 
     import javax.servlet.annotation.WebServlet; 
     import javax.servlet.http.HttpServlet; 
     import javax.servlet.http.HttpServletRequest; 
     import javax.servlet.http.HttpServletResponse; 

     /** 
     * Servlet implementation class BooksServlet 
     */ 
     @WebServlet("/BooksServlet") 
     public class BooksServlet extends HttpServlet { 
      private static final long serialVersionUID = 1L; 

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

      /** 
      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 
      */ 
      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 

response.setContentType("application/json"); 
       Books books = new Books(); 
       ArrayList <Books> bookList = new ArrayList <Books>(); 
       bookList = books.getBooks(bookList); 
       request.setAttribute("bookList", bookList); 

    JSONObject obj=new JSONObject(); 
      JSONArray arr = new JSONArray(); 
      for(int i = 0 ; i< bookList.size() ; i++) 
      { 
       Books b = bookList.get(i); 

       obj.put("id", b.getId()); 
       obj.put("title", b.getTitle()); 
       obj.put("author", b.getAuthor()); 
       obj.put("price", b.getPrice()); 
       obj.put("qty", b.getQty()); 

       arr.add(obj.toString()); 

       obj = new JSONObject(); 
      } 

       String address = "DisplayBook.jsp"; 
       RequestDispatcher dispatcher = 
         request.getRequestDispatcher(address); 
         dispatcher.forward(request, response); 
      } 

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

     } 

JSP文件:

<%@ page language="java" 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> 
<script type="text/javascript" src="js/jquery-1.11.0.min.js"></script> 
<script type="text/javascript"> 
$(document).ready(function(){ 
    function showBook(){ 
     $.ajax({ 
      type:"GET", 
      url:"BooksServlet.java", 
      dataType: "json", 
      data: JSON.stringify({ bookList: bookdata }), 
      success:function(data){ 
       $("#content").html(data); 
      } 
     }); 
    } 
    showBook(); 
}); 
</script> 

<h3 align="center">Manage Book Details</h3> 
<table border="1" align="center"> 
    <tr> 
     <td> <input type="button" id="display" value="Display All Data" /> </td> 
    </tr> 
</table> 
<div id="content" align="center"></div> 
<table> 
    <tr> 
     <td>id</td> 
    </tr> 
</table> 

</body> 
</html> 

JSP文件是我迷路了。我想我會將數據正確地發送到列表中,但沒有收到。或者我發送錯誤的數據在servlet中? 任何幫助,將不勝感激!

回答

0

BooksServlet.java不應該在您的jquery url中,而應該是一個返回JSON的servlet。我沒有得到你想要用你的jQuery代碼做什麼。現在它似乎準備好接收HTML,而不是JSON。

  • 我建議你像螢火蟲或類似請到網絡和analize請求/響應正在創建使用插件。
  • 我會使用DataTables處理JSON在前端側
  • 我會用一個JSON解析器,用於後端部分例如「Struts 2的結果類型JSON
+0

我添加了response.setContentType(「application/JSON」),那是好還是我該如何讓它接收JSON? – zydexo 2014-10-20 10:24:21

+0

將contentType更改爲JSON不會自動將您的內容更改爲JSON。首先熟悉Ajax和HTML,然後從JSON開始。 – PbxMan 2014-10-20 10:49:06

0

嘗試訪問[服務器]/[背景]/BooksServlet,例如本地主機:8080/myappp/BookServlet,因爲你的/ BookServlet是在你的servlet映射URL

看到這個@WebServlet annotation with Tomcat 7http://www.codejava.net/java-ee/servlet/webservlet-annotation-examples

順便說一句,我是。仍然沒有得到,您的Ajax URL轉到/ BookServlet並返回json,但在那個servlet中不返回json數據。

+0

是的,我已經添加了該代碼。 – zydexo 2014-10-20 09:34:25