Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2006-03-21
12
Medium Priority
?
760 Views
Last Modified: 2012-06-27
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!
0
Comment
Question by:hgbdelphi
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 16254655
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.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16254658
>> obj.action='tj.jsp';
>> obj.submit();
>> obj.action='tj2.jsp';
>> obj.submit();

You cannot submit twice.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16254661
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.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 92

Expert Comment

by:objects
ID: 16254718
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.
0
 

Author Comment

by:hgbdelphi
ID: 16254786
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!
               
}
0
 

Author Comment

by:hgbdelphi
ID: 16254804
can i use HttpClient to post the form Data???
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 280 total points
ID: 16254858
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
0
 
LVL 23

Expert Comment

by:Siva Prasanna Kumar
ID: 16256058
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.
0
 

Author Comment

by:hgbdelphi
ID: 16256301
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.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16256411
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 ()
0
 
LVL 23

Assisted Solution

by:Siva Prasanna Kumar
Siva Prasanna Kumar earned 120 total points
ID: 16257731
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.
0
 

Author Comment

by:hgbdelphi
ID: 16269145
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!
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month21 days, 4 hours left to enroll

810 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