I'm trying to fill-out a form automatically and press a button on that form and wait for a response. How do I go about doing this?
To be more particular, I have a a --HUGE-- collection DNA strains which I need to compare to each-other. Luckily, there's a website that does exactly what I need.
Basically, I type-in 2 different sequences of DNA and click the "Align Sequences" button and get a result (the calculation of the score is not relevant).
Is there a way to make a Java program that will automatically insert the input, "click" the button and read the response from this website?
Thanks!
You can use the apache http client to send a request to a web site.
Look at the source to the page in question, and you'll find the part. This contains all the fields that need to be sent to the server. In particular, you'll see that it needs to be sent as a Post, rather than the more common Get. The link above shows you how to do a post with the http client code.
You'll need to provide a nameValuePair for every field in the form, such as these ones:
<input type="hidden" name="rm" value="lalign_x"/>
<input type="checkbox" name="show_ident" value="1" />
<textarea name="query" rows="6" cols="60">
It will probably take some trial and error for you to get all the fields set up correctly. I'd recommend doing this with small data sets. Once it all seems to be working, then try it with your bigger data.
In Python you can use mechanize library (http://wwwsearch.sourceforge.net/mechanize/). It's quite simple and you doesn't need to know Python very well to use it.
Simple example (filling login form):
br = Browser()
br.open(login_link)
br.select_form(name="login")
br["email"] = "email#server.com"
br["pass"] = "password"
br.submit()
You could probably do this using Selenium.
Related
I am using java liferay portal, in which there are multiple portlets. I want to create a portlet with a form that when it is submitted the data is retrieved and the specific result is shown in some other page portlet. But unfortunately these things are not going in the way.
I was thinking of using sessions but 2 problems arrised.
javascript value assignment to java variable.
if the values are passed to the page on which the specific portlet is placed, that portlet doesn't get the values.
Then I heard the concept of Inter Portlet Communication(IPC), and took some help from "liferay in action" but there the code works if both the portlets are placed on the same page, and my requirement is that one portlet is placed on first page and when the form is submitted it is redirected to the second page, to the second portlet for getting the parameters. I tried more example but its not working in my way.
I have found another way of, a relatively easiest, just tried that wiki from liferay
As i understood, you have some JavaScript parameters which you want to pass to the next page. You can, though, do it with APPLICATION_SCOPE of PortletSession and you can solve the problem of converting JS params to Java by placing the values in a input. If these input vars aren't supposed to be written by the user and you take them from somewhere else, you can make the input hidden:
In your jsp:
<form>
<input type="hidden" id="myinput1" name="in1" value="">
<input type="hidden" id="myinput1" name="in2" value="">
</form>
<script>
var a = "avalue";
var b = "bvalue";
document.getElementById("myinput1").value=a;
document.getElementById("myinput2").value=b;
</script>
Then submit the form when you need to. Next, you'll be able to do like this in the ProcessAction method of the portlet:
String a= request.getParameter("in1");
String b= request.getParameter("in2");
PortletSession session = request.getPortletSession();
session.setAttribute("a", a , PortletSession.APPLICATION_SCOPE);
session.setAttribute("b", b , PortletSession.APPLICATION_SCOPE);
In the other portlet, you can find it by calling
session.getAttribute("a",PortletSession.APPLICATION_SCOPE);
This, of course, if you can't simply place them in the next page's url.
As far as I know IPC does indeed only work between portlets on the same page. Also the portlet specification doesn't provide a generalized mechanism for switching pages so you can only use portal vendor specific ways to achieve that. But using public render parameters and a correctly constructed Liferay URL to another page you should be able to achieve the result you want: http://www.liferay.com/web/guest/community/forums/-/message_boards/message/1207858
The problem is pretty simple but I cant find a straight-forward solution online.
I have a document template, which I want to be able to populate with information (different information, more than once) and then print. My initial thought was to create a html template, and then send a parsed version to the print.
I suppose there is more than one question here:
What is the best approach to solve this problem?
If the HTML parsing is the best solution can someone point me in the correct direction as I dont know where to start.
Thanks.
-- EDIT: a better example to clear up any confusion.
On a java GUI program, the user is able to pull up a customers information. I wish them to be able to print an invoice at the click of a button. My current problem is deciding how to populate the invoice with the customers information and send it to the printer.
I would go for a web HTML form. Let the user populate the input fields and pass them in the POST body when a form submit button is pressed. Then you analyze the input and create the final document version (on the server) and only then return the document to the client with an appropriate MIME type in the response header (that's how you enable the printing).
For example (simple form with submit button)
<form name="input" action="html_form_action.asp" method="get">
Username: <input type="text" name="user">
<input type="submit" value="Submit">
</form>
EDIT:
For building document template take a look on playframework.
Is it possible to write a little Javaa program which parses a xml file from my web hosting site and updates this file? Or is there a better alternative to do so? I have to update the file every 10 min with about 10 lines of code each, so I don't want to write it out every time.
You can write little java program. BTW you can write a bigger one two :).
You can write program using any language you want. Including Java.
The program written in any language can parse XML.
Well, now we arrived to the problem. What do you mean when you say that you wish to parse XML from the web site? Does your web site provides URL that allows to download the XML? In this case you can download it (e.g. using HTTP GET method) and parse.
The next problem is how to update the XML on the site. You have to provide such functionality on site itself (e.g. implement service that is able to receive the XML and store it. For example via HTTP GET.
Once you are done you can write truly little java program that downloads the file using HTTP GET, parses it, creates new one and the sends it back to the site using HTTP POST.
I would investigate running this code on your server. What is the update? Does it use data not on your server? You can do this easily in Java but more detail is needed for a better answer.
OK, your idea is fine if your web hosting lets you do http PUT you can get the file using GET, modify it, e.g. using the DOM, and PUT it back. You might prefer to do more server side scripting and write the update at that end, it lets you use relational databases instead of flat files for example. In this case, writing a server side script to accept a POST as the other answer suggested is a good idea.
I've put a little example for you here: http://jcable.users.sourceforge.net/scores.php
There are three files on this website:
scores - a text file that gets updated.
scores.php - a script that shows the current score
add.php - a script that updates the current score.
scores.php looks like this:
<html>
<body>
The current score is <?php readfile("scores"); ?>
</body>
</html>
add.php looks like this:
<?php
if(isset($_POST["score"])) {
$fh = fopen("scores", 'w') or die("can't open file");
fwrite($fh, $_POST["score"]);
fclose($fh);
}
else
{
?>
<HTML>
<BODY>
<FORM action="add.php" method="POST">
New Score: <INPUT type="text" name="score"/>
</FORM>
</BODY>
</HTML>
<?php
}
?>
if you get add.php it will present you the form. But if your program calls it as a post it won't bother. Hope this gives you some ideas - its the simplest possible web app I can think of that has persistent server side data. You can add complexity - xml or json, etc., but the principles are there.
I am using Struts and Java. The problem is that I have a page with some results. The user can click an item and edit it. I want after editing the user to be able to return back to the results. Back isn't broken but if he submits the form for update he would have to click back 2 times I think and may have problem.
I have tried header("Referer") but this doesn't work in Internet Explorer.
I am trying to implement a solution. Any ideas? My idea is to save url and move around an ID of that url. And when I want to return back get the url from ID. Storing it in the session is not a solution because the user may have opened multiple windows.
Passing a URL as a request parameter may create security issues. Powerlord is right that the USER can alter the referrer header. This will allow the user to visit a page, something they can do anyway. More seriously, following a URL that is in a request parameter allows an attacker to send the user to a page of the attacker's choice, with the appearance that this page is recommended by your application. So the answer from BalusC can enable Cross-Site Request Forgery.
The best way is to pass it around as a request parameter. On the edit link or button, just pass the current URL along as request parameter. Here's an example with a link:
edit
Or if it's a button to submit a form, then rather pass it as hidden input value in the same form:
<input type="hidden" name="from" value="${pageContext.request.requestURI}">
In the page with the edit form, transfer it to the subsequent request as hidden input value of the form:
<input type="hidden" name="from" value="${param.from}">
In the action method, just redirect to that URL after finishing the action. Since I don't do Struts, I can't give a detailed Struts example, but here is how you would do it with "plain vanilla" Servlet, you must be able to port it to a Struts approach:
response.sendRedirect(request.getParameter("from"));
I'm playing with Stripes and I wonder if it is possible to send Javascript generated data to ActionBean. To be more specific, when I click with my mouse on certain element on page, I want to send ID of that element back to ActionBean after clicking on stripes:link. Providing I already have that ID saved in a JS variable id, how do I do that?
Are you using parameterised link?
<stripes:link id="mylink" href/beanclass="..." event="...">
<stripes:param name="id" value="some_default_value"/>
Click on me!
</stripes:link>
Which would most probably generate: http://mysite.com/...?id=some_default_value, which you would later use javascript to change some_default_value to the id you want?
Note: Suggestion unverified. I've no dev tool installed on this old lappie.
Edit: On second thought, why not just write some javascript to append "?id=" + id; to the link's url address?
create a hidden input element in your html
<input type="hidden" name="?" id="?" />
use javascript to set the value of it
document.getElementById("?").value = ??;
and the value will be posted with your form submission.