I have a HTML page on my computer and a JSP file on a distant server.
Now how do I,
Display content from the JSP file into the HTML page.
The HTML page must be strictly HTML (no server-side language), but can use AJAX/JavaScript.
Is it even possible to get the information from the server through the JSP page without turning the HTML page into a JSP file itself? How would I implement it?
<script type="text/javascript" src="js/jquery.min.js"></script>
// I have jquery.js under js directory in my webapp
<script type="text/javascript">
var url = "my.jsp";
$(function(){
$.ajax({
url : url, // Pass you Servlet/ JSP Url
dataType : 'html',
success : function(response) {
alert('Success');
$('#output').html(response);
},
error : function(request, textStatus, errorThrown) {
alert(request.status + ', Error: ' + request.statusText);
// perform tasks for error
}
});
});
</script>
JSP
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%
out.println("<h1>Hello World</h1>"); // Write html values here
%>
Your HTML
.... <div id="output"></div>
with using Ajax:
function ClickMe_Click()
{
$.ajax({
type: "post",
url: "path/your.jsp",
data: {"param":"val"},
success: function(msg) {
alert(msg.data);//your info from jsp
},
error:function (xhr, ajaxOptions, thrownError){
alert(thrownError);
}
});
}