wood burning stoves 2.0*
The moose likes XML and Related Technologies and the fly likes Passing HTML string/stream to Browser Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Passing HTML string/stream to Browser" Watch "Passing HTML string/stream to Browser" New topic
Author

Passing HTML string/stream to Browser

Chris Olson Jr
Greenhorn

Joined: Feb 04, 2009
Posts: 8
I'm able to transform the xml into html using xsl and have a perfect html string to show for it. But I'm not able to make the leap of passing the html to a browser opened with this command "String[] commands = { "cmd", "/c", "start", "iexplore.exe", "-nohome" };" in my java class.

Any suggestions? I'm fairly new to xml and xsl and very stuck right now.

Thanks for any help!!

-Chris
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18136
    
    8

This "html string": is it a Java string object? Because if it is, you can hardly expect a browser to reach into your code and extract that string. Likewise I don't think you can put the whole HTML string on the command line (you're using the command line there, essentially).

Write the string to a file and have IE display that file, would be the best approach.
Chris Olson Jr
Greenhorn

Joined: Feb 04, 2009
Posts: 8
I'd love to write it to a file and use that - but company policy is that we don't put a file on the user's computer for security reasons. I've been able to get that to work for me (so I could test formatting and such) - but know I can't implement it.

I can make it a string or a stream - I just wasn't sure if there was a way to pass either to a browser. I guess that was my first question - is it possible? Sounds like the answer is "no".

I have an xsl file and my generated xml. I can merge the two while in my java class. Is there a way to merge them on the jsp?

If it helps - my goal is: the user clicks a print button on a jsp, a new window pops open and the new window displays the print friendly info. I can't do all of it client side because I need to grab an entire data object (which has more information than the presentation object on the initial jsp).

Thanks again for your help!
Chris Olson Jr
Greenhorn

Joined: Feb 04, 2009
Posts: 8
a small addition/clarification....the generated xml is in a Document placed in a DOMSource.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39549
    
  27
If all you intend to do is to show a web page to a user, why don't you open a new window that loads the URL of that page? All the processing can thus happen on the server.

I'm not quite quite clear on where the Java code runs - you mention a JSP, but the thing about "commands" and "iexplorer" seems to indicate it's on the client?


Ping & DNS - updated with new look and Ping home screen widget
Chris Olson Jr
Greenhorn

Joined: Feb 04, 2009
Posts: 8
I use the command line code in my java servlet to open a new browser.

I "mastered" putting my html in a response object and passing the response to my existing page. If I could pass a response object to my new browser page, I'd be golden. But I can't find a way to tie the response object and new browser page together.

Mostly I'm beating my head against my desk right now and have been searching for a solution or alternative solutions. That searching is what led me to maybe transforming my generated xml and xsl file on the new page. I currently do the transform in my java class and ran across an article that mentioned passing a DOM to the page. So I thought I might have headed down the wrong path and needed to pass the DOM instead.

Sorry for any confusion....I'm starting to think a more labor intensive job may be better suited for me.

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18136
    
    8

Chris Olson Jr wrote:I use the command line code in my java servlet to open a new browser.




What on earth is that for? If it does anything at all it will open the browser on the server machine. That's no use at all to the client who submitted the request which ran your servlet.

If you're transforming XML in your servlet, the natural thing is just to return the result of the transformation as your response to the request. That's all. But it appears from your post that you already tried that and didn't get satisfactory results? So can we start from that point in the problem-solving chain?
Chris Olson Jr
Greenhorn

Joined: Feb 04, 2009
Posts: 8
Cool - you answered one of my confusions. Progress is a nice change

Within my servlet, I generate the xml and using Transformer, I merge the xml with my xsl and put it in the response.

response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println(build.transformToHTML());


protected String transformToHTML() {
StringWriter sw = new StringWriter();

StreamResult result = new StreamResult(sw);

StreamSource streamSource = new StreamSource(xslFilePath);

TransformerFactory transFact = TransformerFactory.newInstance();
Transformer transform = transFact.newTransformer(streamSource);

transform.transform(source, result);

return sw.toString();
}

Then I forward to the page I currently have open and everything displays nicely. My problem is that I need the current page to stay displaying as it is and a new window to pop open showing the html created by the above process.

Does that help? And thanks again!

-Chris

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18136
    
    8

Chris Olson Jr wrote:My problem is that I need the current page to stay displaying as it is and a new window to pop open showing the html created by the above process.


Well, this is a common question. Nothing to do with XML, though. All your server can do is to respond to the request, and the requester (aka the client's browser) will display it wherever it decides to do that. If you want it to open a new window, then the HTML which makes the request has to specify a new window. The server can't make that decision.

I don't recall off hand just how you do that, but it's a pretty common thing to do in HTML. I'm sure you can find out how to do that quite easily.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39549
    
  27
This would open a new window when clicked:

<a href="URL of the page goes here" target="new">click me</a>
Carey Evans
Ranch Hand

Joined: May 27, 2008
Posts: 225

Ulf Dittmer wrote:<a href="URL of the page goes here" target="new">click me</a>

That will open a new window named "new", or use an existing window or frame named "new", which might be want you want if you're going to reuse the same window for subsequent popups. You're more likely to want to use target="_blank", though.
Chris Olson Jr
Greenhorn

Joined: Feb 04, 2009
Posts: 8
I was able to build a command line statement that opens a new browser window and it uses the _blank as part of the command. The line I use in java looks like this: "String[] commands = { "cmd", "/c", "start", "iexplore.exe", "-nohome" };" The next line is commands.execute();.

My problem is that I don't know the best way to go to get that new window to display my print-friendly html. I need to know if I am able to build the html and pass it to the new window (maybe through the response object) or if there is another way to pass the DOM from my old window to my new window and have it transform with the XML inside the new window.

I'm able to build the XML, so it isn't so much my concern. One problem is that I don't know where or how to do the transform so that the information gets displayed on the new screen.

If I pass it in some sort of response or request object - I don't know how to control the response object so that it goes to the new window and not the old window. Kind of like pulling an object from the session or request in html - I just don't know how to get the object into the new window.

I hope I'm explaining my problem better....thanks for the patience and help!!

-Chris

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18136
    
    8

I don't think the problem is that you aren't explaining your problem well. I think the problem is that you have suddenly decided to ignore all the answers given over the last ten hours. Let me try to summarize those answers:

(1) Don't bother with trying to start a browser. That would just run on the server and be useless. So don't do it.

(2) Send the HTML you produced directly to the response object in your servlet.

(3) Your server can't control where the output goes. The client controls that. So if you want a new window, the client has to ask for one using a certain HTML attribute.

Do those answers have nothing to do with your problem?
Chris Olson Jr
Greenhorn

Joined: Feb 04, 2009
Posts: 8
I'm not ignoring anything you or the others have told me. I think I've looked up so much stuff in too many places and tried it all with no luck - so I'm getting stuff confused.

(1) I had "don't open a browser" confused with, "don't use a seperate window to display the print friendly html".

(2) I can do the response object - no problem

(3) To clarify - is your direction that when I get the response object in the current screen, I pass it to the new screen using JavaScript? I can open a new window using JavaScript - but hadn't tried passing the html all the way through.

I'm going to try to get these changes added to my code tomorrow. Thanks again for all of your comments and help!!

-Chris
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18136
    
    8

(3) No. On the client, when you send the request for the "print friendly HTML", have that request ask for a new window. We've already been through how to do that earlier in this thread. Remember, it's the client which decides where the response is going to show up. Then, in the server, just send the print friendly HTML to the response. Remember, the server has no control over where its response is going to show up, so no Javascript or other trickery is needed here.
Chris Olson Jr
Greenhorn

Joined: Feb 04, 2009
Posts: 8
I think I've got it - THANK YOU for walking me through this and being so patient.

-Chris
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing HTML string/stream to Browser
 
Similar Threads
pattern suggestions?
GUI editor for xsl, xsl-fo,...
XSL for FindBugs
Displaying XML data through a servlet using XSL
using .xml and .xsl file in Servlets/jsp files to generate .html content (to browser)