0

I'm working on a desktop application using Swing. I have figured out how to log into a website using a POST request and receiving the HTML response. Now I need to send the received HTML to the default browser. Basically, displaying to the user what the browser is returning to me in the background. I have read a bit about set header, etc.

So my question is, how can I post html code from java into a browser in such a manner that it displays as a website and not just text?

I would love not to have to use any server (Socket) type code because I feel there is a compatibility risk if the program is run from different computers.

    // Log Into Website

    URL objtest = new URL("http://rccpdems01/ems/Login.php/Login.php");
    HttpURLConnection con = (HttpURLConnection) objtest.openConnection();
    con.setRequestMethod("POST"); 
    String datas = "userid=Gt737326&psword=Test&btnLogin=Login";
    con.setDoOutput(true);
    DataOutputStream wr = new DataOutputStream(con.getOutputStream());
    wr.writeBytes(datas);
    wr.flush();
    wr.close();

    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
    String inputLine;
    while ((inputLine = in.readLine()) != null) 
        System.out.println(inputLine);
    in.close();
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • Let me see if I understand: you make a POST request to a server and get the response in HTML. Now you want/need to send this HTML to an external browser? Where are you performing these actions? – Luiggi Mendoza Jun 20 '14 at 16:24
  • I actually want to send that HTML that I am receiving back to the browser that I got it from (So yes I guess an external browser), I am performing these actions in Java. – user3742362 Jun 20 '14 at 16:26
  • 1
    *I am performing these actions in Java* if it wasn't java then you won't tag this question with Java. Besides that, I mean what kind of application you're working with: console, standalone, web, mobile, a background process... You should explain the problem in programmer terms, not in end user terms. – Luiggi Mendoza Jun 20 '14 at 16:28
  • Do you have an application server like [JBoss](http://jboss.org/), or a Java web server like [Tomcat](http://tomcat.apache.org/) or [Jetty](http://www.eclipse.org/jetty/)? (that is not meant to be an exhaustive list, an exhaustive list would be off-topic anyway) – Elliott Frisch Jun 20 '14 at 16:28
  • Oh ok, it is a java application, I am coding in Netbeans and it is using a my sql database. – user3742362 Jun 20 '14 at 16:29
  • 1
    Uh... the IDE you use is **completely irrelevant** to the problem. Java programming language is not tied to an IDE. Again: which kind of application are you working with: a web application that can be accessed through a browser, a mobile application used on cellphones, a console application that is executed in a command line? Again: we are talking between programmers, not between an end user and a programmer. – Luiggi Mendoza Jun 20 '14 at 16:31
  • It is a desktop application on a PC. – user3742362 Jun 20 '14 at 16:33
  • 1
    Ok, we're talking about a standalone application. So your question is how to display the HTML you obtained from the external source in your desktop application, right? Another question: are you creating an applet or using swing? – Luiggi Mendoza Jun 20 '14 at 16:35
  • No I do not want the HTML code to display in my desktop app, I want it to display in the default browser or even better the browser that I got the HTML from. I am using swing. – user3742362 Jun 20 '14 at 16:37
  • @user3742362 You got the HTML in a browser how? Now I'm very confused... it's displaying as text where exactly? – Elliott Frisch Jun 20 '14 at 16:38
  • I got the HTML using a POST method, then I just looped the output. I will update my question with the code I already used. – user3742362 Jun 20 '14 at 16:40
  • So I know from looking at the HTML that the website/browser is returning that the website accepted my details but now I want to display that (The HTML code - which would form a webpage) to the user. – user3742362 Jun 20 '14 at 16:43
  • Woah. Where is the browser in that? You should probably read [Chapter 15 of the Jave EE tutorial - Servlets](http://docs.oracle.com/javaee/6/tutorial/doc/bnafd.html). – Elliott Frisch Jun 20 '14 at 16:46
  • @ElliottFrisch OP is working in a standalone application using swing. I guess there's a simpler way to open the default browser using `Runtime` and `Process` classes rather than embedding jetty or another server. – Luiggi Mendoza Jun 20 '14 at 16:48
  • Yes I know how to open the browser I just want to know how to send HTML to it in a way that it can understand that it should display the HTML as a webpage. – user3742362 Jun 20 '14 at 16:49
  • Review here: http://www.webdeveloper.com/forum/showthread.php?291577-java-program-to-open-a-web-page-in-browser-and-post-data-into-the-same-opened-page – Luiggi Mendoza Jun 20 '14 at 16:52
  • @user3742362 Create a temp file with an .html extension? Then use [Desktop.browse()](http://docs.oracle.com/javase/8/docs/api/java/awt/Desktop.html) to that path? – Elliott Frisch Jun 20 '14 at 16:57
  • Ok I do not have access to the website until Monday again, but I am creating a file and then opening it using Desktop.open and it is working. I am just wondering if the user is going to be able to use the webpage as it's URL might not be correct ? – user3742362 Jun 20 '14 at 17:44

1 Answers1

0

If it is a desktop program,I suppose you want to do this

Runtime runtime=RunTime.getRuntime();
 String path="www link or a local html file";
 String browserpath="path to your browser like c:/Program Files/firefox/firefox.exe";
 Process process=runtime.exec(browserpath + path);
 process.waitFor();
Pax
  • 32
  • 5
  • Ok I guess that could work, so I can create a file in the java read the HTML code into it and then sent that file to the browser ? – user3742362 Jun 20 '14 at 16:55
  • If you get the html by a buffered reader as I looked your edit,build a file via the buffered reader then try to use my answer,so yes. – Pax Jun 20 '14 at 17:01