Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Loading jsp fragments through AJAX

 
Chad Cook
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65226
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Chad Cook
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65226
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65226
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65226
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65226
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't you go dissing Santa Claus!!!
 
J. Insi
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65226
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"it can't" isn't a very good description of what is happening.
 
J. Insi
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65226
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65226
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65226
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do those error messages tell you?
 
Stefan Evans
Bartender
Posts: 1784
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic