码迷,mamicode.com
首页 > Web开发 > 详细

jsp servlet ajax入门删除

时间:2014-07-24 22:00:52      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   java   color   os   io   re   

<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的删除异步刷新

jsp servlet ajax入门删除,布布扣,bubuko.com

jsp servlet ajax入门删除

标签:style   blog   http   java   color   os   io   re   

原文地址:http://www.cnblogs.com/yanz/p/3866102.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!