File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Send response to AJAX from jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Send response to AJAX from jsp" Watch "Send response to AJAX from jsp" New topic
Author

Send response to AJAX from jsp

Iman Paryudi
Greenhorn

Joined: Mar 26, 2012
Posts: 11
I have a problem in returning response to AJAX from jsp.
I have tried this code:
response.getWriter().print(result);

It works but the response will return not only the result but also HTML tags, like this:

20.0<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> </body></html>

How can I return only the value?
Or how can I eliminate the unwanted string behind the value?

William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12781
    
    5
How can I return only the value?


Simple - use a servlet. JSP always assumes you want to write an HTML page.

Bill
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61190
    
  66

William Brogden wrote:Simple - use a servlet. JSP always assumes you want to write an HTML page.

I'll disagree with this. JSP can be used to create any text view. It doesn't have to be an HTML page. In fact, I use JSP quite often to create little HTML fragments to return as Ajax responses.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61190
    
  66

Iman Paryudi wrote:I have a problem in returning response to AJAX from jsp.
I have tried this code:
response.getWriter().print(result);

You have Java code in a JSP? In 2012?

What's the value of result?

You haven't given us any details at all. What type of response are you trying to write? How are you formulating result? Where is this code embedded?

Iman Paryudi
Greenhorn

Joined: Mar 26, 2012
Posts: 11
William Brogden wrote:
How can I return only the value?


Simple - use a servlet. JSP always assumes you want to write an HTML page.

Bill


Bill, I have tried to use servlet but I had difficulties in calling the bean.
FYI, I am using bean to store the value. By using jsp, I can call the bean using:
<jsp:useBean id="beanAnalysis" class="bean.Analysis" scope="application"/>

AFAIK, bean can only be called from servlet if the servlet is called by another servlet and not by jsp.

Do you have any information on how I can use servlet in this context?

Below is my coding in jsp:

<html>
<head>
</head>
<body>
<jsp:useBean id="beanAnalysis" class="bean.Analysis" scope="application"/>

<%
beanAnalysis.setWallUValue(Float.parseFloat(request.getParameter("val")));

//MORE CODING ON CALCULATIONS

//CALCULATE ENERGY PERFORMANCE
SolarGainCalculation sgc = new SolarGainCalculation();
sgc.setTransmissionLossEP(beanAnalysis.getCurrentTransmissionLoss());
sgc.setVentilationLossEP(beanAnalysis.getCurrentVentilationLoss());
sgc.setInternalGainEP(beanAnalysis.getCurrentInternalGain());
sgc.setSolarGainEP(beanAnalysis.getCurrentSolarGain());
beanAnalysis.setCurrentEnergyPerformance((int)sgc.energyPerformance());

response.getWriter().print(beanAnalysis.getCurrentEnergyPerformance()); //THIS IS THE VALUE I RETURN TO AJAX

%>
</body>
</html>


Iman
Iman Paryudi
Greenhorn

Joined: Mar 26, 2012
Posts: 11
Bear Bibeault wrote:
Iman Paryudi wrote:I have a problem in returning response to AJAX from jsp.
I have tried this code:
response.getWriter().print(result);

You have Java code in a JSP? In 2012?

What's the value of result?

You haven't given us any details at all. What type of response are you trying to write? How are you formulating result? Where is this code embedded?





First of all, let me explain what I am trying to do.
I am trying to make a gui using dojo slider. What I want is that each time user slide the slider, the current value of the slider will be displayed in the textbox beside it.
Next, this value will be used in further calculation. The result of this calculation will be displayed in another textbox in the page.
What I have done is: I create AJAX code to send the current value of the slider to be calculated by java code. The value sent is of type float. I use jsp to write the java code for the calculation.
In the jsp page, I have to call bean because some data needed for the calculation are stored in this bean. This is my reason to use jsp and not servlet.
Acutally I am already able to send the value from this calculation back to AJAX. The problem is that the returned value is not only contained numeric value but also string (HTML tags) as I mentioned in my question.
Meanwhile I want the returned value only contain numeric value. This is important since the value will be used again in the next calculation.
I have tried a way to eliminate the string but no luck.

Below is my coding.

This is coding that contains AJAX



And below is the coding that return the value to AJAX:
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61190
    
  66

And below is the coding that return the value to AJAX:

Which has a bunch of HTML tags in it. Why should you be surprised that they are in the response when you put them there?

And William's original point is valid: if you don't need HTML in the response you shouldn't be using a JSP in the first place.
Iman Paryudi
Greenhorn

Joined: Mar 26, 2012
Posts: 11
I have solved my problem by using servlet as Bill's suggestion. Thanks alot Bill.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Send response to AJAX from jsp