0
我在jsp頁面中選擇了xml文件,然後在servlet的post方法中轉換這些文件並在html頁面上顯示數據。但它不工作,當我選擇一個XML文件,並驗證我得到下面的錯誤錯誤404 - 在Java中將XML轉換爲HTML時未找到
"Error 404 - Not Found".
下面是java
代碼:
@WebServlet(name = "Validation", urlPatterns = { "/validation" })
public class Validation extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=windows-1252";
static Document document;
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
String xmlfile=request.getParameter("fichier");
String xsltfile="C:\\Documents and Settings\\Administrateur\\Bureau\\test\\EDI3\\EDI";
File stylesheet = new File(xmlfile);
File datafile = new File(xsltfile);
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse(datafile);
// Use a Transformer for output
TransformerFactory tFactory = TransformerFactory.newInstance();
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);
} catch (TransformerConfigurationException tce) {
// Error generated by the parser
System.out.println("\n** Transformer Factory error");
System.out.println(" " + tce.getMessage());
// Use the contained exception, if any
Throwable x = tce;
if (tce.getException() != null) {
x = tce.getException();
}
x.printStackTrace();
} catch (TransformerException te) {
// Error generated by the parser
System.out.println("\n** Transformation error");
System.out.println(" " + te.getMessage());
// Use the contained exception, if any
Throwable x = te;
if (te.getException() != null) {
x = te.getException();
}
x.printStackTrace();
} catch (SAXException sxe) {
// Error generated by this application
// (or a parser-initialization error)
Exception x = sxe;
if (sxe.getException() != null) {
x = sxe.getException();
}
x.printStackTrace();
} catch (ParserConfigurationException pce) {
// Parser with specified options can't be built
pce.printStackTrace();
} catch (IOException ioe) {
// I/O error
ioe.printStackTrace();
}
}
}
任何指導或教程,可以讓我明白的地方我會錯誤的讚賞。
我沒有開發servlet,但我期望輸出被寫入某個附加到'HttpServletResponse響應'的流,而不是'System.out'。所以試試'StreamResult result = new StreamResult(response.getOutputStream());'。通常,XSLT使用命名空間處理XML,因此您可能需要使用可識別名稱空間的DocumentBuilderFactory。另一方面,XSLT處理器的實現者通常會說他們的本地樹模型比W3C DOM好,所以不要使用DOMSource考慮使用SAXSource或StreamSource。 – 2014-11-04 14:43:52