Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
178 views
in Technique[技术] by (71.8m points)

javascript - HTML page calls JSP file

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
<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>


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...