提交jsp表单后仍保留在同一页面

问题描述:

我正在使用jsp将数据发布到servlet,但在发布数据后我想留下来查看jsp。
简要:
1)我在这里有一个带有2个文本框的jsp。我使用javascript按钮点击将数据从一个复制到另一个。
2)我使用相同的按钮将数据发布到数据库。
3)我希望这两个动作一次完成,不应该转到第三个jsp(servlet post结果),但是,应该转到我使用的另一个jsp。

I'm using a jsp to post data to a servlet, but after posting the data i want to stay back insame jsp. Briefly: 1) I have a jsp here with 2 textboxes. i use a javascript to copy data from one to another on a button click. 2) I use the same button to post data to a database. 3) I want both the actions to be done at a time and should not go to the third jsp(servlet post result), but , should go to another jsp i use.

我能够单独处理这两件事,但我无法做到这一点。数据在数据库中更新并在第一个文本中显示一个新行(这是我的错误,我正在使用重定向方式)或将数据从第一个文本框移动到另一个文本框并且不进行数据发布。请帮我解决这个问题。下面是我用来做的代码。

i'm able to handle these 2 things seperately but together i'm unable to do it. the data gets updated in database and shows a new line in the first text(that's my fault i'm using the redirect way) or moves the data from first textbox to another and doesn't do the data posting. please help me about doing it. below is the code piece i used to do it.

用于复制数据和发布数据的java脚本是:

java script to copy data and post data are:

function move(){  

document.getElementById('tgt1').value = document.getElementById('Allocation').value;  
document.getElementById('Allocation').value="";  
document.getElementById("Send").disabled=true;  

}
function invoke(but)  
  {  
   if(but==0)   
    {  
        document.myform.action="Alloc_Insert.do";  
    }  

按钮的方法声明如下:

<table><tr><center><td><input type="Submit" value="Allocate" id="Send"     onClick="invoke(0);move();" style="width:150px" style="font-size:100%"/></td></center></tr>    </table> 

我使用的servlet代码如下。

and the servlet code i used are as follows.

ResultSet rs=null;  
        String Add=request.getParameter("tgt1");  
        String user=(String) session.getAttribute("myusername");  
        Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();Date d1 = new Date();    
        String d1_str = new SimpleDateFormat("yyyy-MM-dd").format(d1);   
        Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","system","tiger");  
        PreparedStatement ps=con.prepareStatement("UPDATE SCOPE1 SET ALLOCATED='"+d1_str+"', SPECIALIST='"+user+"' WHERE DBID='"+Add+"'");  
        con.setAutoCommit(true);  
        int i=ps.executeUpdate();  
        if(i==1)  
                           {  
             String redirectURL= "Update_Counts.jsp";  
            response.sendRedirect(redirectURL);  
                        }  
                   else{  

            out.print("retry");  
                  }  

我想发布数据以及与文本框值相同的数据复制到第二个文本框,因为我将它用于我的进一步参考。

i want to post the data aswell as be on the same with the textbox value copied into second textbox, as i would be using it for my further referrence.

谢谢

正如我在评论中提到的,考虑一下javascript异步调用(AJAX)到你的servlet。

As I mentioned in my comment, consider javascript asynchronous calls (AJAX) to your servlet.

在这种情况下,你的页面不会改变,你可以在后台与服务器进行所有的交互。

In this case your page won't change and you can do all your interaction with the server in the background.

这是AJAX教程

如果您不是经验丰富的Javascript程序员,请考虑这个JavaScript教程

If you are not experienced Javascript programmer, then consider this JavaScript tutorial

JSP本质上是一个servlet,即它是服务器端代码,但你可以将javascript嵌入到JSP页面中,在客户端的浏览器(客户端)上工作,并在后台向服务器发送/接收数据包,而无需将整个页面提交给服务器。

JSP on its nature is a servlet, i.e it is server side code, but you can embed javascript to your JSP page, that will work on the client's browser (client side), and send/receive data packets to the server in the background, without submitting the whole page to the server.

以下是JSP和AJAX组合的示例