<script type="text/javascript">
var xmlreq = false;  
function createXmlHttpRequest()
{      
    if (window.XMLHttpRequest) {  
        xmlreq = new XMLHttpRequest();    
        } else if (window.ActiveXObject) {
            try {                 
                xmlreq = new ActiveXObject("Msxml2.XMLHTTP");      
                } catch (e1) {                                                  
                    try {
                        xmlreq = new ActiveXObject("Microsoft.XMLHTTP");
                        } catch (e2) {      
                            
                        }
                        }
                }
    return xmlreq;
}

function del(id){
    var request = createXmlHttpRequest();
    request.open("post","<%=path%>/StuServlet?methodName=delete&id="+id);
    request.send();
    request.onreadystatechange=function(){
        if (xmlreq.readyState == 4)
          {
            if (xmlreq.status == 200)
            {    
                var check = xmlreq.responseText;  
                show (check);
<TD><a href="javascript:void(0)" onclick="del(<%=id%>)">刪除</a></TD>

  

  public int delete(int i){
        	Connection conn = new ConnectionUtil().openConnection(); 
        	try{
        		PreparedStatement ps = conn.prepareStatement(DELETE_STUDENT_SQL); 
        		ps.setInt(1,i);
        		ps.executeUpdate();
        		return 1;
        	}catch(SQLException e){
        		e.printStackTrace();
        		
        	}finally{
        		try{
        			conn.close();
        		}catch(SQLException e){
        			e.printStackTrace();
        		}
        	}
			return 0;
        }

  

private void delete(HttpServletRequest request, HttpServletResponse response) 
			throws IOException {
		// TODO Auto-generated method stub
		int id =Integer.parseInt(request.getParameter("id"));
		StudentDao dao =new StudentDaoImpl();
		PrintWriter out = response.getWriter();
		
		if(dao.delete(id)==1){
			out.write("OK");
		}
		else{
			out.write("NO");
		}
		//query(request, response);
		out.close();
	}

  自己硬着头皮做的一个ajax的删除异步刷新

posted on 2014-07-24 17:22  严zhi  阅读(547)  评论(0编辑  收藏  举报