We help IT Professionals succeed at work.

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

hgbdelphi
hgbdelphi asked
on
Medium Priority
790 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!
Comment
Watch Question

Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
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 SPrincipal Technologist
CERTIFIED EXPERT

Commented:
>> obj.action='tj.jsp';
>> obj.submit();
>> obj.action='tj2.jsp';
>> obj.submit();

You cannot submit twice.
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
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.
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
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.

Author

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

Author

Commented:
can i use HttpClient to post the form Data???
Principal Technologist
CERTIFIED EXPERT
Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Siva Prasanna KumarPrincipal Solutions Architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
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.

Author

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 SPrincipal Technologist
CERTIFIED EXPERT

Commented:
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 Architect
CERTIFIED EXPERT
Top Expert 2006
Commented:
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.

Author

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!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.