473,387 Members | 1,425 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Error in AJAX code

Hi Gurus,

I am new to Ajax and JAVA.
I am able to call the servlet class from the JSP page using AJAX.

The servlet class is called successfully.

I am processing some data and the data has to passed back to the front end JSP.

I have used the following code

**********************JAVA Script:***************************

Expand|Select|Wrap|Line Numbers
  1. function getRequestObject() {
  2.   if (window.ActiveXObject) {
  3.     return(new ActiveXObject("Microsoft.XMLHTTP"));
  4.   } else if (window.XMLHttpRequest) {
  5.     return(new XMLHttpRequest());
  6.   } else {
  7.     return(null);
  8.   }
  9. }
  10.  
  11. function displayTimeInCity(val) {
  12.   alert('test');
  13.   alert(val);
  14.   var address = "start";
  15.   alert(document.getElementById('F_010_ADDRESSTYPE'));
  16.   alert(document.getElementById('F_010_ADDRESSTYPE').value);
  17.   var addr = document.getElementById("F_010_ADDRESSTYPE").value;
  18.   alert(addr);
  19.   var data = "addr=" + escape(addr) + "&useHTML=true";
  20.   sendRequestWithData(address, data, showResponseText);
  21. }
  22.  
  23. function sendRequestWithData(address, data, responseHandler) {
  24.   request = getRequestObject();
  25.   request.onreadystatechange = responseHandler;
  26.   request.open("POST", address, true);
  27.   request.setRequestHeader("Content-Type", 
  28.                            "application/x-www-form-urlencoded");
  29.   alert(address);
  30.   request.send(data);
  31. }
  32.  
  33. function showResponseText() {
  34.   if ((request.readyState == 4) &&
  35.       (request.status == 200)) {
  36.       alert(request.responseText);
  37.     document.getElementById("F_010_STREET1").value =
  38.       request.responseText;
  39.   }
  40. }


*************************servlet Class*********************************

Expand|Select|Wrap|Line Numbers
  1. //Function called as "doPost" in this servlet....
  2. public String doHandleEvent(String eventName, HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException 
  3.   {
  4. // Set the name of the JSP that has to be called when this method is finished.
  5. // INITIAL_JSP ist defined in the interface Constants.java
  6.     System.out.println(" in events");
  7.     String nextJSP = INITIAL_JSP.jsp;
  8.  
  9.                 //Data passed as query parameter to the servlet class.....
  10.     if (request.getParameter("addr")!=null)
  11.     {
  12.        response.setHeader("Cache-Control", "no-cache");
  13.        response.setHeader("Pragma", "no-cache");
  14.        response.setContentType("text/html");
  15.        //System.out.println("Addr = "+request.getParameter("addr"));
  16.        //PrintWriter out = response.getWriter();
  17.        //out.print("naveen");
  18.        RequestDispatcher dispatcher =
  19.        request.getRequestDispatcher("naveen");
  20.        dispatcher.include(request, response);
  21.        return nextJSP;
  22.     }
  23.  
  24. return nextJSP; //nextJSP is the String param with "inital.jsp"
  25. }
Now my question is
How do i get the String "Naveen" in the JAVA Script Code...
"request.responseText"

I am getting the following error:

************************************************** ****************************************
<h1>Error: 500</h1><h2>Location: /INFOXPHARMA/start</h2><b>Internal Servlet Error:</b><br><pre>java.lang.IllegalStateException: Cannot forward as OutputStream or Writer has already been obtained at org.apache.tomcat.facade.RequestDispatcherImpl.doF orward(RequestDispatcherImpl.java:178) at org.apache.tomcat.facade.RequestDispatcherImpl.for ward(RequestDispatcherImpl.java:162) at com.sap.ip.me.api.runtime.jsp.AbstractMEHttpServle t.dispatchRequest (AbstractMEHttpServlet.java:907) at com.sap.ip.me.api.runtime.jsp.AbstractMEHttpServle t.doGetNotThreadSafe(AbstractMEHttpServlet.java:34 8) at com.sap.ip.me.api.runtime.jsp.AbstractMEHttpServle t.doGet(AbstractMEHttpServlet.java :689) at com.sap.ip.me.api.runtime.jsp.AbstractMEHttpServle t.doPost(AbstractMEHttpServlet.java:706) at javax.servlet.http.HttpServlet.service(HttpServlet .java:760) at com.sap.ip.me.api.runtime.jsp.AbstractMEHttpServle t.service (AbstractMEHttpServlet.java:313) at javax.servlet.http.HttpServlet.service(HttpServlet .java:853) at org.apache.tomcat.core.ServletWrapper.doService(Se rvletWrapper.java:405) at org.apache.tomcat.core.Handler.service(Handler.jav a :287) at org.apache.tomcat.core.ServletWrapper.service(Serv letWrapper.java:372) at org.apache.tomcat.core.ContextManager.internalServ ice(ContextManager.java:806) at org.apache.tomcat.core.ContextManager.service(Cont extManager.java :752) at org.apache.tomcat.service.http.HttpConnectionHandl er.processConnection(HttpConnectionHandler.java:21 3) at org.apache.tomcat.service.TcpWorkerThread.runIt(Po olTcpEndpoint.java:416) at org.apache.tomcat.util.ThreadPool$ControlRunnable. run (ThreadPool.java:501) at java.lang.Thread.run(Thread.java:534)</pre>

************************************************** ****************************************


Thanking you in advance.
Naveen
Oct 12 '07 #1
3 1922
dmjpro
2,476 2GB
This is a Java related Question.

What does it mean?

Expand|Select|Wrap|Line Numbers
  1. RequestDispatcher dispatcher =
  2.        request.getRequestDispatcher("naveen");
  3.  
Debasis Jana
Oct 12 '07 #2
This is a Java related Question.

What does it mean?

Expand|Select|Wrap|Line Numbers
  1. RequestDispatcher dispatcher =
  2.        request.getRequestDispatcher("naveen");
  3.  
Debasis Jana
Hi Debasis Jana,

Thanks for the reply.

This is the dispather object used to return any string along with the request object.

RequestDispatcher dispatcher =
request.getRequestDispatcher("naveen");

I am also using the
PrintWriter out = response.getWriter();
out.print("naveen");

in the new code.
This is used to output the text from the context.

Anywaz, I have resolved this issue.
I am using the "public String doHandleEvent" in the code I mentioned. This is extended from the "AbstractMEHttpServlet" class. This class is extended from the SAP ME. Which I think was cutomly used by SAP.

I have created my own servlet class using the actual "HttpServlet" class and the

PrintWriter out = response.getWriter();
out.print("naveen");

to return the string required and am able to read the string in the Javascript.

Anywaz thanks you again for your interest.

Naveen
Oct 12 '07 #3
dmjpro
2,476 2GB
Glad to hear it.........Post back again.

Debasis Jana
Oct 12 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: kamleshsharmadts | last post by:
I am using Ajax with struts in web application. from jsp i am calling a function of ajax.js onclick of a button. code of that call function which calling from jsp given as below:- ...
1
by: monudjn | last post by:
Hi I am implementing ajax in portal. Using ajax i am able to updating the content of portlets asynchronously. However i am facing a problem The Problem: While submitting the form i am getting...
7
ak1dnar
by: ak1dnar | last post by:
Hi, I got this scripts from this URL There is Error when i submit the form. Line: 54 Error: 'document.getElementbyID(....)' is null or not an object What is this error. Complete Files
3
by: =?Utf-8?B?bWNpbWFnaW5n?= | last post by:
We have recently applied AJAX to our web site, nothing particularly fancy, just some update panel, progress images and collapsible panels, just the basics to improve the user experience. The...
2
by: germ | last post by:
doing a simple page webmethod call an a page via PageMethods works fine in ie7 & opera9 the same call on firefox ( and I assume netscape ) generates the following error : Error: " nsresult:...
2
by: kpg | last post by:
I have an AJAX enabled web service consumed by an AJAX enabled web app, given a zip code it returns the city and state. Tested the web service, it works fine. I created a services collection...
4
by: Jeremy | last post by:
I am getting an error Sys.WebForms.PageRequestManagerServerErrorException: An unknown error occured while processing the request on the server. The status code returned from the server was:500. ...
20
by: vjayis | last post by:
hi when i m trying to fetch data from one page to another page using ajax, i get an error message in IE., but it runs well in firefox., could anyone help me., here is my javascript code., ...
9
by: Trapulo | last post by:
Hello, with ASP.NET 2.0 Ajax every unexpected error is managed client-side with a popup that reports the error to the user. In ASP.NET 3.5 this behavor has been changed: how can I have a similar...
7
by: Jim in Arizona | last post by:
I'm brand new at ajax. In fact, about 20 minutes ago was the first time I got it to work. The problem I'm having on another page did not work, however. I'm running into the following error: ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.