标签:order cli www url nbsp ack 维护 文档 正方形
servlet有两种方式,一种就是放到配置文件中,一种就是放到文档的注解中。我们选择第二种的时候就不用勾选"将信息添加到部署了"。
try (PrintWriter out = response.getWriter()) { String json = "{\"key\":\"value\"}"; try { Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/apihost", "apihost", "apihost"); String sql = "select * from article_list";
//防止SQL注入,线程 StringBuilder sb = new StringBuilder();
PreparedStatement stmt = conn.prepareStatement(sql); ResultSet rs = stmt.executeQuery(); while (rs.next()) { sb.append("{"); sb.append("\"id\":"); sb.append(rs.getString("id")); sb.append(",\"title\":\""); sb.append(rs.getString("title")); sb.append("\",\"shijian\":"); sb.append(rs.getString("shijian")); sb.append("},"); } out.print("[" + sb.substring(0, sb.length()-1) + "]"); } catch (Exception e) { out.print(e.getMessage()); } } } |
$(function() { $("#homepage").load(function() { RssMaskLayer.show();//正在加载 RssLoadBox.show();//加载框 $.getJSON("http://localhost:8084/api/article/list",function(json) { RssMaskLayer.close();//自己封装的加载图标 RssLoadBox.close();//自己封装的加载框 $("#homepage ul").mapitem(json); $("#homepage ul").find("li").click(function() { $.getJSON("http://localhost:8084/api/article/view?id="+$(this).attr("dataid"),function(json) { $("#articleviewpage").mapjson(json); }); }); }); }).load(); }); |
如果只允许百度连接就把百度的域名放到后面response.setHeader("Access-Control-Allow-Origin", "www.baidu.com");
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setHeader("Access-Control-Allow-Origin", "*"); response.setContentType("text/html;charset=UTF-8");//json,{"id",1,"title":"标题1","matter":"内容1","shijian":2431231},[{"id",1,"title":"标题1","matter":"内容1","shijian":2431231}] |
标签:order cli www url nbsp ack 维护 文档 正方形
原文地址:http://www.cnblogs.com/bonly-ge/p/7076458.html