How to Include a JSP in a PHP page
I wish to include JSPs include files which contain java code in a PHP template. The two includes in question are a header file, and a footer file. Anyone any experience of doing this? We are considering just doing a HTTP request to grab the resulting HTML from the JSP f开发者_StackOverflow社区iles independantly, but aren't sure if there will be slight performance issues with doing so.
Is there any better solution using some of the tools within Apache to perform this?
echo file_get_contents('http://full/link/to/jsp/page');
If you JSP page echos a header, body structure, you'll need to strip it out. You can do that from the JSP side or PHP.
That's disabled on some systems so you might need to use cURL (it also allows you to post back which you might need to do if you're playing with forms).
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_URL, 'http://full/link/to/jsp/page');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
echo curl_exec($ch);
You can't include a JSP page into a PHP page.
You can do what you are thinking of though: doing a HTTP request to get HTML content from JSP and embed that into the PHP result. Not pretty, but will work.
There is the Java / PHP Integration extension, but it doesn't allow to compile Java code. I don't think there is a way to compile Java from PHP, if not executing command line commands.
Depending on your requirements, if you don't want to impact page loading, you could also perform an AJAX request to grab the content once the HTML page is loaded, and inject it in the page : this would move the problem to the client.
Does this JSP page change frequently, or depend on PHP page's parameters (some kind of advertisement) ?
You could also cache the output of your JSP (even by parameters) for a pair of hours or a whole day, to avoid calling the page on every request.
精彩评论