Akshay Bijawe Akshay Bijawe - 6 months ago 12
HTML Question

Servlet doesn't refresh html page

This is the directory structure of my servlet:

+---build
| \---classes
+---Files
| esoutput.json
| newOutput.xml
| newoutputfinal.xml
| output.xml
|
+---src
| \---com
| \---example
| AddQueryTag.java
| ElasticsearchClient.java
| JsonToXml.java
| mainClass.java
| output.xml
| SaveJsonContent.java
| SaxonImplementation.java
|
+---target
| +---classes
| | +---com
| | | \---example
| | | AddQueryTag.class
| | | ElasticsearchClient.class
| | | JsonToXml.class
| | | mainClass.class
| | | output.xml
| | | SaveJsonContent.class
| | | SaxonImplementation.class
| | |
| | \---META-INF
| | | MANIFEST.MF
| | |
| | \---maven
| | \---com.example
| | \---PIBTest1
| | pom.properties
| | pom.xml
| |
| \---test-classes
\---WebContent
| firstPage.html
|
+---META-INF
| MANIFEST.MF
|
\---WEB-INF
| resultfile.html
| web.xml
|
+---lib
\---src


My main servlet class(mainClass.java) looks like this:

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

//Some processing code goes here
//My resultfile.html gets updated and finally I use Request Dispatcher to forward it

request.getServletContext().getRequestDispatcher("/WEB-INF/resultfile.html")
.forward(request, response);

}


This is a search application. User enters the query on “firstpage.html” and gets back the results.

When I first start my web app, “firstpage.html” is started as I configured it in the web.xml file. So when I first start my web app, it shows the “firstpage.html”, then User enters the query and the “resultfile.html” is created. But the web-app shows a blank page instead.
I checked the “resultfile.html” and the file is properly generated. So there’s no problem with my code. I want to be able to forward my generated “resultfile.html” page. Am I doing something wrong here? I’m new to Servlets. Thank you everyone.

I’m using Tomcat V7.0 and eclipse.

Update: When the user goes back to the "firstpage.html" and enters the new query, the "resultfile.html" shows the results for the old query. "resultfile.html" doesn't display the updated information.

Update 2: My "resultfile.html" is generated by SaxonImplementation class. The code for that class is as follows:

package com.example;

import java.io.File;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

public class SaxonImplementation {

public void simpleTransform(String sourcePath, String xsltPath,
String resultDir) {
System.setProperty("javax.xml.transform.TransformerFactory",
"net.sf.saxon.TransformerFactoryImpl");
TransformerFactory tFactory = TransformerFactory.newInstance();
try {
Transformer transformer = tFactory
.newTransformer(new StreamSource(new File(xsltPath)));
transformer.transform(new StreamSource(new File(sourcePath)),
new StreamResult(new File(resultDir)));
} catch (Exception e) {
e.printStackTrace();
}
}

}


I'm calling this class from my doGet method of mainClass.java to do the conversion of XML to HTML file.

Answer

I do not know why resultsfile is appearing as blank. There is the possibility of a server cache issue, but I don't know. For that I would suggest editing the file manually and/or viewing it manually. You need to determine whether the system is writing a blank file or just printing a blank page even though the file may be correct. Thanks for the update. Either the file is not fully written before you are sending it to the client, or there is a server cache issue as those files are not expected to change frequently. Please be sure that flush and close have completed on your FileWriter or FileOutputStream (that you are using to write to resultfile.html) before you call forward on the request. If you are sure that is already happening then it must be a cache issue. So I would suggest reading the resultfile manually and writing it to the response instead of forwarding.

That being said I think you need to change the design of your software to work more efficiently and multi-user safe. Writing to a shared resultsfile.html will not achieve multi-user safety.

I would write the results directly to the user. response has a function called getWriter which returns a Writer that you can use to send the HTML 'directly' to the client. (skip resultfile.html altogether)

Update: A google search for "StreamResult custom Writer" reveals this page which may be helpful: How to convert stream results to string Instead of new StreamResult(new File(resultDir)) you can use new StreamResult(writer) where writer is the result from response.getWriter()