jQuery in Action, 2nd edition*
The moose likes HTML, CSS and JavaScript and the fly likes How to display Ajax result on the same jsp page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "How to display Ajax result on the same jsp page" Watch "How to display Ajax result on the same jsp page" New topic
Author

How to display Ajax result on the same jsp page

prasad regula
Greenhorn

Joined: Jul 03, 2006
Posts: 24
I have been going through multiple examples on the web. They all say
1) go to one.jsp write the ajax code in the script tag and send the data from this jsp.
2) retrive the data and display in two.jsp

They are selecting the value from one jsp and displaying the value in the other jsp. What do I need to do if I need to select the value in one combo box, and display the related value on the second combo box of the same page.

I know no JSF


PRSubject
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61314
    
  66

prasad regula wrote:I know no JSF

So? What does JSF have to do with anything?

I have been going through multiple examples on the web. They all say
1) go to one.jsp write the ajax code in the script tag and send the data fr om this jsp.

First, some basic concepts. You are not really sending an Ajax request from a JSP. A JSP is merely a server-side template for creating HTML pages. So once the page arrives at the browser where JavaScript (and Ajax) can be executed, it's just an HTMl page. It is no longer a JSP. This may seem like a pedantic distinction, but's it's actually a very important concept to understand.

2) retrive the data and display in two.jsp

No. I'm sure that's not what they say. The whole point of Ajax is to make a request without having to go to another page.

Now, the Ajax request may use a JSP to format its own response. Maybe that's what you are confused about?

They are selecting the value from one jsp and displaying the value in the other jsp. What do I need to do if I need to select the value in one combo box, and display the related value on the second combo box of the same page.

"Same page" is always going to happen. You don't have a choice in the matter.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
prasad regula
Greenhorn

Joined: Jul 03, 2006
Posts: 24
Thanks for the reply. It was helpful. I also found a good tutorial at http://www.learn-ajax-tutorial.com/. Even it speaks the same point you have said(Everything starts with a html).

I have one simple doubt. If this is a case what would be the situation if this page where I am applying the ajax, is not a starting page and comes somewhere in the middle of the page flow, and this page has some text to be displayed on the top say Name of the user. How do you manage the dynamic values with a html.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61314
    
  66

Sorry, I am not understanding your question. But it does sound more like an HTML question than a JSP question, so I've moved this post accordingly.
prasad regula
Greenhorn

Joined: Jul 03, 2006
Posts: 24
One way you are right. It has become a html question from Ajax as ajax needs to be a html at the time of submitting the data to the webserver. My doubt here is as below:
1) has it not been a ajax issue,
I would have placed a jsp
Submitted the jsp to retrive the data from the database and
Redisplay the page with the retrived data from the database.

With JSP I have the facility of sending the HttpServletResponse data etc. Now if a html comes in between, how do we manage with the dynamic data sent by the server.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61314
    
  66

prasad regula wrote:One way you are right.

One way?

Still not getting your question. Are you asking how to deal with the response once it's returned to the page? Or on the server?
prasad regula
Greenhorn

Joined: Jul 03, 2006
Posts: 24
On the server side in the servlet I have the following code



On the client side I need to display this user name on the top of the page. Had that been a jsp I would have written a following statements


Now this has become a html as I need to deal this page with ajax. How should I get that username attribute and display on the html.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I think you're still missing Bear's point.

Both of the code samples you posted are run on the server side.
To see the code that will be interpreted on the client side, use your browser's "View Source" option.
You'll notice that there is no JSP code in there. You will only see the results from your JSP which, again, is interpreted on the server side.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Bear Bibeault wrote:
prasad regula wrote:One way you are right.

One way?

Hey, that's one more way to be right than *I* usually am.
 
GeeCON Prague 2014
 
subject: How to display Ajax result on the same jsp page