http://wpcertification.blogspot.com/2009/03/understanding-standard-portlet-life.html
at this page there is a sentence like: "The Portal server will create only one instance of portlet per JVM"
What does it mean? Is it means that; if i use class variable in my portlet, all users will use the same values?
I have a problem like this. I am keeping page number of a jsp page, in a class variable. And if my friend open this page with his computer, he always seeing the same page with me!!!
Any ideas?
Portlets (like servlets) can service multiple requests concurrently. They are scoped to the application and should be threadsafe.
You should not use member variables for per-user state. Request scope data should be held in the request (or possibly as render parameters). If you want user data to persist across requests, you should use the session.
Related
This is a question what I have been wondering for quite some time.
Often enough I have my jsp which has to post some data to my controller. In this jsp I have some data I need to post to controller, but aren't touched by the user.. (ie, administration data like an ID). As far as I know there are 2 options to give the controller this data.
Use <input type="hidden"> fields
Put everything in a session variable.
Option 2 has my preference since it requires me to type less code, and I can't forget hidden fields. Though this also has downsides, like another page overriding the session attribute.
What are your preferences? And are there any other options?
Thanks!
Davey
I always prefer to make session object as small and as light as possible. I know its convenient but if your site is high traffic then these session object size will soon start adding to the JVM instance of the Web Server.
So I would prefer option 1 over option 2.
I am new to Java and started exploring it a little bit.
I know this is silly question but i did not find any supporting blog or article on net which will clarify my doubt.
I want to know the scope of Java object.
Consider a scenario: Suppose i am accessing a page in browser say "index" which is using object X with some value it it and if i refresh the page can i get the previous value of object X?
If yes, how does it holds? because it all together a different request and how does it recognize that it is the same request?
Thanks in Advance
I am heavily recommending you to read the official docs on Object scope.
There are four possible scopes:
scope="page"--The object is accessible only from within the JSP page where it was created.
scope="request"--The object is accessible from any JSP page servicing the same HTTP request that is serviced by the JSP page that created the object.
scope="session"--The object is accessible from any JSP page sharing the same HTTP session as the JSP page that created the object.
scope="application"--The object is accessible from any JSP page used in the same Web application (within any single Java virtual machine) as the JSP page that created the object.
Here is the official docs link
This question is more to do with server side retention I assume when you say "if I refresh the page". If a servlet had stored the value that was presented on the page then yes, it would still show it on the page.
If the question was based around java applets and the value had not been persisted and just entered into a textbox then no, it would not still be there.
If you want to know about object scope you might consider rephrasing your question with an example. Most web scenarios with browsers for instance may use a framework such as Spring which is a little more complicated than just object scope.
If you want to learn more about web scope consider looking at this oracle page on the servlet life cycle.
You can store and retrieve objects by saving them as attributes in a session (HttpSession object) using:
public void setAttribute(java.lang.String name, java.lang.Object value)
public java.lang.Object getAttribute(java.lang.String name)
For example, I can save a user's score on a multi-page quiz:
session.setAttribute("score", (Integer) value);
score = ((Integer) (session.getAttribute("score"))).intValue();
This is persistent to traversing/refreshing pages. Even though it is not the same request, it is the same session.
I'm working on a web project. The flow is like this
User inputs the parameters
Call to servlet is made (post method)
The servlet loads a large file (300.000 lines , one word per line) in a tree object. Does some calculations.
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).
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.
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
There is a hyperlink in my webpage, on click of which it navigates to a jsp page. once am in that jsp page i should be able to lock the jsp page where if others access the same jsp page it should show an alert or a warning saying its already in use. when i come out of that jsp page then if others access the URL it should navigate to that page. Any idea how to achieve that in java? Am using spring framework 3.1. it is a web aplication. Can i handle it in javascript?
you can create a static counter.
you can also set a key/value pair on the first request and your code decides to show alert based on the value of the key.
To make that work you need couple of things:
1. create a static lock, using concurrnet.util library objects
2. when a user get to the jsp, try to lock. if no success - alert, if sucess - just show the page
3. when suer holding the lock leave the page, unlock, making any subsequent request to compete for the lock again.
Better off, just for security define that there will be only one instance of the servlet created from the JSP. That is a configuration on the web server.
I have a Java web application which stores some data in the session. The data in the session changes as the user interacts with the application (e.g. flow is managed by a controller, each controller has several form pages, on each form page some data is updated in the session and flow goes to the next form page).
The problem is that some users are opening more than one tab to the application, each tab with a different step in the flow. At this point data in the session is messed up since the tabs share the same session (app uses cookie managed sessions).
Telling the users to use different browsers to avoid sharing the same session id (e.g. one Firefox window and one IE window) is not an option since surely at some point somebody will forget to do this and instead use tabs, thus messing up their data.
Adding some verifications that detect that another flow is requested from another tab and display a message to the user saying this is not allowed is not an option either since it pisses of the users and we don't want that do we? :D
The fact is that using another tab is useful for the users because they are more efficient in what they use the application for, so I am keeping this option. But the question now is how best to manage the one session data for the more tabs?
What I thought of, was to have the controller generate a token when it starts the flow and pass this token to each form page which in turn sends it back to identify itself. If another tab requests the same controller action when there is an ongoing flow then generate another token and pass that around.
Basically, I want each flow to have a token and inside the session I won't just keep one set of data but have a set of data for each token and then match requests based on the token.
Now the problem is that this approach will need a lot of rewritings to the application and I was wondering if there is a best practice for managing such a situation or can someone suggest other approaches. I am open to ideas.
Have you encountered this situation? How did you handle it?
This is usually done by assigning a windowId for each tab/window and passing it on each request. Jsf supports this via orchestra. Spring mvc will support it in the next version.
I recently needed this for a simple case, so I implemented it myself. Took half an hour. However, my scope was very limited:
pass a windowId with each request, and return it back for the next request. The first time - generate it.
for any attribute you want to store in the session, put a Map<String, Object> where the key is the windowId
This is exactly what Seam was created to handle. In Seam there's a concept called a Conversation which basically does exactly what you are explaining. Conversations are basically are a way to divide the Session into many pieces that can expire at some timeout. You can look at the source code for org.jboss.seam.core.Manager class to see how it's actually implemented and get inspired ;)
Depending on the complexity of your application, you may want to investigate implementing tabs within your application. This gives you wholesale control over the flow, while still providing users with the functionality they want. I'd argue it's, bugwise, the most robust solution, since you won't have a dependency on the way the browser handles sessions, minimising the number of "known unknowns".
Of course, there'll be potentially a large upfront cost to this, depending on how your application is structured. Without more information about your app, you're the best placed person to decide.
You can also try to wrap your application inside Adobe Air
And then limit your web application to be only accessable from this air. By doing this you dont need to consider the web browser fragmentation and their unique behaviour.