日期:2014-05-16  浏览次数:20464 次

Ajax实现jsp里面某个输入框内容修改之后后台数据库自动更新
对应在数据库中表格 rocars表的msg_id,ccrn两个字段。现在要实现在界面上修改ccrn的值,ajax提交到response.jsp页面,并调用RocarsEntiy.updateCcrn方法更新对应的ccrn,最后无刷新显示。

request.jsp页面中有rocarsId,和ccrn两个text。
对应在数据库中表格 rocars表的msg_id,ccrn两个字段。现在要实现在界面上修改ccrn的值,ajax提交到response.jsp页面,并调用RocarsEntiy.updateCcrn方法更新对应的ccrn,最后无刷新显示
代码:

request.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
pageEncoding="ISO-8859-1"%> 
<!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>Insert title here</title> 
<script language="javascript"><!-- 
function GetXmlHttpObject(){ 
    var xmlHttp = null; 
    try{ 
        xmlHttp = new XMLHttpRequest(); 
    }catch(e){ 
        try{ 
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); 
        }catch(e){ 
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
        } 
    } 
    return xmlHttp; 

} 
function updateCcrn(rocarsId,ccrn){ 
alert(rocarsId) 
alert(ccrn) 
    xmlHttp = GetXmlHttpObject(); 
    if(xmlHttp == null){ 
        alert ("you browser don't support the ajax"); 
         return; 
    } 
    var url = "./response.jsp"; 
    url = url + "?rocarsId="+ rocarsId; 
    url = url + "&ccrn="+ ccrn; 
    url = url + "&sid ="+ Math.random(); 
    xmlHttp.onreadystatechange = stateChanged; 
    xmlHttp.open("GET", url, true); 
    xmlHttp.send(null); 
} 
function getCcrn(str){ 
    xmlHttp = GetXmlHttpObject(); 
    if(xmlHttp == null){ 
        alert ("you browser don't support the ajax"); 
         return; 

    } 
    var url = "./response.jsp"; 
    url = url + "?q="+ str; 
    url = url + "&sid ="+ Math.random(); 
    xmlHttp.onreadystatechange = stateChanged; 
    xmlHttp.open("GET", url, true); 
    xmlHttp.send(null); 
} 
function stateChanged() 
{ 
    if(xmlHttp.readyState==4) 
    { 
        document.getElementById("rocarsccrn").value = xmlHttp.responseText; 
    } 
} 
// --></script> 
</head> 
<body> 
<form name="form1" action="" method="post"> 
    <label>rocarsId:</label><input type="text" name="rocarsId" value="140" /> 
    <label>ccrn:</label><input type="text" id="rocarsccrn" name="rocarsccrn" onchange="updateCcrn(document.form1.rocarsId.value,this.value)"/> 
</form> 
</body> 
</html>


response.jsp:
<%@ page language="java" contentType="text/plain; charset=UTF-8" 
pageEncoding="UTF-8"%> 
<%@ page import="com.lwf.eus.util.*,java.util.*,com.lwf.eus.entity.*,com.lwf.eus.bean.*" %> 
<% 
    String rocarsId = request.getParameter("rocarsId"); 
    String ccrn = request.getParameter("ccrn"); 
    System.out.println("rocarsId:" + rocarsId); 
    System.out.println("ccrn:" + ccrn); 
    RocarsEntity.updateCcrnById(rocarsId,ccrn); 
    out.print(ccrn); 
%>


RocarsEntity.java:
package com.lwf.eus.entity;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import com.lwf.eus.bean.RocarsBean;
import com.lwf.eus.util.ConnectionManager;

public class RocarsEntity {
	public static Vector getRocarsList() {
		Vector vRocars = new Vector();
		// Connection conn = ConnectionManager.getConnection();
		Connection conn = ConnectionManager.getConnectionFromDS();
		Statement st = ConnectionManager.createStatement(conn);
		String sql = "select msg_id,ccrn from rocars";
		ResultSet rs = null;
		try {
			rs = st.executeQuery(sql);
			while (rs.next()) {
				RocarsBean rocars = new RocarsBean();
				rocars.setRocarsId(rs.getString(1));
				rocars.setCcrn(rs.getString(2));
				vRocars.addElement(rocars);
			}
		} catch (S