How do I include a header into my site that is hosted externally?
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:
<%@ include file="www.CLIENT.com/CLIENT2/MiddlePageFiles/Vendor_header.html" %>
We also tried
<%@ 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".
As a temporary work around, were are hosting the HTML on our own servers.
Any ideas?
Asked by: Blake301 | Posted: 28-01-2022
Answer 1
If you choose to do this in Java, it's nice and easy using the HttpClient from Apache Commons.
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.
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.
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.
Answered by: Julian779 | Posted: 01-03-2022Answer 2
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
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.
Answer 3
How about using the JSTL core library and doing:
<c:import url="http://www.CLIENT.com/CLIENT2/MiddlePageFiles/Vendor_header.html" />
That should be able to include remote content at request time.
Answered by: Tara479 | Posted: 01-03-2022Answer 4
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.
Answered by: Cadie464 | Posted: 01-03-2022Similar questions
java - Securing ActiveMQ externally by IP filtering
I have an instance of ActiveMQ running on an externally facing server and exposing a STOMP interface.
I would like to be able to ensure that externally connected clients can only receive topic notifications, and not send anything to the topic themselves. Any internally connected clients will be able to both receive and send.
I was hoping there might be some rules I could set up on the topic or server level...
java - Ant cannot find a class needed by an externally defined taskdef
I am trying to use the axis-java2wsdl ant task to create a wsdl from one of my java classes, but I cannot get the classpath correct.
I am using Ubuntu's libaxis-java package which installs axis-ant.jar in $ANT_HOME/lib and axis.jar in /usr/share/java. The interesting parts of my build.xml look like this:
<property name="library.dir" value="lib"/>
<property name="system.library.dir" value="...
java - Best way to grab website content externally
There's a search site whose search results are generated dynamically by javascript. So the user enters a query, and the site displays the content on the page, without refreshing.
I need to grab those search results programmatically (say from a Java program or a perl/python script).
So ideally, I can launch my program with 100 queries as user inputs, and then the program would hit that website with each qu...
java - how to sort JTable by providing column index externally
I would like to implement sorting on JTable by providing column index externally in program. Here is my sample code in which i have initialize JTable, Add one Column and 30 rows to JTable. After rows has been added i am sorting JTable by providing column index 0 but i could not get sorted data. how can i get my first column in sorted order? what's wrong with my code. Why sortCTableonColumnIndex() method could not sort data...
How does a Java thread behave when a method is accessed externally?
Suppose I have the following thread:
public class MyThread {
public void run() {
while (true) {
// do something forever
}
}
}
Then I instantiate the thread as follows:
MyThread thread = new MyThread();
What happens if I now call
thread.performSomeFunction()
Specifically, how does perform...
java - Add content in Response object externally
I have a simple application which renders a jsp file into the browser after getting some data from a servlet.Now I want to add some data in the form of some HTML Tag into the response object coming out of the jsp.
I have made a filter and response wrapper which overrides the getWriter method by returning a custom PrintWriter as:
StringWriter sw = new StringWriter();
public PrintWriter getWriter() {
...
java - filtering externally loaded javascript in htmlunit
While using htmlunit to scrape a webpage, I occasionally notice warnings like these that flood the console output.
Jul 24, 2011 5:12:59 PM com.gargoylesoftware.htmlunit.javascript.StrictErrorReporter warning
WARNING: warning: message=[Calling eval() with anything other than a primitive string value
will simply return the value. Is this what you intended?] sourceName=[http://ad.doubleclick.net/adj/N5762.m...
java - JPA when the database is modified externally
I have a simple web-app with Database with two tables.
The administrators can modified the first table manually directly from SQL script (for example with PHPMyAdmin) and my web-app should be write into second tables some data after this edit.
Can I do it?
java - Save operation - File is not externally editable
I am using JFileChooser and it is working fine.
File a = new File(strSavepath);
JFileChooser fc = new JFileChooser(a);
Problem is that I am unable to edit the saved file manually using Notepad or other editors unless I run garbage collector manually using VisualVM.
Am I missing something or someone else has also observed this behavior?
Externally access Spring Java RMI service in cloudbees
I have deployed a spring rmi web application(WAR file) to the cloudbees successfully. This application contains simple RMI service called "greetingRmiService" which return a String contains a greeting message. Here is the server log part that says it deployed my rmi service successfully.
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@79d9cd24: defi...
Still can't find your answer? Check out these amazing Java communities for help...
Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)