| Author |
reload jsp page using Ajax
|
duhit Choudhary
Ranch Hand
Joined: Apr 01, 2012
Posts: 64
|
|
hi all,
i am new in jsp and i want to reload jsp page say one.jsp in which i have to change a div (content) with another jsp page say two.jsp. i also want to do this by using ajax. if anybody can help to tell how to do that would be very good.
thanks in advance.
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56150
|
|
|
Easiest way would be to use jQuery and its .load() method.
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
sourav jain
Greenhorn
Joined: Nov 19, 2012
Posts: 22
|
|
The following scriptlet code adds a Refresh header that specifies a 60-second interval for refreshing the JSP. Place this code at the top of the JSP before any content appears:
<% response.addHeader("Refresh","60"); %>
If you want to refresh the JSP to another web component or page, use this syntax:
<% response.addHeader("Refresh","10;
http://localhost:8080/home/thanks.jsp"); %>
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56150
|
|
sourav jain wrote:The following scriptlet code adds a Refresh header...
That will cause the whole page to refresh. It will not help the OP who has requested that only part of the page be refreshed via Ajax.
(P.S. Refreshing the whole page needlessly is a poor practice that has been obsolete since the advent of Ajax.)
|
 |
sourav jain
Greenhorn
Joined: Nov 19, 2012
Posts: 22
|
|
Hi i found solution. i am refresh the date.jsp page into div (id="t") of test.jsp without reloading test.jsp page
Test.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="js/jquery.js">
</script>
<script type="text/javascript">
var i = setInterval(function(){
$("#t").load("date.jsp");
},1000);
</script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<div id="t">
<jsp:include page="date.jsp" ></jsp:include>
</div>
<p> hi </p>
</body>
</html>
2) date.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page import="java.util.Date"%>
<%@page import="java.text.SimpleDateFormat"%><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
Date d = new Date();
SimpleDateFormat sp = new SimpleDateFormat("hh:mm:ss");
String t= sp.format(d);
out.print(t);
%>
</body>
</html>
|
 |
 |
|
|
subject: reload jsp page using Ajax
|
|
|