1

I want to create a cron job, which executes a .jar every week. This jar is supposed to query a MySQL server and save the values. That's the easy part which i can easily do. But now I need to open a web page and enter 2 values in text boxes and after that press a button. Can it be done without any need of GUI? Is it possible to accomplish this with pure java? If yes, any suggestions on how to insert the values? Which library would simplify that task? Thanks in advance. (I would prefer to use no library if its possible). I have to enter these 2 textfields and then press the send test email to button

This is how the website looks like

Or would a PHP-Script be better suited for this?

Code of the webpage:

<body>
<form id="j_id2" name="j_id2" method="post" action="/SchnitzelDB/app/mail;jsessionid=BCF1C0890EE83C0C7A4B7B916F4360A2?execution=e1s4" enctype="application/x-www-form-urlencoded">
<input type="hidden" name="j_id2" value="j_id2" />
<table>
<tbody>
<tr>
<td><input type="text" name="j_id2:j_id4" value="9" /></td>
<td><input type="submit" name="j_id2:j_id5" value="Send Email" /></td>
</tr>
<tr>
<td><input type="text" name="j_id2:j_id6" value="email" style="width:200px;" /></td>
<td><input type="submit" name="j_id2:j_id7" value="Send Test Email To" /></td>
</tr>
</tbody>
</table>
<input type="hidden" name="javax.faces.ViewState" id="javax.faces.ViewState" value="e1s4" />
</form>
</body>
XtremeBaumer
  • 6,275
  • 3
  • 19
  • 65
  • Consider [Selenium](http://www.seleniumhq.org/). – SantiBailors Mar 02 '17 at 09:35
  • There are headless browsers that can help you with this task. here is a list for some of them: https://github.com/dhamaniasad/HeadlessBrowsers – Daniel Shalev Mar 02 '17 at 09:35
  • 2
    If all you need is to post a HTML form programmatically, see : http://stackoverflow.com/questions/7581550/how-to-send-post-form-with-java – Arnaud Mar 02 '17 at 09:36
  • @SantiBailors selenium seems to need a web browser to work. is it possible to use it without any web browser? and if yes, how? – XtremeBaumer Mar 02 '17 at 09:48
  • Sorry, I don't know that, but the headless browsers mentioned in DanielShalev comment might be relevant. However, now that I understand the question better, @Berger 's comment seems to be spot on, if I were you I'd process that one first. – SantiBailors Mar 02 '17 at 09:53

1 Answers1

0

So I googled a bit more around and found something more suitable for my problem. It is htmlunit. Its fairly easy to access the content on the webpage and fill them with values or click buttons.

final HtmlForm form = page.getFormByName("j_id2");
final HtmlTextInput textField = form.getInputByName("j_id2:j_id4");
final HtmlTextInput textField1 = form.getInputByName("j_id2:j_id6");

final HtmlSubmitInput button = form.getInputByName("j_id2:j_id5");
for (String s : list) {
    textField.setValueAttribute(String.valueOf(week));
    textField1.setValueAttribute(s);
    button.click();
}

this is all i had to do in order to achieve the desired behavior

XtremeBaumer
  • 6,275
  • 3
  • 19
  • 65