错误! “文件过早结束”

时间:2017-03-05 14:38:43

标签: html xml jsp servlets xslt

我总共有3个文件如下

  1. data.xml中
  2. render.xslt
  3. last.xslt
  4. 见图片http://i.imgur.com/BUNtY3D.png

    我的第一个任务是将 data.xml render.xslt 作为输入,在此之后将输出为 All.xml 给定的servlet将处理 All.xml last.xslt 作为输入,这将为Http Request提供输出响应。

    import java.io.File;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.net.URL;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.xml.transform.Source;
    import javax.xml.transform.Transformer;
    import javax.xml.transform.TransformerFactory;
    import javax.xml.transform.stream.StreamResult;
    import javax.xml.transform.stream.StreamSource;
    
    /**
     *
     * @author Shubham Dalal
     */
    public class Manager extends HttpServlet {
        public void doGet(HttpServletRequest request,
            HttpServletResponse response)
        throws ServletException, IOException,
        java.net.MalformedURLException {
    
            // Tell the response object that our servlet returns HTML
            response.setContentType("text/html; charset=UTF-8");
            // Ask for a response stream
            PrintWriter output = response.getWriter();
            // This block tries to open our XML and XSLT files
            // which may throw an exception, thus the try..catch
            try {
                // Get a empty transform factory (factory pattern)
                TransformerFactory tFactory = TransformerFactory.newInstance();
    
                // Get the actual server path for XML and XSLT files
                String loc = getServletContext().getRealPath("") + "/";
    
                Source sdk2 = new StreamSource(
                    new URL("file", "", loc + "full.xml").openStream());
                Source Main = new StreamSource(
                    new URL("file", "", loc + "Main.xslt").openStream());
                // Generate the transformer
                Transformer Maintransformer = tFactory.newTransformer(Main);
                // GRAND FINALE!
                // Perform the transformation, send output to the browser
                Maintransformer.transform(sdk2, new StreamResult(output));
    
                // Get the XML input document and the stylesheet using
                // the string loc that holds the path information,
                // creating a JAXP Source instance
                Source sdk = new StreamSource(
                    new URL("file", "", loc + "sdk.xml").openStream());
                Source list = new StreamSource(
                    new URL("file", "", loc + "list.xsl").openStream());
                // Generate the transformer
                Transformer listtransformer = tFactory.newTransformer(list);
                listtransformer.transform(sdk, new StreamResult(new File("", loc + "full.xml")));
    
            } catch (Exception e) {
                output.write(e.getMessage());
                e.printStackTrace(output);
            }
            output.close();
        }
    }
    

    问题是我不知道如何在执行First任务的单个servlet中编写整个代码,然后处理第二个任务,即All.xml和last.xslt以提供html输出。

    我是servlet的新手。

    提前致谢。

0 个答案:

没有答案