• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 771
  • Last Modified:

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!
0
hgbdelphi
Asked:
hgbdelphi
  • 5
  • 4
  • 2
  • +1
2 Solutions
 
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.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> obj.action='tj.jsp';
>> obj.submit();
>> obj.action='tj2.jsp';
>> obj.submit();

You cannot submit twice.
0
 
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.
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
objectsCommented:
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
 
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!
               
}
0
 
hgbdelphiAuthor Commented:
can i use HttpClient to post the form Data???
0
 
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
0
 
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.
0
 
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.
0
 
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 ()
0
 
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.
0
 
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!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now