• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Writing a File and Request Dispatcher Forward

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 28193
95
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
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.
 
Stu Higgs
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Stu Higgs
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does anyone know how to do this?
 
Sheriff
Posts: 67746
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
Patience is a virtue.
 
Stu Higgs
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
While I am researching the solution here is more info for better information:

Servlet:
package servlets;

import dombuilders.*;//My package
import xmlxslttransformers.*;//My package

import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HtmlViewServlet extends HttpServlet {

ServletContext sc;
String sXmlFilePath = "xml/article.xml";//hard coded for dev purposes
String sXSLFilePath = "xsl/article.xsl";//hard coded for dev purposes

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

//this is the call to the class that performs the transforms.
new TXMLTransformer(sXmlFilePath, sXSLFilePath);

}

protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}


protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

}

Transformer class with exceptions and imports left out:

package xmlxslttransformers;

public class TXMLTransformer
{

public TXMLTransformer(String xmlFilePath, String xslFilePath){//explicit contructor
ToHtml(xmlFilePath, xslFilePath)
}

public void ToHtml(String xmlFilePath, String xslFilePath){

File stylesheet = new File(xslFilePath);
File datafile = new File(xmlFilePath);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
TransformerFactory tFactory = TransformerFactory.newInstance();
try{

DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(datafile);//'document' represents our DOM in memory which was created from the xml file

StreamSource stylesource = new StreamSource(stylesheet);
Transformer transformer = tFactory.newTransformer(stylesource);

DOMSource source = new DOMSource(document);

StreamResult result = new StreamResult(System.out);

transformer.transform(source, result);


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 ]
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you looked at the docs for theServletOutputStream

Here some sample code that uses it to write from a stream.
In this case the stream came from a file but I'm guessing it should be enough to get you going.

 
Stu Higgs
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

public StreamResult(OutputStream outputStream) {
setOutputStream(outputStream);
}

Thanks for your help.
 
Stu Higgs
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I have been at this for hours and maybe there are a lot of things I do not understand about Servlets. I am fairly new to trying to use them.

My first assumption is that they will behave like regular Java Classes.

For the problem above I came across a solution that works fine in a Stand Alone app where I am able to get the html from the transform method into a String via StringWriter.

1. StreamResult result = new StreamResult(new StringWriter());
2. transformer.transform(source, result);
3. htmlString = result.getWriter().toString();

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 ]
 
Stu Higgs
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Paul Clapham
Marshal
Posts: 28193
95
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
Like this:Don't write to a file and then immediately copy to the response.
 
Stu Higgs
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this example the code should be in the Servlet? Just want to make sure I understand.

transformer.transform(source, new StreamResult(response.getOutputStream()));
 
Paul Clapham
Marshal
Posts: 28193
95
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
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.
 
Stu Higgs
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

1. StreamResult result = new StreamResult(new StringWriter());
2. transformer.transform(source, result);
3. htmlString = result.getWriter().toString();

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.
reply
    Bookmark Topic Watch Topic
  • New Topic