jsp群组与ajax聊天

时间:2015-05-01 10:54:21

标签: javascript jquery ajax jsp

你好每一个我正在尝试用ajax开发一个jsp群聊...  这是我的索引页

function SChat() {

    var uname = form1.uname.value;
    var msg = form1.msg.value;
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function()
    {
         if (xmlhttp.readyState == 4 && xmlhttp.status == 2000)
         {
                document.getElementById('chatlogs').innerHTML = xmlhttp.responseText;

         }
   }

   xmlhttp.open("GET", "insert.jsp?uname=" + uname + "&msg=" + msg",true);
   xmlhttp.send();
}

Html:

<form name="form1" >
        Enter Your Chat name <input type="text" name="uname"> </br>
        Message <textarea name="msg"></textarea>
        </br>
        <a href ="#" onclick="SChat()">send</a>
        </br>
        <div id="chatlogs">
            llllllll
        </div>
</form>

这是我的插入页面

String uname = request.getParameter("uname");
String msg = request.getParameter("msg");

Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/chatbox",
        "root", "");
Statement st = con.createStatement();
st.executeUpdate("insert into logs(msg,username) values ('" + msg + "','" + uname + "')");

String query = "select * from logs ";
ResultSet rs = st.executeQuery(query);
while (rs.next()) {
    out.println(rs.getString(3));
    out.println(rs.getString(3));
}    

问题是值是不是传递给插入页面而结果是没有在内部html中打印任何想法?

1 个答案:

答案 0 :(得分:0)

xmlhttp.status == 2000,尝试使用200:)

在运行插入请求之前,您应该小心清理用户输入,但这与您的问题无关。