资源简介

用jquery中的.getjson()技术和.ajax技术实现从后台中的mysql数据在前台显示,能够直接在myeclipse运行。

资源截图

代码片段和文件信息

package servlet;

import java.io.IOException;
import java.io.PrintWriter;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.linkedHashMap;
import java.util.List;
import java.util.Map;

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





import net.sf.json.JSONobject;

import org.json.JSONArray;

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



public class JsonServlet extends HttpServlet {

/**
 * The doGet method of the servlet. 

 *
 * This method is called when a form has its tag value method equals to get.
 * 
 * @param request the request send by the client to the server
 * @param response the response send by the server to the client
 * @throws ServletException if an error occurred
 * @throws IOException if an error occurred
 */
public void doGet(HttpServletRequest request HttpServletResponse response)
throws ServletException IOException {

     this.doPost(request response);
}

/**
 * The doPost method of the servlet. 

 *
 * This method is called when a form has its tag value method equals to post.
 * 
 * @param request the request send by the client to the server
 * @param response the response send by the server to the client
 * @throws ServletException if an error occurred
 * @throws IOException if an error occurred
 */
public void doPost(HttpServletRequest request HttpServletResponse response)
throws ServletException IOException {
        response.setHeader(“Content-Type:text/xml;charset=utf-8“ “Cache-Control:no-cache“);
response.setContentType(“text/html“);
PrintWriter out = response.getWriter();
//System.out.println(“hello world!“);
String driver=“com.mysql.jdbc.Driver“;
String url=“jdbc:mysql://localhost:3306/test“;
Connection conn=null;
Statement statement=null;
ResultSet rs=null;
List al=new ArrayList();
//加载驱动
try{
Class.forName(driver);
conn=DriverManager.getConnection(url“root““root“);
statement=conn.createStatement();
String sql=“select * from users“;
rs=statement.executeQuery(sql);

while(rs.next()){
Users users=new Users();
int Id=rs.getInt(“ID“);
//System.out.println(ID);
users.setId(Id);
String username=rs.getString(“username“);
users.setUsername(username);
String password=rs.getString(“password“);
users.setPassword(password);
al.add(users);
}
Gson gson=new Gson();
// Mapject> liut=new HashMapject>();
// for(int i=0;i// Users users=al.get(i);
// String bjson=gson.toJson(users);
// liut.put(i+““ bjson);
// }
String tjson=gson.toJson(al);
     out.print(tjson);
     
/

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2016-04-27 14:59  20160427----AjaxJson数据前后台互动\
     目录           0  2016-04-27 14:59  20160427----AjaxJson数据前后台互动\JAS_1\
     文件        1151  2016-04-27 11:36  20160427----AjaxJson数据前后台互动\JAS_1\.classpath
     文件        1330  2016-04-27 10:13  20160427----AjaxJson数据前后台互动\JAS_1\.project
     目录           0  2016-04-27 14:59  20160427----AjaxJson数据前后台互动\JAS_1\.settings\
     文件         522  2016-04-26 17:26  20160427----AjaxJson数据前后台互动\JAS_1\.settings\.jsdtscope
     文件         364  2016-04-26 17:26  20160427----AjaxJson数据前后台互动\JAS_1\.settings\org.eclipse.jdt.core.prefs
     文件         564  2016-04-27 11:36  20160427----AjaxJson数据前后台互动\JAS_1\.settings\org.eclipse.wst.common.component
     文件         414  2016-04-26 17:26  20160427----AjaxJson数据前后台互动\JAS_1\.settings\org.eclipse.wst.common.project.facet.core.xml
     文件          49  2016-04-26 17:26  20160427----AjaxJson数据前后台互动\JAS_1\.settings\org.eclipse.wst.jsdt.ui.superType.container
     文件           6  2016-04-26 17:26  20160427----AjaxJson数据前后台互动\JAS_1\.settings\org.eclipse.wst.jsdt.ui.superType.name
     目录           0  2016-04-27 14:59  20160427----AjaxJson数据前后台互动\JAS_1\WebRoot\
     目录           0  2016-04-27 14:59  20160427----AjaxJson数据前后台互动\JAS_1\WebRoot\meta-INF\
     文件          39  2016-04-26 17:26  20160427----AjaxJson数据前后台互动\JAS_1\WebRoot\meta-INF\MANIFEST.MF
     目录           0  2016-04-27 14:59  20160427----AjaxJson数据前后台互动\JAS_1\WebRoot\WEB-INF\
     目录           0  2016-04-27 14:59  20160427----AjaxJson数据前后台互动\JAS_1\WebRoot\WEB-INF\classes\
     文件      188671  2016-04-27 11:30  20160427----AjaxJson数据前后台互动\JAS_1\WebRoot\WEB-INF\classes\commons-beanutils.jar
     文件      575389  2016-04-27 11:30  20160427----AjaxJson数据前后台互动\JAS_1\WebRoot\WEB-INF\classes\commons-collections-3.2.1.jar
     文件      279781  2016-04-27 13:01  20160427----AjaxJson数据前后台互动\JAS_1\WebRoot\WEB-INF\classes\commons-httpclient.jar
     文件      243016  2016-04-27 11:30  20160427----AjaxJson数据前后台互动\JAS_1\WebRoot\WEB-INF\classes\commons-lang.jar
     文件       38015  2016-04-27 11:30  20160427----AjaxJson数据前后台互动\JAS_1\WebRoot\WEB-INF\classes\commons-logging-1.0.4.jar
     文件       77667  2016-04-27 11:30  20160427----AjaxJson数据前后台互动\JAS_1\WebRoot\WEB-INF\classes\ezmorph-1.0.1.jar
     文件      210856  2016-04-27 10:04  20160427----AjaxJson数据前后台互动\JAS_1\WebRoot\WEB-INF\classes\gson-2.3.1.jar
     文件      159123  2016-04-27 11:30  20160427----AjaxJson数据前后台互动\JAS_1\WebRoot\WEB-INF\classes\json-lib-2.4-jdk15.jar
     文件      703265  2016-04-27 09:10  20160427----AjaxJson数据前后台互动\JAS_1\WebRoot\WEB-INF\classes\mysql-connector-java-5.1.6-bin.jar
     文件      119888  2013-06-09 13:38  20160427----AjaxJson数据前后台互动\JAS_1\WebRoot\WEB-INF\classes\org.json.jar
     目录           0  2016-04-27 14:59  20160427----AjaxJson数据前后台互动\JAS_1\WebRoot\WEB-INF\classes\servlet\
     文件        3580  2016-04-27 14:54  20160427----AjaxJson数据前后台互动\JAS_1\WebRoot\WEB-INF\classes\servlet\JsonServlet.class
     文件         905  2016-04-27 13:01  20160427----AjaxJson数据前后台互动\JAS_1\WebRoot\WEB-INF\classes\servlet\Users.class
     文件      108874  2016-04-27 11:30  20160427----AjaxJson数据前后台互动\JAS_1\WebRoot\WEB-INF\classes\xom-1.0.jar
     目录           0  2016-04-27 14:59  20160427----AjaxJson数据前后台互动\JAS_1\WebRoot\WEB-INF\lib\
............此处省略19个文件信息

评论

共有 条评论