Wednesday, June 27, 2007

Javascript in Address bar

Recently i was asked to design a system which performs some operation.The technology used was jsp.The operation is , i have two jsp pages , first.jsp and second.jsp. 'first.jsp' has a form that takes input from the user.On submitting the form , it goes to 'second.jsp' where some database operation is carried on based on certain conditions.For example, partA of the database operation is carried on if the condition succeeds and partB is carried on if the condition fails.But the condition parameter is neither hardcoded nor supplied by the user through the GUI. The user need to add the condition parameter to the url in the address bar.

for example like ,

"http://www.mydomain.com/second.jsp?conditionparameter=parametervalue"

Since the url ("http://www.mydomain.com/second.jsp") has been already specified in the forms action attribute of the 'first.jsp',as well as the option of getting the condition parameter from the user through popup or alert or anykind of GUI is restricted , and passing the conditionparameter as the query string is the only way allowed i tried fetching the URL from the address bar through some javascript and jsp methods.

In the case of javascript , i tried using document.location and windows.location.href, windows.location.search etc. In the case of jsp , i tried httpservletrequest , response objects methods.But in both cases , i could not get the condition parameter that i specify in the address bar.

After a long head ache, i found the strength of java script. yes , i just typed the javascript in the address bar while being in the 'first.jsp' page.

This is the following code , i just typed in the address bar after cleaning the url already present there.

javascript:document.formname.action="http://www.mydomain.com/second.jsp?conditionparameter=parametervalue"
Thats it. Finally the form details along with the condition parameter has been caught in the 'second.jsp' and the task was completed successfully.

No comments: