I have an index.html
file in my local disk file system on c:\report\index.html
. I need to display this file in web application's JSP page in an <iframe>
.
How can I achieve this?
Isn't it simple ?
<IFRAME SRC="D:\\lib\\hello.html" width="400" height="200">
<!-- Alternate content for non-supporting browsers -->
</IFRAME>
Create a servlet which gets an InputStream
of it with help of FileInputStream
and writes it to the OutputStream
of the response after having set the proper content type header (so that the browser understands how to deal with it). Finally just specify the URL of the servlet in the <iframe src>
.
E.g.
@WebServlet("/reportServlet")
public class ReportServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
Inputstream input = new FileInputStream("c:/report/index.html");
OutputStream output = response.getOutputStream();
// Write input to output the usual way.
}
}
with
<iframe src="${pageContext.request.contextPath}/reportServlet" ...></iframe>
An alternative is to map c:/report
as a new webapp context in your server configuration so that you can just access it directly by http://example.com/report/index.html.
<iframe src="/report/index.html" ...></iframe>