AJAX+Java評論表單示例

在這個例子中,我們創建一個發表評論的表單。表單數據保存在資料庫中,所有發佈的注釋列表顯示在注釋表單下方。

使用Java中的AJAX創建注釋表單示例的步驟
需要按照以下步驟操作:

  • 在資料庫中創建表
  • 加載org.json.jar檔
  • 創建評論表單
  • 創建伺服器端頁面以保存表單數據並列出所有發佈的評論

創建評論表單
在此頁面中,我們創建了一個從用戶獲取輸入的表單。當用戶單擊“發表評論”按鈕時,將調用postComment()函數。在這個函數中編寫了所有的ajax代碼。

檔:index.html

<!DOCTYPE html>
<html>

<head>
    <script>
        var request;
        function postComment() {
            var comment = document.commentform.comment.value;
            var email = document.commentform.email.value;

            var url = "index.jsp?comment=" + comment + "&email=" + email;

            if (window.XMLHttpRequest) {
                request = new XMLHttpRequest();
            }
            else if (window.ActiveXObject) {
                request = new ActiveXObject("Microsoft.XMLHTTP");
            }

            try {
                request.onreadystatechange = function () {
                    if (request.readyState == 4) {
                        var val = request.responseText;
                        document.getElementById('mylocation').innerHTML = val;
                    }
                }//end of function
                request.open("GET", url, true);
                request.send();
            } catch (e) { alert("Unable to connect to server"); }
        }
    </script>
</head>

<body>
    <h1>Comment Form</h1>
    <form name="commentform">
        Enter Comment:<br />
        <textarea name="comment" style="width:300px;height:100px" required>
</textarea><br />
        Enter Email:<br />
        <input type="text" name="email" required /><br /><br />

        <input type="button" value="Post Comment" onclick="postComment()">
    </form>

    <span id="mylocation"></span>
</body>

</html>

創建伺服器端頁面以處理請求
在這個jsp頁面中,編寫資料庫代碼以保存注釋並列印所有注釋。

<!DOCTYPE html>
<html>

<head>
    <style>
        div.box {
            margin: 2px;
            border: 1px solid pink;
            padding: 10px;
            background-color: #e3e3e3
        }
    </style>
</head>

<body>

    <%@ page import="java.sql.*" %>
    <%
String comment=request.getParameter("comment");
String email=request.getParameter("email");
if(comment==null||email==null||comment.trim().equals("")||email.trim().equals("")){
out.print("<p>Please write comment</p>");
}else{

try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
PreparedStatement ps=con.prepareStatement("insert into usercomment(comment1,email) values(?,?)");
ps.setString(1,comment);
ps.setString(2,email);
int i=ps.executeUpdate();

PreparedStatement ps2=con.prepareStatement("select * from usercomment order by id desc");
ResultSet rs=ps2.executeQuery();

out.print("<hr/><h2>Comments:</h2>");
while(rs.next()){
out.print("<div class='box'>");
out.print("<p>"+rs.getString(2)+"</p>");
out.print("<p><strong>By: "+rs.getString(3)+"</strong></p>");
out.print("</div>");
}

con.close();
}catch(Exception e){out.print(e);}

}//end of else
%>
</body>

</html>

上一篇: Java AJAX電子郵件檢驗示例 下一篇: Java+AJAX搜索示例