Posting a servlet plus several actions with a single click event..

Posted on 2004-11-23
Last Modified: 2008-02-01
Need to ask another related question;

If I have a click event with the form action <input type = "submit" onClick="doFile(this)" value='upload file'/>

and the following function. How can I add more functionality to this single click event. ie if I wanted to post the servlet then I wanted to go off and create some other stuff, but on the single click action? Is this possible?

function doFile(btn){
  var form1 = btn.form;
  form1.action = "Uploader/uploadfile";
  return true;
Question by:skellyc
    LVL 63

    Expert Comment

    It is possible, but it is not recommanded.
    If the same page does two processing jobs, submitting a form and changing the page location, you risk that the server abort the submit request.
    LVL 63

    Expert Comment

    Oh, and the regular way of doing that sequence is to do it in sequence.
    That does say: your server side script that takes the submit request creates a response which redirects the user to the new page.


    Author Comment

    thanks for the info, but thats a bit of a problem. It would be much easier to do a couple of functions on that jsp page. As it is, the servlet will get posted, but then how will the servlet know to get back to the page that initiated it, ann set some conditions for that page?
    LVL 63

    Expert Comment

    You stated it already: the sequence has to be done on the JavaServerPages script side, not in JavaScript.
    The JavaScript is pure browser side client scripting.

    Author Comment

    sorry, must be a bit thick:) I'm really not following.

    The jsp is running on the server, and the servlet being actioned is uploading some files to the unix box. My javascript is running client side methods such as post, close form, disable buttons aka. So, the javascript method runs and sets the action to post to the servlet containing file information required for uploading. This much I'm up to.
    But now after the form has been posted and the servlet has run I need to enable/disbale some addiitonal controls on the form using javascript if poss. If inside the servlet what methods can I use to diable/enable controls on the page for example or to run another function?. I assume there must be some kind of mechanism to return to the page and do some more work?
    If you have any examples would be great...?
    LVL 63

    Accepted Solution

    There is an important part in your description that you did not mention.
    It is true that you can do all the form and page control on client side and submit the form data to the server side.
    But after the submit you have NO control about the page becuae you cannot know at what time the server received the submitted data and you can change the page.
    There is a small trick to circumwent that problem.
    You put an invisible IFRAME on your page and submit your form to that IFRAME.
    Like this:
    <form target="myFrame">
    <input type="submit" onClick="doFile(this)" value="upload file" />

    <iframe name="myFrame" height="0" width="0"></iframe>

    Now you can change the page elements after the form is submitted.
    But loading a new page would discard also the hidden iframe and the server would not have a Respons page connection.


    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now