52,797
社区成员
发帖
与我相关
我的任务
分享
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<script language="javascript">
/******************错误处理的方法*******************************/
function onerror(){
alert("您的操作有误!");
}
/******************实例化Ajax对象的方法*******************************/
function getInfo(){
http_request = false;
if (window.XMLHttpRequest) { // 非IE浏览器
http_request = new XMLHttpRequest(); //创建XMLHttpRequest对象
} else if (window.ActiveXObject) { // IE浏览器
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP"); //创建XMLHttpRequest对象
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP"); //创建XMLHttpRequest对象
} catch (e) {}
}
}
if (!http_request) {
alert("不能创建XMLHttpRequest对象实例!");
return false;
}
String url="getInfo.jsp";
//调用返回结果处理函数
http_request.open('GET','getInfo.jsp', true); //创建与服务器的连接
http_request.send(null);
http_request.onreadystatechange = funtion(){
if(http_request.readyState==4){
if(http_request.status==200){
document.getElementById("showInfo").innerHTML=http_request.responsText;
}else{
alert("调用失败");
}
}
};
}
window.onload=function(){
getInfo(); //调用getInfo()方法获取公告信息
window.setInterval("getInfo()", 600000); //每隔10分钟调用一次getInfo()方法
}
</script>
<title>实时显示公告信息(无重构)</title>
</head>
<body>
<div style="border: 1px solid;height: 200px; width:200px;padding: 5px;">
<marquee direction="up" scrollamount="3">
<div id="showInfo"></div>
</marquee>
</div>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<% request.setCharacterEncoding("utf-8"); %>
<!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=UTF-8">
<title>获取信息</title>
</head>
<body>
<jsp:useBean id="conN" class="第13章Ajax.ConnDB" ></jsp:useBean>
<%
String sql = "SELECT title FROM tb_bbsinfo ORDER BY id DESC";
ResultSet rs = conN.getRes(sql);
while(rs.next()){
out.println("<ul><li>"+rs.getString(1)+"</li></ul>");
}
if(!(rs.next())){
out.println("<ul><li>"+"暂无公告信息"+"</li></ul>");
}
%>
</body>
</html>
package 第13章Ajax;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class ConnDB{
public Connection conn = null; // 声明Connection对象的实例
public Statement stmt = null; // 声明Statement对象的实例
public ResultSet rs = null; // 声明ResultSet对象的实例
public Connection getConnection() {
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/conndb?useSSL=true";
String username="root";
String pwd="888555mm";
conn = DriverManager.getConnection(url,username,pwd);
if (conn == null) {
System.err.println("警告: DbConnectionManager.getConnection() 获得数据库链接失败.");
}
}catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public ResultSet getRes(String sql) {
try {
conn = getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
}