Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

java program to open a web page in browser and post data into the same opened page

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i m stuck into a situation. The problem is that i want to write a java program/function which opens a web page in a default browser and post some data into that same opened web page.


Please could anyone guide me to do this. I dont have a clue in this.

Your help is much appreciated. Thanks in advance
 
Ranch Hand
Posts: 426
Eclipse IDE Fedora Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try exec() .
 
biraj joshi
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
exec() only opens webpage in browser but i want to post data into that opened page from getRuntime.exec() function
 
Roger Sterling
Ranch Hand
Posts: 426
Eclipse IDE Fedora Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

biraj joshi wrote:exec() only opens webpage in browser but i want to post data into that opened page from getRuntime.exec() function



Most browsers have arguments you can pass in as parameters on the exec (for example, the http address you want the browser to go to). If you actually want to manipulate the GUI on-screen, thats a whole different ball game that requires advanced knowledge. In essence, you need to get the GDI instance, search for the pattern of pixels you want to use as a reference, then position the cursor over those pixels, and post WMI messages into the WMI queue for the characters you want to appear.

 
Rancher
Posts: 43027
76
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you actually have to open a browser? If you just want to post the form, using a library like HtmlUnit would seem a much better approach.
 
Marshal
Posts: 75637
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
… and welcome to the Ranch
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

biraj joshi wrote:i m stuck into a situation. The problem is that i want to write a java program/function which opens a web page in a default browser and post some data into that same opened web page.


But do you? What you're describing is how you want to solve the problem, when you haven't actually told us WHAT the problem is.

And at first glance, Ulf's suggestion would seem like a very sensible (and much simpler) alternative.

Winston
 
biraj joshi
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the problem is with getRuntime.exec(url) as it only opens the webpage in browser but doesnot allows to post the data(not a query string attach with url) in the same webpage..I also tried httpclient post method which allow me to post the data into another page but it doesnot opens the same page in browser.

I just want to open a webpage in browser.In that webpage i have some form hidden text fields and i want to post the data into that hidden fields through post method.In other words whenever the webpage is opened i want those hidden fields to be filled with post data.
 
Roger Sterling
Ranch Hand
Posts: 426
Eclipse IDE Fedora Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

biraj joshi wrote:the problem is with getRuntime.exec(url) as it only opens the webpage in browser but doesnot allows to post the data(not a query string attach with url) in the same webpage..I also tried httpclient post method which allow me to post the data into another page but it doesnot opens the same page in browser.

I just want to open a webpage in browser.In that webpage i have some form hidden text fields and i want to post the data into that hidden fields through post method.In other words whenever the webpage is opened i want those hidden fields to be filled with post data.



You may have failed to encode the query string. And you need to pass the parameters, not the command line.

http://stackoverflow.com/questions/7134486/how-to-execute-command-with-parameters
 
Ulf Dittmer
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To me, it doesn't sound like you need to open a browser to do that; am I missing something? What do you think of the approach I suggested in my previous post?
 
biraj joshi
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
opening the webpage is the main part.Your previous post only post the data in the page but doesnot invoke that page in browser.
 
Ulf Dittmer
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you mean by "invoke"? Does it mean submitting the form using some specified set of data is not the main point? If so, then what is the main point?
 
biraj joshi
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
opening the webpage and posting data into the page..these both are main parts.how to achieve this
 
Ulf Dittmer
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand "posting data into the page". What is the point of "opening the webpage"? Will the human user actually do something with that page?
 
biraj joshi
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes..that opened webpage has form which is design to do some task..user fill up the form and submit for further process
 
biraj joshi
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the below codes opens the browser and send data through get method to the opened webpage..

String url = "http://localhost:9999/testing/index.jsp?msg=biraj";
String cmd = "cmd.exe /c start " + url;
Runtime.getRuntime().exec(cmd);

i have attached query string in the url..but what i really want to do is post the data using post method and open the browser(http://localhost:9999/testing/index.jsp) to do further process
 
biraj joshi
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks..the firefoxdriver opens the browser..it needs the jar selenium 2.39.0
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

biraj joshi wrote:Thanks..the firefoxdriver opens the browser..it needs the jar selenium 2.39.0


Glad you got it sorted, but I still suspect you might be overengineering this.

There are several standard frameworks for dynamic web page content, but I actually wonder if this business of writing a page on the fly and then running it isn't likely to run you into a whole pile of problems later on. It sounds like a form of "reflective" HTML, which just seems plain nasty to me.

Unfortunately, since you haven't told us WHAT the problem is you're trying to solve (you've told us how you want to solve it), it's very difficult to advise.

Winston
 
biraj joshi
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the problem was i could not able to open a webpage(php page) in a browser from my java program
 
Marshal
Posts: 27211
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But that's not a problem... not unless there's some reason why you need to do that. That reason would of course be because of some problem, the one you still haven't told us about.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am having the same problem. I want to open a webpage from within my java client, and send that webpage some HTTP POST parameters (not GET as I don't want the parameters to be in the address bar - I want them to be hidden). The reason I want to open the actual web page is because the user needs to edit a form on that page (prepopulated with some of the POST parameters sent from the java client).

I've looked at your responses but I can't see an answer to the HTTP POST part.

Here is my code (works fine):-


This successfully opens the website and passes the GET parameters listed in the URL address. That's not what I want. I want to send this webpage HTTP POST parameters.

Does anyone know how to do this?
 
Ulf Dittmer
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If this was my problem, instead of looking for a Java solution, I'd investigate the platform scripting support -maybe WSH for Windows- and check if driving the browser via its COM model (or whatever it's called these days) is possible.
 
Clare McLennan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That all sounds very complicated for something that is quite straightforward. Are you really saying there is no way within a java client to open a URL in Internet Explorer and pass it some HTTP POST parameters?
 
Ulf Dittmer
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sometimes the things that are easy to describe are hard to do :-) Mixing Java code and native code is anything but straightforward. There may be Java-based tools that can do this (you might want to look into Selenium, which can be used to drive a browser from within Java code, but be prepared for a learning curve), but fundamentally it's not a good fit.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Clare McLennan wrote:That all sounds very complicated for something that is quite straightforward. Are you really saying there is no way within a java client to open a URL in Internet Explorer and pass it some HTTP POST parameters?


And just to add to what Ulf said, what both you and biraj are describing sounds to me like a solution, not a requirement - ie, you're not telling us what you want, but how you want to do it.

If I get it right, you want some way of supplying dynamic content to a webpage without using query strings. Does that about cover it? Because, as I said above, there are quite a few ways of doing that with servlets from what I understand.

Just don't ask me to quote chapter and verse, because I'm no expert.

Winston
 
Clare McLennan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Winston

You are half-correct in that I was trying to define the solution because the website had already been written, the front page was requiring an HTTP POST to stop the parameters being shown in the address bar. Therefore I was limited in what I could do. But I have come to the conclusion that it is simply not possible, in one call from a java client, to open the IE browser, get it to browse to a specific URL and pass it hidden HTTP POST parameters.

Therefore after faffing around for a while on this subject, my conclusion is that it is going to be quicker to add to the website as follows:-

1. create a servlet, and get my java client to do an HTTPPOST to that servlet (i.e. not opening the IE browser) with all the parameters the front page needs.

2. get the servlet to retain all POST parameters and respond to my java client with a Session ID

3. java client then uses Session ID to open the web front page (the original page I wanted) with Internet Explorer using the Runtime.getRuntime().exec command. The front page can get the necessary parameters from the session rather than the client.

Which is a pity as I don't really need sessions and I wanted to do all this with one clean call from client to webpage. It ain't going to happen.

If anyone has any better ideas, do let me know!

p.s. in answer to your question: "supplying dynamic content to a webpage without using query strings"....That's only half of what I want. I also want to open the Web Browser AT THE SAME TIME. It is this last part which is making all this so complicated.
 
Sheriff
Posts: 67634
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Clare McLennan wrote:If anyone has any better ideas, do let me know!


My better idea is stop the following silliness:

the front page was requiring an HTTP POST to stop the parameters being shown in the address bar.


Using a POST where a GET is required sounds like its at the heart of your problem. Why this silliness? If someone felt that "hiding" the parameters with a POST was a measure of security, well, that's a big old fail.

What's the reason for the POST? It seems to be at the heart of all the complications.
 
Clare McLennan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some of the POST parameters are user ids and passwords :-)

Yes, yes I know - but this is an internal website - i.e. both client and website are within the firewall. There is not too much concern about someone sniffing users ids and passwords inside the firewall BUT there IS concern about users seeing user ids and passwords in the address bar of the website.

And even if we take out the user ids and passwords and only put non-sensitive stuff in the HTTP GET/POST - our client STILL doesn't want anything in the address bar.

 
Bear Bibeault
Sheriff
Posts: 67634
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Clare McLennan wrote:Some of the POST parameters are user ids and passwords :-)


My point exactly -- putting them in a POST offer no security. None. Whatsoever.

BUT there IS concern about users seeing user ids and passwords in the address bar of the website.


With a few clicks or keystrokes, the values are just as visible as if there were on the address bar.

Your security is an illusion. And now you are doing handsprings and contortions to get around limitations brought on by a mechanism that offers only the illusion of security. Perhaps it's time for someone to step back and reassess the approach?

My advice: use BASIC HTTP authentication. No usernames or passwords required on the URL or in the POST body. (The credentials are encoded into the headers. Still not 100% secure from local prying eyes -- but a far sight better than what you are doing.)
 
Clare McLennan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK I'll take a look but I will have to persuade client that something should be in address bar.

Thanks
 
Ulf Dittmer
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I advise against anything that puts credentials into the URL, just in case that's one of the options. Those can end up in far more places than POST parameters (caches, logs, bookmarks etc.). If security is a for-real concern, in addition to authentication, you should also use an encrypted connection (i.e., HTTPS). Credentials should not be sent through an unencrypted connection.
 
Bear Bibeault
Sheriff
Posts: 67634
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Clare McLennan wrote:OK I'll take a look but I will have to persuade client that something should be in address bar.


My point is that by using BASIC authentication there's no need for credentials on the URL.

I don't think that it will take much effort to convince the client to use real security over the faux security that is in place now.
 
Bear Bibeault
Sheriff
Posts: 67634
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote: you should also use an encrypted connection (i.e., HTTPS). Credentials should not be sent through an unencrypted connection.


I always assume that that's given -- but sadly, it is not.
 
It will give me the powers of the gods. Not bad for a tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic