• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Send response to AJAX from jsp

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

How can I return only the value?



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

Bill
 
Sheriff
Posts: 67735
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bear Bibeault
Sheriff
Posts: 67735
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 67735
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have solved my problem by using servlet as Bill's suggestion. Thanks alot Bill.
 
She said she got a brazillian. I think owning people is wrong. That is how I learned ... tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic