使用桌面应用程序中的Java发布表单

时间:2012-10-25 20:03:20

标签: java forms post

我想在我拥有的网站上将数据发布到html表单。到目前为止,我正在测试它:

HttpClient httpClient = new HttpClient();
                PostMethod postMethod = new PostMethod("website URL");
                postMethod.addParameter("company_label", customerCompany);
                postMethod.addParameter("customer_name_label", customerName);
                try {
                    httpClient.executeMethod(postMethod);
                } catch (HttpException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                if (postMethod.getStatusCode() == HttpStatus.SC_OK) {
                    String resp = postMethod.getResponseBodyAsString();
                } else {
                     //...postMethod.getStatusLine();
                }

但它会抛出此错误

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
    at org.apache.commons.httpclient.HttpClient.<clinit>(HttpClient.java:66)
    at main.main(main.java:113)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)

我是在正确的道路上做我想做的事情,还是应该以不同的方式做这件事?

我尝试使用的表单看起来像

<form name ='XXXXXXX' method='post' onSubmit='return validate_b3cdee6457e94aae9dee29379f9b9470()'>
  <input type='hidden' name='done' value='XXXX' border='0'>
  <input type='hidden' name='version' value='2' border='0'>
  <table border='0' cellspacing='1' cellpadding='2' width='50%' >
    <tr>
      <td><table border='0' cellspacing='1' cellpadding='2' width='50%' >
          <tr>
            <td class='DarkText' style='color:red'><div id='XXXXXXXX' style='display:none;'></div></td>
          </tr>
        </table>
        <div id='div_webForm_b3cdee6457e94aae9dee29379f9b9470'>
          <table border='0' cellspacing='1' cellpadding='2' width='100%' >
            <tr valign='top' id='text_field_row_company' >
              <td  style=''  width='20%' id="company_label"  nowrap class='FormLabel' >Company</td>
              <td class='FormField'      ><input id="company" type='text' name='company' size='30' maxlength='255' class='inputForm'  value=""       ></td>
            </tr>
            <tr valign='top' id='text_field_row_customer_name' >
              <td  style=''  width='20%' id="customer_name_label"  nowrap class='FormLabel' >Customer Name</td>
              <td class='FormField'      ><input id="customer_name" type='text' name='customer_name' size='30' maxlength='255' class='inputForm'  value=""       ></td>
            </tr>
            <tr valign='top' id='text_field_row_phone' >
              <td  style=''  width='20%' id="phone_label"  nowrap class='FormLabel' >Phone</td>
              <td class='FormField'      ><input id="phone" type='text' name='phone' size='30' maxlength='255' class='inputForm'  value=""       ></td>
            </tr>
            <tr valign='top' id='text_field_row_email' >
              <td  style=''  width='20%' id="email_label"  nowrap class='FormLabel' >Email</td>
              <td class='FormField'      ><input id="email" type='text' name='email' size='30' maxlength='255' class='inputForm'  value=""       ></td>
            </tr>
            <tr valign='top' id='text_field_row_needs' >
              <td  style=''  width='20%' id="needs_label"  nowrap class='FormLabel' >Needs</td>
              <td class='FormField'      ><input id="needs" type='text' name='needs' size='30' maxlength='255' class='inputForm'  value=""       ></td>
            </tr>
            <tr valign='top' id='field_row_id_other_needs'  >
              <td    style='' width='20%' height=28px nowrap class='FormLabel'  id='field_cell_label_id_other_needs' >Other Needs</td>
              <td     class='FormField'  id='field_cell_value_id_other_needs'  ><textarea id='other_needs' name='other_needs' rows='5' cols='30'  class='inputForm'   ></textarea></td>
            </tr>
            <tr>
              <td></td>
              <td><input type='submit' name='submitButtonName' value='Submit' border='0' ></td>
            </tr>
          </table>
        </div></td>
    </tr>
  </table>
</form>

4 个答案:

答案 0 :(得分:1)

是。 HTTPClient代码试图使用来自其他地方的代码......来自Apache日志记录。您需要将JAR包含在类路径中(在Eclipse中,它位于Project Properties-&gt; Java Build Path:Libraris选项卡中。

请参阅:Getting java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory exception

和其他人。

答案 1 :(得分:1)

您可能会错过lib文件夹中的相关jar文件:

在此处下载:http://commons.apache.org/logging/download_logging.cgi

答案 2 :(得分:1)

它抱怨Apache Commons Library。请从Apache commons download下载commons-logging.xxxxxx.jar并添加项目lib / classpath。

答案 3 :(得分:1)

您的代码可以正常使用3罐和Open JDK 1.6

commons-httpclient-3.1.jar
commons-logging/1.0.4/commons-logging-1.0.4.jar
commons-codec/1.2/commons-codec-1.2.jar

我测试过:

PostMethod method = new PostMethod("http://search.yahoo.com/search");
祝你好运!