How to lock and Unlock a URL in java - java

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.

Related

How can i detect if a user refreshes a page in JSP?

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

Form with submitting url and revealling discountcode

I want to make a custom 404 error page.
Where customers, if they come on this page, have the posibility to send the broken url to us and when they have sended to us they see a discountcode.
So basicly i need a submit button that send the current url of the page, and shows a specific code afterward, but without reloading the page.
Who can help me with this?
Basically, you'll need three things to do this:
1) A custom 404 URL that will point to your 404.html page. This is configured based on your webserver-specific configuration, so you'll need to make some changes to point to this page.
2) An Ajax call on your 404 page so that you can make a request without reloading the page.
3) A server-side code generator that can generate and return a unique one-time-use discount code.
The Ajax is pretty easy to do. There are a number of different configuration / frameworks online that allow you to make a request without loading the page. At their core, they usually create a hidden IFrame DOM element with a form inside of it, and submit that form, then close the IFrame. You'll need to tie into this from the parent frame, having the form that submits get sent back the generated discount code from the server. You can then access this data from the parent frame, and display it to the user (probably using JavaScript).
One KEY piece of information to this is that your user could just type in some garbage (ex. http://yourdomainurlhere/oihoaiwhegaweg) and be redirected to a 404 error, and get a discount code. Once this knowledge becomes commonplace, people will always do that to get a discount code; thereby invalidating the reasoning you have for wanting to get the broken URL (in order to fix your site).
You can probably try to get around this by checking for referral links, etc., to try to determine if they actually clicked on a link on your site, or if they just arbitrarily typed in a URL. But this will take much more analysis and possible changes to your entire site to track URL clicking in order to generate the invalid URL.

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

prevent crossjumping in struts web application

How can I prevent a user from neglecting the normal work flow of a struts application? For example, I have a list of products. This products have options like view edit whatever. If you click on edit, you will be redirected to a page like /editProductInfo.do and there will be no problem if you go there from List and then click edit a bean will be created with the information of the requested product from database.
Now if you are not logged in and try to access this page /editProductInfo.do you will be asked to log in and if you do so there will be a NullPointerException because the Bean that is needed to fill the form of this /editProductInfo.do is not initialized or even created because the application does not even know which product was requested.
So I want to prevent Users from crossjumping from side to side which has no link to each other. Is this possible in Struts?
Thanks for advice :)
Ah you mention struts 1 :) We successfully used the Struts Workflow Extension for exactly that case. http://www.livinglogic.de/Struts/ It lets you define page flows and lets you react if users atempt to break these flows by reloading, entering URLs, using invalid bookmarks...
Encode the product in the URI; that way you beat both your problem and the problem of two open tabs/windows on different products...

Clear HTTP Session

I am trying to clear everything on my HTML form when I visit it from a hyperlink entry.
Any ideas? My development language is java.
are you using session-scoped data? if so, close your browser and open it again.
I'm not sure the application is, but one way to accomplish this would be to use JavaScript. For example, if it is acceptable to clear the form every time that page is visited you could write a quick function that clears the form when the page is loaded (i.e., using the onload event).
If you only want to clear the form when the page is hit from that link you could add a param to the URL (e.g., clearForm=true) and use JavaScript to pick up the query string and clear the form when that parameter is present.
This is, of course, a purely client-side solution. For a server-side solution it would be helpful to know what framework you are using.

Categories