The browser tries to resolve these resources relative to the current request URI (as you see in the address bar of the browser). These resources, of course, do not exist in your public web content, as you seem to have put them in the classpath.
To solve this problem, you really need to parse the HTML and change all src and / or href attributes for the domain <a> , <img> , <base> , <link> , <script> , <iframe> , etc. so that they point to a servlet that passes these resources from the path to the HTTP response.
This works a bit, but Jsoup makes things easier. Here is an example that assumes your servlet is displayed on the URL /proxy/* pattern.
String proxyURL = request.getContextPath() + "/proxy/"; InputStream input = MyServlet.class.getResourceAsStream("/a/b/resources" + request.getPathInfo()); if (request.getRequestURI().endsWith(".html")) { // A HTML page is been requested. Document document = Jsoup.parse(input, "UTF-8", null); for (Element element : document.select("[href]")) { element.attr("href", proxyURL + element.attr("href")); } for (Element element : document.select("[src]")) { element.attr("src", proxyURL + element.attr("src")); } response.setContentType("text/html;charset=UTF-8"); response.setCharacterEncoding("UTF-8"); response.getWriter().write(document.html()); } else { // Other resources like images, etc which have been proxied to this servlet. response.setContentType(getServletContext().getMimeType(request.getPathInfo())); OutputStream output = response.getOutputStream(); byte[] buffer = new byte[8192]; for (int length = 0; (length = input.read(buffer)) > 0;) { output.write(buffer, 0, length); } } input.close();
Open it http: // yourdomain: yourport / contextname / proxy / test.html .
source share