I have to embed an HTML file inside a jsp . This HTML file is dynamic and has to be downloaded based on the user request. What I tried was to download the html in a directory and then display it from the jsp.
For that one of the methods I tried was something like this:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
String externalWeb = "external";
String externalWebValue = request.getParameter(externalWeb);
_saveUrl(externalWebValue);
StringBuilder contentBuilder = new StringBuilder();
try {
BufferedReader in = new BufferedReader(new FileReader("/pathToExternal/external.html"));
String str;
while ((str = in.readLine()) != null) {
contentBuilder.append(str);
}
in.close();
} catch (IOException e) {
}
String content = contentBuilder.toString();
String page = content;
request.setAttribute("page", page);
request.getRequestDispatcher("/web/external.jsp").forward(request, response);
}
private void _saveUrl(String externalWebValue) {
try {
PrintWriter outputFile = new PrintWriter("pathTo/external.html");
URL url = new URL(externalWebValue);
URLConnection con = url.openConnection();
InputStream is =con.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
outputFile.println(line);
}
outputFile.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
I tried to read directly from the webpage. But same problem , the javascripts don't work and the page content is not loading up.
But when I just click the downloaded html everything is working as it should, but when I am importing it in jsp nothing works. How can I fix this?
Tried to include it in the jsp like this:
<%@ include file="/web/external.html" %>
Still no luck. What is the best way to do this without using iframe? (iframe is not supported by the websites that I plan to render)
The solution to the above problem was adding this in the jsp:
<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>
<html>
<head>
<title> Tag Example</title>
</head>
<body>
<c:import var = "data" url = "${page}"/>
<c:out value = "${data}" escapeXml="false"/>
</body>
</html>
Added this in the servlet:
request.setAttribute("page", externalWebValue);
Used the c tag library
and had to use escapeXml="false"
to make this work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.