aspose file tools*
The moose likes JSP and the fly likes mvc model with jsp, servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "mvc model with jsp, servlet " Watch "mvc model with jsp, servlet " New topic
Author

mvc model with jsp, servlet

frank jardillier
Greenhorn

Joined: Sep 11, 2007
Posts: 12
Hi!

im trying to build a small MVC application. I have one servlet (dispatch) that instanciate RequestDispatcher and forwarding to some JSP view according to request parameters. This servlet also makes some request from the database and sending the results in the request scope for the jsp view.

I have something like that :
-> JSP -> servlet dispatch -> JSP

All is fine except one JSP view. This jsp has to display a chart (with jfreechart) with the values that are put in the request scope by the dispatch servlet. here the code :



the servlet generer_graphique.do has to create a gif/jpeg chart into the output stream. Well, my problem is that the bean daovaleurs is unaccessible from the generer_graphique.do servlet. How can i resolve it? I havent used the Session scope because im in an ajax context, dispatch.do is called more than one time and the request scope is "safe" for threading.

so...if you can help me.... i have done something wrong, but what?
thanks a lot
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Have you considered adding a key to the <img /> tag that can be used to identify the chart that needs to be created?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
frank jardillier
Greenhorn

Joined: Sep 11, 2007
Posts: 12
well...no...

but, really, i dont understand why adding a key to the <img> tag .
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

You do know that you can't send both text (HTML/Javascript) and binary data (your image) in the same response, right?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61010
    
  65

What information is needed from daovaleurs to know how to render the image? Can you not pass that info along as request params on the image URL?

Also, you might consider that you're doing things in the wrong place. If the information is needed when rendering the image, why put it on the request that creates the JSP rather than the one creating the image? Since we have no idea what's required to set up daovaleurs in the first place, we can't tell you.
[ September 11, 2007: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
frank jardillier
Greenhorn

Joined: Sep 11, 2007
Posts: 12
the daovaleurs bean has hundred data; they are statistical information. So i cant put them into the url as parameters for the generer_graphique.do servlet.

ok, so the used method is in wrong way isnt it? The MVC model that i'm usinf in not good for my jsp view?

To solve it, do i have to create my daovaleurs bean into the genrer_graphique.do servlet instead of the dispatch.do servlet? And then, the dispatch.do servlet provides to the jsp via the request scope only the "logical" information that the generer_graphique.do servlet could use to request the database and instanciate the daovaleurs bean?

thank you for your answer
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61010
    
  65

Again, without knowing your whole system it's hard to say for sure, but in general you should be putting the data where its needed rather than creating in an unneeded location and then have to pass it around. Is there anything that prevents you from setting up the data in the image servlet?
frank jardillier
Greenhorn

Joined: Sep 11, 2007
Posts: 12
ok.
data could be extracted from database in the servlet which generates the chart.

Reading your posts shows to me that my method is wrong. I have to extract data into the servlet code only when needed.

The whole code is very poor because the code is very simple. Here the code for the servlet dispatch.do



What i was trying to do ? First, daovaleurs is a bean that requests the database according to some paramaters. Result is an ArrayList() with some hundred elements.

My idea is that i can request once time the database for the same parameters, so the database server is used only when new data is needed. The result is put into an Attribute. And this attribute is dispatched to the JSP view. there is more than one JSP view possible, the "forwarding" is according to a "dispatch.do" parameter (name "mode").


here is the code for the page_graphique.jsp :



here is the code of the generer_graphique.do servlet :




With these codes, the daovaleurs Attribute is null into the generer_graphique.do servlet. This is "normal". My first question was is there one method to "put" the daovaleurs bean from the JSP to the servlet called by the <img> tag ? because i want to extend the JSP to display a table with the same daovaleurs bean.

if i understand what you have written, this is not possible.

I'm going to explain another time what i was looking for. Sorry if my english is bad. Well, i want to build a simple webapp which can request
many times the database server. Data extracted can be "important", like some hundrer of element. Each data extracted can be displayed as tables, charts or maps. With the idea to minimize the amount of real requests to the database server, i thought using a "cache" Attribute which can be used by any JSP view. Each JSP view can display the data as it wants. For example, the page_graphique.jsp view can display just one chart, but in my mind, could display with the same data Attribute chart AND table. So, only one request was done for the same data.

in this context, i was trying to implement the webapp in a MVC structure, so i use a central servlet which extracts data and forwards them to the right JSP view. Each JSP view can display the data how it wants (charts, maps, tables with the same data).
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

It sounds like you might want to read this data, bind it to session scope, and the forward to the JSP for HTML rendering. Within the JSP's HTML code you can have image tags which point to a servlet which can stream your binary data.
If it's in session scope, you should be able to identify it with a fairly short key which can be worked into the img tag's src attribute.

If you're interested, we have a sample app in our codebarn that contains a servlet that streams images to the browser.
http://www.javaranch.com/codebarn/codebarn-servlets-simplestream.jsp


In our example, the images are read from disk using ServletContext.getResourceAsStream. In your case, you'll probably want to save the images as byte arrays and read directly from those.

This, of course depends on the images being reasonably small.

If they are not small, you may want, instead, to store the criteria for reading them from the database in session which can also be mapped to a simple key which can be worked into the img's src attribute. Then with each image request, you pull the criteria from session scope using the key, read the image from the database and stream it to the browser.

Does any of this help?
frank jardillier
Greenhorn

Joined: Sep 11, 2007
Posts: 12
thank you for yout response.
it's very interested example for me. I have learn something more!

if i use this example, i have to manage myself the cyclelife of the images produced. Why not ?

But, because there is always one "but", i have thought about the session scope to store data, then it would be easier to share the same data. Because i love to complicate the problem, i'm in an ajax context. Well, the final HTML page looks a table, each cell is the html result of an ajax call (function $().load() from jquery). Each ajax call is a request to the dispatch.do servlet with different parameters for each call. So, session scope is not thread safe, storing attribute in the request scope is thread safe, that's why i used it.

Second point, take the example that my JSP view produces image and table from the same data. Image can be produced before the jsp was called using your example. But what about the table? i have to store it too.

Your example is great because thanks to it i have learn new items. But, i dont think this is the way to use for my problem. I'm sorry to tell that because you are trying to help me and i thank to you for that. It's really great to know that someone thinks about my problem.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by frank jardillier:

Well, the final HTML page looks a table, each cell is the html result of an ajax call (function $().load() from jquery).

That sounds outrageously expensive.
Do you have a good reason for doing this?



Second point, take the example that my JSP view produces image and table from the same data. Image can be produced before the jsp was called using your example. But what about the table? i have to store it too.


You don't need to store the table
Build the table on the first request, and get the image after the table has been rendered in the browser (just like any other HTML page with images)
frank jardillier
Greenhorn

Joined: Sep 11, 2007
Posts: 12
well, i was trying this method.
if i have a good raison to use it? Request the server only when it's necessary (eg when the user wants to see a chart or a map); Less charge on server : the client is working, there is more power on the client side than on the server side; the user sees the elements displaying and there is no blank page during some seconds...

it's just a try jquery is very fas, light and works on all well-known browsers. the "final table" has 6-7 cells. Some cells share the same data but the display is different (table or chart or map) and my first try is to code one JSP view for one element type; finally, each cell is the html result of one JSP. May be the next time, i will try to code only JSP which produce the "whole" table (devlopping JSP custom tags eg).

i have another question about session scope or attribute. How many bytes can be stored in request scope or session scope? Is the right place (session scope ) to store data or prefetch computing ? is there a real overhead process ? well...i have to ask these questions into a tomcat forum...
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

There is no fixed limit on session size.
A session is little more than a map of references to objects on the Java heap so your limit has more to do with the amount of ram allocated to the JVM.

If you're clustering servers and using session replication, you will also want to consider the overhead required to do all the serialization and deserialization required to stream the session data to all the other nodes.


If you're delaying the retrieval of the table's cell data until someone actually needs it (Like with Google Maps), then yes a separate AJAX call for each cell can help reduce traffic and make your page feel more responsive. If, on the other hand, you're just filling all the cells in a table with an AJAX request for each every time the page loads, then, it sounds like a lot of extra overhead with no gain to me.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61010
    
  65

Indeed,it would be an egregious misuse of Ajax and jQuery to pepper the server with a multitude of requests at page load when the cells could easily be renderred in the initial request for the JSP. What is the reason to not just render them in the first request?
frank jardillier
Greenhorn

Joined: Sep 11, 2007
Posts: 12
okay.
thank you, all the answers help me a lot.
frank jardillier
Greenhorn

Joined: Sep 11, 2007
Posts: 12
Originally posted by Bear Bibeault:
Indeed,it would be an egregious misuse of Ajax and jQuery to pepper the server with a multitude of requests at page load when the cells could easily be renderred in the initial request for the JSP. What is the reason to not just render them in the first request?


okay. i'm a newbie about server performance management.
Your idea will be my next try
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: mvc model with jsp, servlet