I am using Ajax to call Servlet and trying to forward to JSP from Servlet.
The problem is .forward doesn't work for me in Servlet.
The forward works perfectly if i remove Ajax call. Could someone suggest me where i am wrong.
Here is my code:
//Ajax function (This function is onChange function on a textfield)
var req;
function name_onChange() {
//var idField = document.getElementById("action");
var url = "HelloServlet?action=submit";
if (typeof XMLHttpRequest != "undefined") {
req = new XMLHttpRequest();
} else if (window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
}
req.open("GET", url, true);
req.onreadystatechange = callback;
req.send(null);
}
// This is my servlet code
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
String action = req.getParameter("action");
req.setAttribute("name","ABC"); //executes the below lines but nothing happens
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/ResultJSP.jsp");
dispatcher.forward(req,res);
}