I have a jsp page in which their is header, left and right panel, header and left panel is static.
Left panel is having href, if we click on link a new page will be loaded in right panel. I want to achieve this task using Jquery Ajax without refreshing whole page.
Now my query is on click of link/button using jquery ajax I call servlet and I from servlet I want to set jsp dynamic values and then load the same in right panel with updated values.
I tried setting the values but I am getting null. Can you please assist me in setting dynamic values in jsp.
Below is my sample code on click of button I want to include hello.jsp with dynamic variable "message" if I succeed here I can solve my above problem.
index.jsp
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JQuery AJAX Testing</title>
<script src="js/jquery-1.11.0.min.js"></script>
<script>
$(document).ready(function() {
$("button").click(function(){
$.ajax({
type : "post",
url : "AjaxServlet",
success : function(responseText) {
$('#message').load("jsp/hello.jsp");
},
error : function(xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
});
});
</script>
</head>
<body>
<div id="ajaxtest">
<h2>AJAX Testing using JQuery</h2>
</div>
<div id="message"></div>
<button>Change Content</button>
</body>
</html>
AjaxServlet code:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("Servlet post method is called");
// I want to set this value in hello.jsp
request.setAttribute("message", "I am from request get attribute");
response.getWriter().write("This is reply from servlet");
}
hello.jsp
<h1>Hello this page is include from index JSP</h1>
<%=request.getAttribute("message")%>
I want to set message in hello.jsp from servlet on load from JQuery
Pise