6

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 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";  
    }  

the methods for the button are declared as follows:

<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> 

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.

Thanks

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Rakesh
  • 564
  • 1
  • 8
  • 25
  • Just as an option - use javascript AJAX calls to servlet to send/receive data. –  Aug 30 '12 at 13:57
  • Hi Rafael thanks for the reply, i'm new to java and also unaware of Ajax plz let me know how to achieve this in regular jsps or javascripts – Rakesh Aug 30 '12 at 14:03

3 Answers3

1

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.

Here is AJAX tutorial

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

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.

Here is the example of JSP and AJAX combination

fean
  • 546
  • 4
  • 12
  • 37
0

You can invoke ajax in your jsp like this:

function test(tg1){
  context = '${pageContext.request.contextPath}';
  var http_request = getXMLHttpRequest();
            if (!http_request) {
        alert('Giving up :( Cannot create an XMLHTTP instance');
        return false;
    }

    http_request.open("POST", context + "/ServletName?tgt1=" + tg1, true);
    http_request.onreadystatechange = function() {
        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
                var response = http_request.responseText;
                //do something if you have a response from the servlet
            }
        }
    };
    http_request.send(null);
}

In your servlet, remove your redirect code.

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();  

You can add a response from the servlet using response.getWriter().write("something");

Russell Gutierrez
  • 1,372
  • 8
  • 19
  • hi russell thanks for the code.could you please let me know if i use this only in jsps, and no servlets used atall, how is this code to be modified. – Rakesh Sep 03 '12 at 07:02
0

In your code I can see (parts of) 2 JavaScript functions move() and invoke(but).
The first one moves data from one DOM element ('Allocation') to another ('tgt1'). The second is attempting to submit data to the server/jsp page.

Is that correct and that what you are trying to do?

BTW: you should call them in the order of move(); invoke(...);, NOT other way around because once the form is submitted move() will not run - nothing can happen on the client side, because the processing left the page.

I noticed that in your move() function you disable DOM element "Send" (document.getElementById("Send").disabled=true;) which happens to be your submit button. This action will prevent the form from being submitted, so you should probably remove that line.

Germann Arlington
  • 3,315
  • 2
  • 17
  • 19