3

I'm working on a web project. The flow is like this

  1. User inputs the parameters
  2. Call to servlet is made (post method)
  3. The servlet loads a large file (300.000 lines , one word per line) in a tree object. Does some calculations.
  4. Redirects to a new page for the results.

My question is this. The user may do this operation more than once. The loading of the file to the tree object is taking too long. How can I do this, only once? I've thought to have just one servlet (or jsp) and not redirecting to different pages, but manipulating the view with javascript. This way I think that I will manage to load the file only once (and whenever the user refreshes). Do you have any suggestions?

EDIT 1. This object is created by a class that is called from the servlet. I'm using apache tomcat 7 and java 1.6 . HTML5 is an option too (someone mentioned the history api).

Alkis Kalogeris
  • 17,044
  • 15
  • 59
  • 113
  • 1
    Two questions: First, is this a [tag:java] or [tag:javascript] question? Second, can we see some relevant code please? –  Apr 26 '13 at 15:56
  • It's a java question. There is no code. The question is how I can keep the object persistent through page changes. – Alkis Kalogeris Apr 26 '13 at 16:00
  • 1
    In this case i strongly advice to use [single-page application](http://addyosmani.com/blog/building-spas-jquerys-best-friends/). – Ikrom Apr 26 '13 at 16:10

2 Answers2

6

Is the file specific to the user ? Keep the object in the user's session.

If its common to many users and they only need to read/ view this data keep it in the application object.

If your talking about the view -> browser then yes the way to do is ajax and/or frames and/or iframes

Advantage with frames is you can keep the data in another farme and still access it from other pages, but change the main view area to another page completely


Detail user's session : available in servlets, filters, jsps etc -> key Object pair. can have any number of keys and objects but remember its all in the RAM, until session expires or if it is explicitly destroyed.

http://docs.oracle.com/cd/E19502-01/819-3669/bnaij/index.html

How to use the "application" object in a Servlet?

Application level http://docs.oracle.com/javaee/5/api/index.html?javax/servlet/jsp/PageContext.html get and setAttribute(String name, Object object) This object can be read in all pages and jsps. Need to take care if object is already there (getAttribute does not return null) before you do the task of initializing it. Does the main file change? IF so can keep some code that calls a jsps that refreshes it. (like an admin jsp)

Frames/ ajax is for client level and you need to ask a more specific question for this but stack over flow and google have many examples of using this.

Community
  • 1
  • 1
tgkprog
  • 4,493
  • 4
  • 41
  • 70
  • It's common to the users. What do you mean by "keep it in the application object" ? – Alkis Kalogeris Apr 26 '13 at 16:02
  • 1
    @alkis it means to save the object in Application Scope a.k.a. [`ServletContext`](http://docs.oracle.com/javaee/6/api/javax/servlet/ServletContext.html). – Luiggi Mendoza Apr 26 '13 at 16:11
  • Thank you Luiggi. I agree with you it's a very helpful answer. I will wait to see if any other good answers appear a little longer and then close it. tgkprog if you could refine your answer a little bit I can assure you, yours will be the selected answer. – Alkis Kalogeris Apr 26 '13 at 16:31
  • if it solves your problem you can choose it. but really would be better of you gave more information. are you showing the file onthe browser or was that just an idea so you can send it back in future requests and not have to load it at server again? if the user needs to see the full file ONLY then send it to the browser – tgkprog Apr 26 '13 at 17:13
  • The application level solution, takes care of my problem. So thank you – Alkis Kalogeris Apr 26 '13 at 17:58
2

You can use ajax for that + html5 history api. So when navigating you will not reload page, but with history api, you will have same visual effect

karaxuna
  • 26,752
  • 13
  • 82
  • 117