这个我可能会说得有点乱。希望大虾帮忙下。
情况是这样的。我用servlet处理一个jsp页面,简单说就是做了两个功能,一个删除,一个修改。代码如下
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
String action=request.getParameter("option");
if(action.equals("remove")){
remove(request,response);
}
if(action.equals("modify")){
modify(request,response);
}
}
然后在jsp页面中,有的时候能修改数据有时时候又不能了。而且删除几乎是不可以。不过有几次我点的时候也删除了
用的是Ajax处理的。两个代码我分别贴一下
function removeCart(cid,bid){
var xmlhttp;
//根据不同浏览器初始化xmlhttp
try{
//IE 6+
xmlhttp=new ActiveXObject("Msxl2.XMLHTTP");
}catch(e){
try{
//FireFox
xmlhttp=new ActiveXObject("Micrsoft.XMLHTTP");
}catch(e){
try{
//IE 5.5+
xmlhttp=new XMLHttpRequest();
}catch(e){
alert("您的浏览器不支持Ajax!");
}
}
}
xmlhttp.open("GET","modify?option=remove&bid="+bid,true);
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
var indexRow=document.getElementById("cart_"+cid).rowIndex;
document.getElementById("details").deleteRow(indexRow);
}
}
xmlhttp.send(null);
}
function count(cid,bid){
var count=parseInt(document.getElementById("nums_"+cid).value);
if(count<0||isNaN(count)){
alert("请输入正确的数量");
document.getElementById("nums_"+cid).value=0;
document.getElementById("price_"+cid).innerHTML="0.0";
return;
}
if(count==0){
document.getElementById("nums_"+cid).value=0;
document.getElementById("price_"+cid).innerHTML="0.0";
}
var xmlhttp;
try{
xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
}catch(e){
try{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}catch(e){
xmlhttp=new XMLHttpRequest();
}
}
xmlhttp.open("GET","modify?option=modify&bid="+bid+"&count="+count,true);
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
var price=parseFloat(document.getElementById("hidden_price_"+cid).value);
document.getElementById("price_"+cid).innerHTML=count*price;
}
}
xmlhttp.send(null);
}
试了好多办法,刚刚吃饭前可以修改,还删除了下,现在又不行了。真心不知道什么原因,请大虾帮助下。。小弟不胜感谢

解决方案 »

  1.   

    这种问题在开发中也遇到过,还是你的程序有点问题,某个地方不完善!
    看看数据库中 是不是有不能为空的字段,如int类型的 。
    我觉得问题出在你传递的参数上面,从那里入手,后天调试一下,看看什么错误!
      

  2.   

    我servlet 有时候乱码,有时候不乱码,⊙﹏⊙b汗
      

  3.   

    补充一下
    String action=request.getParameter("option");if(action.equals("remove")){
    remove(request,response);
    }
    if(action.equals("modify")){
    modify(request,response);
    }
    这是Servlet里处理的相关代码这个JSP页面的
    xmlhttp.open("GET","modify?option=modify&bid="+bid+"&count="+count,true);这个可以
    xmlhttp.open("GET","modify?option=remove&bid="+bid,true);
    这个就不可以了
      

  4.   

    回3楼我有时也这样。有时也纠结。
    不过我JSP页面后来统一成gbk。传递的时候会设置request为gbk.
    不行就用new String转化。。好像就OK了
    好像说是数据库是char,java是字节码的原因
      

  5.   

    先new一个date = new Date()然后把“date=”+date 加到url上试试  可能是你缓存的问题  你的请求每次都没变 它默认可能上缓存里拿 所以就不会执行后台的读取操作
      

  6.   

    再问一下楼上的大大们,感觉好像你们说的有道理
    不过事务提交是什么个概念,我是小菜菜,请详细解释一下。
    这样吧,我贴那个remove的方法出来 下
    public void remove(HttpServletRequest request,HttpServletResponse response)
    throws ServletException,IOException{

    int bid=Integer.valueOf(request.getParameter("bid"));
    List<Book> book_in_cart=(List<Book>)request.getSession().getAttribute("book_in_cart");
    for(int i=0;i<book_in_cart.size();i++){
    if(book_in_cart.get(i).getBid()==bid){
    book_in_cart.remove(i);
    break;
    }
    }
    }
    下面是修改的具体方法
    public void modify(HttpServletRequest request,HttpServletResponse response)
    throws ServletException,IOException{

    int bid=Integer.valueOf(request.getParameter("bid"));
    int count=Integer.valueOf(request.getParameter("count"));
    List<Book> book_in_cart=(List<Book>)request.getSession().getAttribute("book_in_cart");
    for(int i=0;i<book_in_cart.size();i++){
    if(book_in_cart.get(i).getBid()==bid){
    book_in_cart.get(i).setCount(count);
    break;
    }
    }
    }