aspose file tools*
The moose likes JSP and the fly likes Loading jsp fragments through AJAX Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Loading jsp fragments through AJAX" Watch "Loading jsp fragments through AJAX" New topic
Author

Loading jsp fragments through AJAX

Chad Cook
Greenhorn

Joined: Dec 02, 2004
Posts: 29
I'm currently working on a project where we are generating large amounts of HTML server side and returning it through AJAX to dynamically update the page. While this works, it is a pain constructing the HTML via Java strings. Is there a way to have a servlet or other Java code call a JSP fragment to render it's contents and then return the resulting HTML so it can be passed back to the page? It would be nice to code the JSP fragment with the syntax highlighting in my IDE rather than using dozens of strings in the Java code.

Or if someone knows of a general way to do this better with some newer technology. I feel out-of-touch with Java technology and that I should know of an obvious solution to this problem.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61424
    
  67

Firstly, you should rarely ever have to generate HTML in Java strings. There are plenty of better ways to do it.

If there's no processing involved, just hit the JSP fragment directly. Why go through a servlet?

If there's pre-processing involved, hit the servlet, do the processing, then forward to a JSP.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Chad Cook
Greenhorn

Joined: Dec 02, 2004
Posts: 29
There is some processing involved, but the way the application is designed it needs to be shown on an existing page, hence the AJAX call. Unfortunately we can't forward to a new JSP because we have to keep the old page up.

One way to visualize it is we have several widgets which the user can display on the page, but each one can perform functions through AJAX. It would be nice to code what is displayed in the widget in a JSP fragment and then return the generated HTML back.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61424
    
  67

Chad Cook wrote:One way to visualize it is ...
I completely get what you are doing. I do it all the time.

Chad Cook wrote:Unfortunately we can't forward to a new JSP because we have to keep the old page up.
I didn't say anything about replacing the old page or not using Ajax. You can use all the same mechanisms in an Ajax request that you can in a "normal" request.

It would be nice to code what is displayed in the widget in a JSP fragment and then return the generated HTML back.
It can. What makes you think it can't?
Chad Cook
Greenhorn

Joined: Dec 02, 2004
Posts: 29
Bear Bibeault wrote:
It would be nice to code what is displayed in the widget in a JSP fragment and then return the generated HTML back.
It can. What makes you think it can't?

How would I do this?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61424
    
  67

Just make your Ajax request hit the URL of:

a) A JSP that generates the HTML fragment

or

b) A servlet that performs processing, then forwards to a JSP that renders the HTML fragment

I'm not quite sure what the problem is. This is easy as pie.
Chad Cook
Greenhorn

Joined: Dec 02, 2004
Posts: 29
It doesn't sound too hard, I'm just brushing up on some concepts and technologies after being out of the game for a while.

I'll check this out. Thanks.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61424
    
  67

I think the problem is that you are thinking that Ajax requests are somehow "special". From the point of view of the server, they aren't.

You can use all the same mechanisms that can be used in a "normal" request. All that matters is that what is returned as the response is what you want.
Chad Cook
Greenhorn

Joined: Dec 02, 2004
Posts: 29
Ajax isn't special? Next thing you know you'll try to tell me that Santa Claus isn't real...
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61424
    
  67

Don't you go dissing Santa Claus!!!
J. Insi
Ranch Hand

Joined: Aug 11, 2010
Posts: 90
Bear Bibeault wrote:Just make your Ajax request hit the URL of:

b) A servlet that performs processing, then forwards to a JSP that renders the HTML fragment

I'm not quite sure what the problem is. This is easy as pie.


Hey, I'm donig that now...
firstly ajax call servlet,
then servlet set some variable sessions,
and then forward into jsp page rendered the variable stored previously,,,

but it can't. Which portion that I forgotten?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61424
    
  67

"it can't" isn't a very good description of what is happening.
J. Insi
Ranch Hand

Joined: Aug 11, 2010
Posts: 90
Bear Bibeault wrote:"it can't" isn't a very good description of what is happening.


okay, sorry... orite. Here is the quick summary that I already done.

I have a bean;


and also i have this servlet that taking up the Bank List ...



And then I tried to put my jsp rendering with this;


Well, the case that I said, "It can't " here is that there is no output.
It seems the List is detected as not empty... but there's not rendered properly.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61424
    
  67

J. Insi wrote:"It can't " here is that there is no output.

Again, be precise! No output at all? Not even the table tag? Or no table rows? If you keep people guessing, they lose interest and will just move on to other topics.
J. Insi
Ranch Hand

Joined: Aug 11, 2010
Posts: 90
Bear Bibeault wrote:
J. Insi wrote:"It can't " here is that there is no output.

Again, be precise! No output at all? Not even the table tag? Or no table rows? If you keep people guessing, they lose interest and will just move on to other topics.


Ya Bear Bibeault, like i said "No output".
No table at all, like i said.

The error generated was here;



what's the wrong thing I've done?

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61424
    
  67

That error is probably not related to your issue -- it means that you placed the bean in the session and it could not be serialized.

What else is in the logs?
J. Insi
Ranch Hand

Joined: Aug 11, 2010
Posts: 90
Bear Bibeault wrote:That error is probably not related to your issue -- it means that you placed the bean in the session and it could not be serialized.

What else is in the logs?

yes, that's right... I put the variable of that List into a Session.
So it's is unique.

and then here is the next log...




Which point that I forgot, is it the source package ordering issues, Bear Bibeault?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61424
    
  67

What do those error messages tell you?
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1018
Well because the only instances of "id" and "name" I can see on the page are referring to the bankData object, to me that says that the bankData object is a String.
Which means bankList must be a list of Strings.

But the code intimates that bankList is a list of Bank objects, so I don't understand how that could happen.
I guess my next step would be to investigate the new MysqlUtil().getBankTableData(); method to see what that does, and what it actually returns.



J. Insi
Ranch Hand

Joined: Aug 11, 2010
Posts: 90
Stefan Evans wrote:Well because the only instances of "id" and "name" I can see on the page are referring to the bankData object, to me that says that the bankData object is a String.
Which means bankList must be a list of Strings.

But the code intimates that bankList is a list of Bank objects, so I don't understand how that could happen.
I guess my next step would be to investigate the new MysqlUtil().getBankTableData(); method to see what that does, and what it actually returns.



Ya, stefan Evans.

I'm the one who arrange the ArrayList of Bank Object... and that's a real thing.
I need the Bank Object need to be placed inside the ArrayList so then later on I just iterate throught the List of that Banks .... so anyway, if you ask me
what is the content of the code of new MysqlUtil().getBankTableData();

Let me say it here; it is a method that obtaining rows from mysQL and then taking 'em added up into the ArrayList...

anway, the .getBankTableData() method is having no problem at all.

here is the code;
o gosh....


anyway, I'm just wondering, whether it is because of my project folder / package ordering issues... isn't?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Loading jsp fragments through AJAX