-2

I have a few html files each of them containing the components of my future web page. I want them to be shown if user was looking for them. But how can I show many html files together in servlet response? As I know, the code below can redirect user to only one html file.

RequestDispatcher rd = request.getRequestDispatcher("/index.html");
rd.forward(request, response);
response.sendRedirect("/index.html");

I tried to use IO streams:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    List<String> content = new ArrayList<String>();
    BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("/header.html")));
    while ((str = in.readLine()) != null) {
        content.add(str);
    }
    PrintWriter out = response.getWriter();
    for (String item: content) {
        out.println(item);
    }
}

But it gives me only java.io.FileNotFoundException:header.html. Browser says: "HTTP Status 500 - Internal Server Error". How can I solve this? Should use another way?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Azamat Salamat
  • 57
  • 2
  • 10
  • 2
    Do you want to return several views in a single responde or want to trigger a download of the physical HTML files when the client fires the request? – Luiggi Mendoza Aug 05 '15 at 14:57
  • The only way to send back multiple pages would be to tar or zip them. Typically, the way web browsing works is that the first query that goes out downloads an HTML file which may have other dependencies (images, javascript etc) which are requested once the initial html file arrives – ControlAltDel Aug 05 '15 at 15:04
  • You could use rd.include, but you will have the head tag many times, html files will be concatenated. – Radu Toader Aug 05 '15 at 15:29
  • 1
    How exactly is "java.io.FileNotFoundException: header.html" unclear? Why exactly do you think that this specific problem is related to including multiple HTML files? – BalusC Aug 05 '15 at 15:52

1 Answers1

0

Your java.io.FileNotFoundException is because of this new FileInputStream("/header.html")

You need the path to the file.

If it were a JSP that was reading the file:

new FileInputStream(application.getRealPath("/")+"header.html")

For a servlet, its slightly more complicated because you can't use application and rather need a variable with a ServletContext stored in it:

private ServletContext context;

public void init(ServletConfig config) throws ServletException
{
    this.context = config.getServletContext();
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
{
   ....
   ....   new FileInputStream(context.getRealPath("/")+"header.html")
   ....
}
developerwjk
  • 8,619
  • 2
  • 17
  • 33
  • There's no need for a `FileInputStream` to obtain a web resource as an `InputStream`. Anytime you think to need `getRealPath()`, stop and ask yourself if there isn't a better way. See also http://stackoverflow.com/questions/12160639/what-does-servletcontext-getrealpath-mean-and-when-should-i-use-it – BalusC Aug 07 '15 at 20:32
  • @BalusC what makes that the case? I just tried `new BufferedReader(new FileReader("./WEB-INF/conf/config.xml"))` and `new BufferedReader(new FileReader("/WEB-INF/conf/config.xml"))` on a app I have and they don't work but `new BufferedReader(new FileReader(application.getRealPath("/")+"WEB-INF/conf/config.xml"))` does...so is this only to do with `FileInputStream` or its a Java version issue or what? – developerwjk Aug 07 '15 at 20:56
  • @BalusC The link does not answer the question I have – developerwjk Aug 07 '15 at 21:01