1

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.

A Coder Gamer
  • 840
  • 1
  • 10
  • 31
  • @BalusC Hi BalusC, I was not getting a FileNotFoundException. I don't think it is a duplicate question. – A Coder Gamer May 10 '16 at 17:30
  • The question may be different, but the answer is definitely applicable on the current question. – BalusC May 10 '16 at 17:34
  • @BalusC So that means this is not a duplicate question and I already got a different answer for my question which is what I was looking for. – A Coder Gamer May 10 '16 at 17:57

1 Answers1

1

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()

Community
  • 1
  • 1
700 Software
  • 85,281
  • 83
  • 234
  • 341
  • Thanks for your reply. My web-app is in very initial stage as I'm new to Servlets. I'm gonna try to use the getWriter function as you suggested. Thank you for your time :). – A Coder Gamer May 09 '16 at 17:46
  • Please use the Check mark if you'd like to mark this questions as answered. You can always post a new question if a new question occurs to you. – 700 Software May 09 '16 at 17:48
  • 1
    Sure, I'm trying this solution. – A Coder Gamer May 09 '16 at 17:49
  • :-)​​​​​​​​​​​​[​](http://example.com) – 700 Software May 09 '16 at 17:52
  • My resultfile.html is generated from another file. One of the functions of my program converts the xml file to "resultfile.html". So I believe I cannot use the getWriter function of the response. How shall I proceed in that case? – A Coder Gamer May 09 '16 at 18:15
  • Your `doGet` appears to only read resultfile.html, and is not generating it before it reads. Where is the code that generates resultfile.html? Is this part of the same `GET` request or is a new request generated? If it is part of the same request, then you should be able to pass any `Writer` you wish in place of the `FileWriter` you are currently using. Look up "adding parameter to Java method" (or function) on Google and you can see how extras can be sent to other parts of your code. – 700 Software May 09 '16 at 18:20
  • Can you check my question again? I've updated it with more information. – A Coder Gamer May 09 '16 at 18:24
  • 1
    A google search for "StreamResult custom Writer" reveals this page which may be helpful: http://stackoverflow.com/questions/13217657/how-to-convert-stream-results-to-string#13217846 Instead of `new StreamResult(new File(resultDir))` you can use `new StreamResult(writer)` where `writer` is the result from `response.getWriter()` – 700 Software May 09 '16 at 18:25
  • Thanks a lot George. It worked. :) – A Coder Gamer May 09 '16 at 18:35