我必須在 jsp 中嵌入一個 HTML 文件。此 HTML 文件是動態的,必須根據用戶請求進行下載。我嘗試的是將html下載到一個目錄中,然后從jsp中顯示它。為此,我嘗試過的其中一種方法是這樣的: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(); } }我試圖直接從網頁上閱讀。但同樣的問題,javascripts 不起作用,頁面內容沒有加載。但是,當我單擊下載的 html 時,一切正常,但是當我在 jsp 中導入它時,卻沒有任何效果。我怎樣才能解決這個問題?試圖像這樣將它包含在jsp中:<%@ include file="/web/external.html" %>仍然沒有運氣。在不使用 iframe 的情況下執行此操作的最佳方法是什么?(我打算渲染的網站不支持 iframe)
如何從jsp顯示包含javascript的外部網站?
幕布斯6054654
2021-08-04 10:06:36