how can i post form data to two action with one button???

hi,experts
 my code will post form data to tj2.jsp,and not post to tj.jsp,
how can i post form data to 2 action,and use one button???

ex.htm:
<html>
  <SCRIPT LANGUAGE="JavaScript">
  <!--
     function dosubmit(){
          var obj=document.all.fm1;
            obj.action='tj.jsp';
            obj.submit();
            obj.action='tj2.jsp';
            obj.submit();
       }
  //-->
  </SCRIPT>
   <form name="fm1" method="post">
      <input type="text" name="tx1">
         <input type="button" value="test" onclick="dosubmit();">
   </form>
</html>

tj.jsp:
<html>
  <body>
     <%
         System.out.println("tj.jsp"+request.getParameter("tx1"));
       %>
     tj.jsp post data:<%=request.getParameter("tx1")%>
  </body>
</html>

tj2.jsp:
<html>
  <body>
     <%
          System.out.println("tj2:"+request.getParameter("tx1"));
       %>
     tj2.jsp: post data:<%=request.getParameter("tx1")%>
  </body>
</html>

i want post the form data to tj.jsp and tj2.jsp when i click the test button.
Thanks!
hgbdelphiAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mayank SAssociate Director - Product EngineeringCommented:
Not sure if you can do that. Why do you want to do it, BTW? If you understand the way web-applications work, one request will fetch you one response. If you want to show both JSPs, you can show them in a frameset or an iframe. You can look at AJAX for asynchronous response but I wouldn't recommend taking that route if you don't know anything about it.

The best is perhaps to submit the request to a servlet and let the servlet what is to be done (it can do the same processing which both JSPs were doing together) and then forward the response to a JSP which can show the combined output.
Mayank SAssociate Director - Product EngineeringCommented:
>> obj.action='tj.jsp';
>> obj.submit();
>> obj.action='tj2.jsp';
>> obj.submit();

You cannot submit twice.
Mayank SAssociate Director - Product EngineeringCommented:
The first submit () would have submitted the request to the server. The browser will get the response and then show it on the screen. So whatever you have after the first submit () is gone.
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Mick BarryJava DeveloperCommented:
doesn't really make sense, every request returns a response which is displayed by the browser.
Sending two requests simultaneously would result in two responses, which the browser would not be able to display
in short you cannot do it.
hgbdelphiAuthor Commented:
hi,experts
 i want to submit 1 request to 2 servlet(like: /first.do and /second.do) ,if i can not do that,and i can do like this???

    function dosubmit(){
         var obj=document.all.fm1;
          obj.action='/first.do';
          obj.submit();
      }

in the Servlet(first.do):

public class OutAction extends Action{
       OutModel outModel = new OutModel();
       ArrayList checkCon = new ArrayList();
       public ActionForward execute(ActionMapping mapping, ActionForm form,
                   HttpServletRequest request,
                   HttpServletResponse response){
              
       DataForm inOutForm = (DataForm )form;
                mybean.save(inOutForm);
                //i want save the inOutForm to another Servlet.
               //first i can do response.write("<script language='javascript'>window.location='http://mydom/second.do?tx1="+inOutForm.getTx1+"'</script>");
//but when the post data is too large,i can not use get method,i want use post method ,and what Class i can use ?and can use post not get?

Thanks!
               
}
hgbdelphiAuthor Commented:
can i use HttpClient to post the form Data???
Mayank SAssociate Director - Product EngineeringCommented:
From one servlet, you can definitely post to as many other servlets that you want. The tricky part is if you want to show all the responses. You can use this for POST:

http://www.javaalmanac.com/egs/java.net/Post.html

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
ex.htm:
<html>
  <SCRIPT LANGUAGE="JavaScript">
  <!--
     function dosubmit(){
         var obj=document.all.fm1;
          obj.action='tj.jsp';
          obj.submit();
             }
  //-->
  </SCRIPT>
   <form name="fm1" action = "tj2.jsp" method="post">
      <input type="text" name="tx1">
        <input type="submit" value="test" onclick="dosubmit();">
   </form>
</html>

tj.jsp:
<html>
  <body>
     <%
        System.out.println("tj.jsp"+request.getParameter("tx1"));
      %>
     tj.jsp post data:<%=request.getParameter("tx1")%>
  </body>
</html>

tj2.jsp:
<html>
  <body>
     <%
         System.out.println("tj2:"+request.getParameter("tx1"));
      %>
     tj2.jsp: post data:<%=request.getParameter("tx1")%>
  </body>
</html>

Hey i think this must work here we are just making use of  two basic things one is HTML submit & the other one is Javascript submit.

if this also doesn't work just try out using a a hidden input variable.

please tell me if the above one is not working i will get you the code with hidden variable.
hgbdelphiAuthor Commented:
hi,shivaspk
you code can work,but i don't want post data to one jsp page,and i want post form to tj2.jsp and tj.jsp through one submit.
Mayank SAssociate Director - Product EngineeringCommented:
Try the frameset option. Submit the data it to a servlet and let the servlet forward it to another JSP that has a frameset - first one displays tj.jsp and second one displays t2.jsp. You will not be able to forward the same request from the servlet to both JSPs, so store the result in the session instead using session.setAttribute ()
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
hi hgbdelphi,

if you are using the JSP's as shown above i don't think there is a much concern regaring metod="post" or "get" but any ways, this i flet is a real good question.
I am not sure but i think there is a way to convert post request to get, using java script mostly that may be useful.

Bye.
hgbdelphiAuthor Commented:
hi,experts
now i use HttpClient 3.0  to do it ,this is my code it can work fine.
ex.htm:

<html>
  <SCRIPT LANGUAGE="JavaScript">
  <!--
     function dosubmit(){
          var obj=document.all.fm1;
            obj.submit();
       }

  </SCRIPT>
   <form name="fm1" method="post" action="http://localhost:8080/mytest/webapp/tt/my/sodit.do">
      <input type="text" name="id">
      <input type="text" name="value">
      <input type="text" name="value2">
         <input type="button" value="test" onclick="dosubmit();">
   </form>
</html>


and the sodit.do is :

public class OrderAction extends Action {
      public ActionForward execute(ActionMapping mapping, ActionForm form,
                   HttpServletRequest request,
                   HttpServletResponse response){
      
        System.out.println("orderActionhxstart!");    
      
            String url = "http://localhost:8080/mytest/webapp/yy/my2/mytest.do?action=newSave";
            ClientUtil clientUtil=new ClientUtil(request,url);
            String retStatus=clientUtil.postFormData();
            System.out.println("responseStatus:"+retStatus);
        return mapping.findForward("orderSearch");
}

and in http://localhost:8080/mytest/webapp/yy/my2/mytest.do?action=newSave is:

public class SystemMenuAction extends Action{
      
      
      public ActionForward execute(ActionMapping mapping, ActionForm form,
                   HttpServletRequest request,
                   HttpServletResponse response){
        String s=request.getParameter("id");
        String s2=request.getParameter("value");
        String s3=request.getParameter("value2");
        String action=request.getParameter("action");
        System.out.println("sosxo:"+s+"  s2:"+s2+"  s3:"+s3+"  action:"+action);
        JsUtil.showMessageStatus(response,"a");
}


and the ClientUtil.java is :


import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

import java.util.ArrayList;

public class ClientUtil {
    private HttpServletRequest request=null;
    private String postURL="";

    private ClientUtil(){
          
    }
   
    public ClientUtil(HttpServletRequest request,String postURL){
            this.request=request;
            this.postURL=postURL;
      }
   
      //
      public String postFormData(){
            HttpClient httpClient = new HttpClient();
            PostMethod postMethod = new PostMethod(postURL);
            String retStatus="";
            Enumeration enumLs=request.getParameterNames();
        String name="";
        String value="";
        ArrayList pairList=new ArrayList();
            while (enumLs.hasMoreElements()) {
            name = (String) enumLs.nextElement();
            value = StringUtil.encodedGBK(ParamUtil.getParameter(request, name));
                NameValuePair nameValuePair=new NameValuePair();
                nameValuePair.setName(name);
                nameValuePair.setValue(value);
                pairList.add(nameValuePair);
            }
            NameValuePair[] dataPair=new NameValuePair[pairList.size()];
            for(int i=0;i<pairList.size();i++){
                  dataPair[i]=(NameValuePair)pairList.get(i);
            }
            postMethod.setRequestBody(dataPair);
            int statusCode=0;
            try {
                  statusCode=httpClient.executeMethod(postMethod);
            } catch (Exception e) {
                  statusCode=-1;
                  e.printStackTrace();
            }
            if(statusCode==200){  //commit success!
                  try {
                        retStatus=postMethod.getResponseBodyAsString();
                  } catch (IOException e) {
                        e.printStackTrace();
                  }
            }
            postMethod.releaseConnection();
            return retStatus;
      }
}

and  JsUtil.showMessagStatus() is:

      public static void showMessageStatus(HttpServletResponse response, String status) {
            java.io.PrintWriter out = null;
            try {
                  out = response.getWriter();
            } catch (IOException e) {
                  e.printStackTrace();
            }
            out.print(status);
      }


when i post success,it will show a in console,i think this is ok!

Best Regard!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.