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.
Related
How would i go about checking if a person is refreshing a jsp page? Could we make it for example that every time we reload a page we would first check if this happens and then we check with System.out.print().
Hmm, first System.out.print is of little use in a JSP because it will never reach the client. At best it will end in the logs, at worst it is lost.
Then in simple cases you can trust the HTTP Referer header that is supposed to give the previous page url. But it is an unreliable way because it may not be transmitted by the browser. A more reliable way is to use a session variable to store the current page (you could use a filter to set it after a request is being processed). If the requested page is the current page, then it is being refreshed.
In complex use cases (AJAX requests) you should first define what is a page, because not every request will constitute a page: some will call a page while some only ask data. But once this is clear, you can apply the previous way: if the user asks for a page which is the current page (as stored in session), then the page is being reloaded.
If you want to do something every time a page load. Then you can use simple javascript for that. Try this
window.onload=function()
{
dosomething();
console.log("Page is loaded");
}
You can learn about it from here.
https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onload
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 jsp page -- let's call it index.jsp. The user clicks on a link in this page -- let's call the link section1.jsp. Inside section1.jsp, there exists a form. When the user submits the form, it opens up another page called portal.aspx.
When the user clicks the back button in the browser, I want the page to go back to index.jsp.
My idea is this: Create a session from request.getHeader('referer') to record the index.jsp page. This will get set when the user enters section1.jsp. The idea is when the user is in portal.aspx and clicks the back button, I want it to check the session variable and, if it is set, redirect to the referer URL. Then I tried it and it didn't work.
Is there some way to accomplish what I just described, without the browser recording secton1.jsp in its history, such that the user will go to index.jsp from portal.aspx, when said user clicks the browser's back button? Please understand I'm looking for a server-side solution and I must use a form to open the portal.
Thank you for any help.
If you are going pure server side, there is no solution.
You may as an alternative use a cookie to store the return URL, this will ensure it doesn't get lost should your page flow change in the future. only issue, now your two back ends need to shate the same domain.
I would personally go with a little of frontend help, these are really API JS api to handle page history.
see https://github.com/browserstate/history.js