include external website inside div in jsp
I would like to include an external website (e.g. www.example.com) in a div in my jsp page. I don’t want to use frames since it’s not advisable.
I tried searching online for a solution, but all what I found was includi开发者_运维问答ng an internal webpage (e.g. webpage.jsp)…
I’ll really appreciate your help.
First of all, using frames is indeed not advisable if it concerns dividing/including local content (which is/was the most common abuse of frames). But it is definitely advisable if it concerns external content. For local content you should rather be using server-side includes such as <jsp:include>
.
As to the concrete question, if the HTML response of the external website does not collide with the HTML response of your own JSP page (i.e. it does not return a complete <html>
document which would make your final HTML response completely invalid because of duplicate/nested <html>
elements, but it returns some context-independent HTML fragment, e.g. <span>blah</span>
), then you can use JSTL <c:import>
for this.
<c:import url="http://external.com/some/fragment.html" />
But if it returns a complete <html>
document and/or is context-dependent, then you really have to use <iframe>
or to bring a proxy servlet in between. For a concrete kickoff example of such a servlet, check this answer: Make HttpURLConnection load web pages with images.
You're going to face several issues if you want to include an external page in a div
tag only, to name a few:
- handling links inside the external website
- cookie management
- resource management (images, css, javascript)
There are different options to handle requirements like this:
- iFrame; it's not nice but why isn't it advisable?
- server-side reverse proxy; may become complex as you have to filter external links (see #3)
- simple frame (somehow outdated)
- well-defined interface provided by the external system (e.g. some RESTful stuff)
To conclude: there is no easy way of providing an external page in a div
only.
精彩评论