Tuesday, 2 December 2014

Getting a 404 on a file running on a local server



This is a web based application, which is running locally on a glassfish server. I've got a Json file, in the same directory as an XSL file. I got script to take a node from the Json file and display it in the XSL file when a button is clicked.


When I hit the button, I get a 404 in the console saying that the Json file cannot be found. I get a 404 when I visit the file path in browser:



http://localhost:8080/MyProject/json_info.json


Yet when I visit the XSL file, it loads fine. So is there any reason that would stop the sever from finding the json file, which is in the same directory as all the other project files (that load fine).


Here is my servlet file, if this could be the issue?



public class MyServlet extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();

String path = getServletContext().getRealPath("/WEB-INF/");
String XSLFileName = path + "/Review.xsl";
File XslFile = new File(XSLFileName);

String URI = "xmldb:exist://localhost:8444/exist/xmlrpc";
String driver = "org.exist.xmldb.DatabaseImpl";

XMLResource res = null;
Node resNode = null;
Document doc = null;

try {
Class cl = Class.forName(driver);
Database database = (Database) cl.newInstance();
DatabaseManager.registerDatabase(database);

// get the collection
Collection col = DatabaseManager.getCollection(URI + "/db/Project", "admin", "password");

col.setProperty(OutputKeys.INDENT, "no");
res = (XMLResource)col.getResource("Review.xml");

resNode = res.getContentAsDOM();

doc = (Document) resNode;
}catch (Exception e) {
System.err.println("Error Document: "+e.getMessage());
}

DOMSource origDocSource = new DOMSource(doc);



try {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
StreamSource stylesheet = new StreamSource(XslFile);

Transformer transformer = transformerFactory.newTransformer(stylesheet);
transformer.transform(origDocSource, new StreamResult(out));


} catch (Exception e) {
System.err.println("Encountered Exception" + e.getMessage());
e.printStackTrace(out);
} finally {
out.close();
}
}

No comments:

Post a Comment