We are hosting a site for a client and they want us to include the header they have on their server into the pages we are hosting. So whenever they change it, it will automatically change on our site.
我们正在为客户托管一个网站,他们希望我们将他们在服务器上的标头包含在我们托管的网页中。因此,无论何时更改,它都会在我们的网站上自动更改。
We are attempting to use the "include" tag in our JSP code. The code we are using is as follows:
我们正在尝试在JSP代码中使用“include”标记。我们使用的代码如下:
<%@ include file="www.CLIENT.com/CLIENT2/MiddlePageFiles/Vendor_header.html" %> .
<%@ include file =“www.CLIENT.com/CLIENT2/MiddlePageFiles/Vendor_header.html”%>。
We also tried
我们也试过了
<%@ include file="**http://**www.CLIENT.com/CLIENT2/MiddlePageFiles/Vendor_header.html" %> .
<%@ include file =“** http://**www.CLIENT.com/CLIENT2/MiddlePageFiles/Vendor_header.html”%>。
Unfortunately these aren't working for us. What seems to be happening is that the code is ONLY looking locally for this file and never seems to go "outside" to look for it.
不幸的是,这些并不适合我们。似乎正在发生的事情是代码只在本地查找此文件,似乎永远不会“外部”寻找它。
We are able to pull the header into our page when we use an iframe but because of the way the header is constructed/coded the mouse over drop-down menus aren't working as they should when we use the iframe. The drop-down menus are "cascading" underneath the rest of the content on the page and we weren't able to bring them to the "top".
当我们使用iframe时,我们可以将标题拉入页面,但是由于标题的构造/编码方式,鼠标悬停下拉菜单在我们使用iframe时不能正常工作。下拉菜单在页面上的其他内容下面“级联”,我们无法将它们带到“顶部”。
As a temporary work around, were are hosting the HTML on our own servers.
作为临时工作,我们在自己的服务器上托管HTML。
Any ideas?
4 个解决方案
#1
3
If you choose to do this in Java, it's nice and easy using the HttpClient from Apache Commons.
如果您选择在Java中执行此操作,那么使用Apache Commons的HttpClient非常简单。
public static String fetchSourceHtml( String urlString ) {
try {
HttpClient httpClient = new HttpClient();
GetMethod getMethod = new GetMethod( urlString );
getMethod.setFollowRedirects( true );
int httpStatus = httpClient.executeMethod( getMethod );
if (httpStatus >= 400) {
return "";
}
String sourceHtml = getMethod.getResponseBodyAsString();
return sourceHtml;
}
catch (IOException e) {
return "";
}
}
For a quick and dirty solution, your JSP you can call this method directly. You could, of course, create a taglib tag to call the method if you prefer.
对于快速而肮脏的解决方案,您可以直接调用此方法。当然,如果您愿意,您可以创建一个taglib标记来调用该方法。
You may want to change the time-out and retry mechanism for HttpClient. By default it will automatically try up to a maximum of 3 times with each attempt timing out after 30s.
您可能希望更改HttpClient的超时和重试机制。默认情况下,每次尝试在30秒后超时,它将自动尝试最多3次。
However, you probably want to look into caching the strings for a suitable period of time. You really don't want to make 2 blocking external http requests for each page access to your site.
但是,您可能希望将字符串缓存一段合适的时间。您真的不希望为每个页面访问您的站点制作2个阻止外部http请求。
#2
1
JSP includes don't support including remote files, which is why a relative URL is required: http://java.sun.com/products/jsp/syntax/1.2/syntaxref1214.html
JSP包括不支持包含远程文件,这就是需要相对URL的原因:http://java.sun.com/products/jsp/syntax/1.2/syntaxref1214.html
I suggest writing a function which opens a connection to that page and downloads the contents and then prints them to your own out
stream. Then you can put that function in a local file and just include
that.
我建议编写一个函数,打开与该页面的连接并下载内容,然后将它们打印到您自己的流出。然后你可以将该函数放在本地文件中并只包含它。
#3
1
How about using the JSTL core library and doing:
如何使用JSTL核心库并执行:
<c:import url="http://www.CLIENT.com/CLIENT2/MiddlePageFiles/Vendor_header.html" />
That should be able to include remote content at request time.
这应该能够在请求时包含远程内容。
#4
-1
JSP includes are not meant to work like that with external servers. Here is a completely horrible way to fix your problem, but it was the only option for me in a similar situation. Write a class to actually parse the html from that site, and then print it out. I would add that whenever you are going to do something like this, it is always a good idea to have some sort of authentication mechanism in place.
JSP包含并不意味着与外部服务器一样工作。这是解决问题的完全可怕的方法,但在类似的情况下,这是我唯一的选择。编写一个类来实际解析该站点的html,然后将其打印出来。我想补充一点,无论何时你想做这样的事情,总是有一个好的想法,有一些适当的认证机制。
#1
3
If you choose to do this in Java, it's nice and easy using the HttpClient from Apache Commons.
如果您选择在Java中执行此操作,那么使用Apache Commons的HttpClient非常简单。
public static String fetchSourceHtml( String urlString ) {
try {
HttpClient httpClient = new HttpClient();
GetMethod getMethod = new GetMethod( urlString );
getMethod.setFollowRedirects( true );
int httpStatus = httpClient.executeMethod( getMethod );
if (httpStatus >= 400) {
return "";
}
String sourceHtml = getMethod.getResponseBodyAsString();
return sourceHtml;
}
catch (IOException e) {
return "";
}
}
For a quick and dirty solution, your JSP you can call this method directly. You could, of course, create a taglib tag to call the method if you prefer.
对于快速而肮脏的解决方案,您可以直接调用此方法。当然,如果您愿意,您可以创建一个taglib标记来调用该方法。
You may want to change the time-out and retry mechanism for HttpClient. By default it will automatically try up to a maximum of 3 times with each attempt timing out after 30s.
您可能希望更改HttpClient的超时和重试机制。默认情况下,每次尝试在30秒后超时,它将自动尝试最多3次。
However, you probably want to look into caching the strings for a suitable period of time. You really don't want to make 2 blocking external http requests for each page access to your site.
但是,您可能希望将字符串缓存一段合适的时间。您真的不希望为每个页面访问您的站点制作2个阻止外部http请求。
#2
1
JSP includes don't support including remote files, which is why a relative URL is required: http://java.sun.com/products/jsp/syntax/1.2/syntaxref1214.html
JSP包括不支持包含远程文件,这就是需要相对URL的原因:http://java.sun.com/products/jsp/syntax/1.2/syntaxref1214.html
I suggest writing a function which opens a connection to that page and downloads the contents and then prints them to your own out
stream. Then you can put that function in a local file and just include
that.
我建议编写一个函数,打开与该页面的连接并下载内容,然后将它们打印到您自己的流出。然后你可以将该函数放在本地文件中并只包含它。
#3
1
How about using the JSTL core library and doing:
如何使用JSTL核心库并执行:
<c:import url="http://www.CLIENT.com/CLIENT2/MiddlePageFiles/Vendor_header.html" />
That should be able to include remote content at request time.
这应该能够在请求时包含远程内容。
#4
-1
JSP includes are not meant to work like that with external servers. Here is a completely horrible way to fix your problem, but it was the only option for me in a similar situation. Write a class to actually parse the html from that site, and then print it out. I would add that whenever you are going to do something like this, it is always a good idea to have some sort of authentication mechanism in place.
JSP包含并不意味着与外部服务器一样工作。这是解决问题的完全可怕的方法,但在类似的情况下,这是我唯一的选择。编写一个类来实际解析该站点的html,然后将其打印出来。我想补充一点,无论何时你想做这样的事情,总是有一个好的想法,有一些适当的认证机制。