Scope of object in JSP - java

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.

Related

Spring mvc posting data to controller

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.

Keep alive applet object even page is reloaded?

I in position to hold my applet object in my HTML page even page is refreshed or redirected to any other page, Because of the object will use to get the value that are loaded initially in applet, so i have tried to store the object in jQuery session variable, but i couldn't do this, so please any one help me to save the object in a jQuery session variable or please suggest me the way reach my requirement..
Mo, it is not possible, since it goes against how an applet is supposed to operate. Store the relevant data in cookies and have the applet check for the cookies first.

Keep object througe pages

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

How to generate unique links in Wicket application?

Our Wicket 1.5 application has lots of links for different tasks. We have a requirement that links our application generates have to be unique (and non-bookmarable).
How can we achieve this?
By default every link is not bookmarkable as it contains the Page Version Id and is tied to a specific session.
Edit: Bookmarkable Links and Stateless Pages beeing the exceptions of this rule. Don't use them and you're safe.
2nd Edit: To make sure, there are no valid URLs from another session that work in a current session, you could add some unique value to your links by using or implementing a MountMapper as generally described here. This mapper could include a variable from the current websession as a parameter and check for that value when reading an URL...

portlet class variable

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.

Categories