• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Remain in same page even after submitting the form

 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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:



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.




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
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you want to simply return to the same JSP after the servlet has done it's processing? Or not change the page at all? If the latter, you'll need to use Ajax to prevent the page from being refreshed.
 
Rakesh Keerthi
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bear,

I want to both copy data and post it and stay in the same page with the value copied to the second textbox, i don't want a redirect or a refresh done. it should be like the posting to be done as a masked process i.e. user shouldn't know that there is a post operation going on in the backend. And Bear, i'm not aware of this Ajax i learnt Servlets after you suggested the previous time(and i sincerely want to thank you for that), if i should use ajax could you please provide me a sample code that goes here and also suggest me a good tutorial site for it.

Thanks
 
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

please go through the given tutorial . You can also try jQuery ajax() . But you need to know ajax first.

For the copy stuff you can simply use javascript.

Regards,
Vishal.
 
Rakesh Keerthi
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you vishal
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use a code similar to following code to send POST request in asynchronous mode:

Javascript code:
function customJSFunction(){
proto=window.location.protocol;
host=window.location.host;
context=window.location.pathname;
context=window.location.pathname.substring(0,context.lastIndexOf("/"));
context=context.replace("/pages", ""); //applicationcontext
var xmlHttp;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (exception)
{ // Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (exception)
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (exception)
{
alert("Your browser does not support AJAX!");
return false;
}
}
}
var host = window.location.host;
url = proto+"//"+host+context+"/yourActionServlet";
//alert("url ==== "+url);
xmlHttp.open("POST",url,true); //the value true is for using asynchronous mode
xmlHttp.send(null);
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
alert("POST executed successfully");
}
}
}


Now on your button you can call the above javascript using following code:
onclick="javascript:customJSFunction();return false;"
 
Rakesh Keerthi
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Abishek,
thank you very much for the code, now i can proceed with closing of my project. thank you again.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
this return false; is working fine..but after that i set the alert message to confirm the insertion.. so i couldnt able to insert the form data into Data Base
 
Get out of my mind! Look! A tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic