I am trying to write a file to a directory after executing an XSLT transform like so:
transformer.transform(source, new StreamResult(new File("\\html\\article.html")));
I have written files before from within a Web App to a directory int a Web App in the same manner outside of the transform method and it works without a problem. For some reason the file is not being written to the directory from the web application but when I run the same code in Stand Alone App it does write the file to a local directory.
What I am trying to accomplish is a tranform from XML to html, write the file to a directory, and the use RequestDispatcher forward froma servlet to display the html. If anyone know how to do this or knows what I am doing wrong, thanks in advance for your help.
Are you planning to write the HTML to a file, then immediately forward to the HTML you just generated? If so, you should just send the HTML directly to the response's output stream. You don't need an intermediate file in this case. And if you produce one, you are running the risk of two simultaneous requests competing to produce the same file.
Or if you are producing an HTML file that's supposed to be part of your web application in the future, and some other request is going to forward to it, then you need to write it to a directory inside your web application. That example writes to an "html" directory in the root directory of some drive.
Joined: Jan 01, 2006
you should just send the HTML directly to the response's output stream
I think this is most appropriate for what I am doing. Would you happen to have an example for reference? Thanks.
Please note that the transformation takes place outside of the servlet in another class. Not sure if that matters or not here.
[ January 23, 2006: Message edited by: Stu Higgs ] [ January 23, 2006: Message edited by: Stu Higgs ]
My goal is to get the html output into the servlet and then displayed in the broswer when this servlet is called. In the stand alone app using System.out for result, the html prints out. My question is how do i get the html to print out to the browser. Thanks, [ January 23, 2006: Message edited by: Stu Higgs ]
I looked at the ServletOutputStream docs and source file but still don't understand how to take the results from the transformer into the servlet because all the methods in the Transformer class return void and the class is abstract. Not really sure how to deal with that, but I am trying to decipher how to use the code example. I dont think I fully understand the StreamResult class either, but i am looking at this method:
At line three the member/instance variable is set.
From another class called TUser I am creating an instance of the class I use to perform the transform called TTransformer and then am able to access that member variable from TTransformer as would be expected after it is set.
TTransformer tt = new TTransformer(sXmlFilePath, sXslFilePath); System.out.println(tt.htmlString);
No problem here until I try to replace my TUser with a servlet which results in htmlString printing out as null.
response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); TXMLTransformer tt = new TXMLTransformer(sXmlFilePath, sXSLFilePath); out.println("TEST..."+tt.htmlString);
Do Servlets work like any other Java Class? Why would this variable print out as null from the servlet? Obviously I am missing something.
I still have not attempted the example provided by Ben Souther because I am experimenting, reading, and trying many different things so I can have a good grasp on this and while doing so am coming across additional questions. I have no clue why that variable prints as null in the Servlet and not null in the Stand Alone? [ January 24, 2006: Message edited by: Stu Higgs ]
Joined: Jan 01, 2006
Problem is solved. I was not specifying the correct path to the xml and xsl documents in the Servlet. I'm using netbeans and it configures the project for you, creating the project directory structure outside of the server so this was confusing. I'm also running this on Linux which i am brand new to as well in addition to Netbeans. So thanks all for your input on this and sorry to have made a ruckus over something as silly as a path to a file. Thanks.
Does that code have to be in your servlet? Well, you'll notice that as written it needs a reference to the "response" parameter of your doGet() or doPost() method. So if it's in another class, you would have to pass "response" as a parameter to whatever method of that other class does the transformation.
But a better design than that would be to pass the response's output stream as a parameter. Then the other class doesn't have to know anything about servlets (specific subject matter), it only has to know about output streams (general subject matter).
So your other class might have a method like this:And your servlet's doGet() method might contain code like this:I have left off most of the details and only included the basics. How you pass parameters between classes is more of a basic Java question.
Joined: Jan 01, 2006
yes, thats exactly why I asked because then the separate transofmer class would have to know about response. Right now i have it working like this:
where htmlString is a memebr variable in the separate TTranformer class that does the transformation. Then the servlet accesses that member variable from the TTransformer class like this:
response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); out.println(new TXMLTransformer(sXmlFilePath, sXSLFilePath).htmlString); out.close();
That works. The real kick in the rear at this point is that I am not able to read the sXmlFilePath and sXSLFilePathfile from within a directory in the web app, but rather I am only able to read them from a local directory off of / on my hard drive. I'm using Netbeans and i really am not comfortable with it yet. I am going to recreate the Web App in Tomcat and create all directory strucutres myself to see if that helps with reading the file from within a directory in the web app. Also am going to see if I can implement your suggestions. Thanks.