srikanth r
srikanth r

Reputation: 332

How to load an External Html file in servlet?

I want to load an Predefined Html file in my servlet code:-

this is my html file:-index.html

<html>
     <head>
     </head>
     <body>
         <p> This is the msg to be displayed on servlet </p>
     </body>
</html>

This is load page function:-

   function loadPage(href)
            {
                var xmlhttp = new XMLHttpRequest();
                xmlhttp.open("GET", href, false);
                xmlhttp.send();
                return xmlhttp.responseText;
            }
This is my servlet code:-



      out.println("<html><head>");
            out.println("<script>");
            out.println("$(document).ready(function(){");
            out.println("document.getElementById('bottom').innerHTML = loadpage('index.html'");
            out.println(" });");
            out.println("</script>");
            out.println("</head><body>");
            out.println( "<h1 id='bottom' ></h1>");
            out.println("</body></html>");

where i am trying to add index.html page

Can anyone help me to sort this issue, Thanks in advance

Upvotes: 0

Views: 1484

Answers (1)

ramp
ramp

Reputation: 1256

 out.println("document.getElementById('bottom').innerHTML = loadpage('index.html'");

So when the above lines are executed, you expect the 'index.html' page to load. Is that it ?

On a servlet this is just a String that is flushed to the browser. This html/script then executes on the browser.

You have at least two ways to achieve this

  1. On the browser - Make an ajax call that returns the html. Substitute the response as the innerHTML of your dom element.

  2. On the server - Use a request dispatcher to include the html. This, IMHO, is an easier option for your problem.

out.println("document.getElementById('bottom').innerHTML = '"); request.getRequestDispatcher("/index.html").include(request,response); out.println("';");

Upvotes: 1

Related Questions