Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Ajax , Servlet and JSP Problem

843842Nov 26 2008 — edited Dec 1 2008
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);
	}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 29 2008
Added on Nov 26 2008
6 comments
1,217 views