You are here

WebProxy - Passing The Attributes to JavaScript

Submitted by peter.aganyo on Tue, 12/30/2008 - 16:27

I wonder if anyone has been able to pass iPerson attributes successfully to plain old HTML+JavaScript(with no server-side scripting.) I have been using JSP to pass the values as follows:

		uid = request.getParameter("uid");
		firstname = request.getParameter("firstName");
		lastname = request.getParameter("lastName");
		email = request.getParameter("pdsEmail");
		fullname = request.getParameter("commonName");
		String[] AN = 
                request.getParameterValues("pdsExternalSystemID");

However, I would like to compare performance between my JSP implementation and HTML+JavaScript (if this is at all possilbe).

Here is how I would use JavaScript if (and only if) the parameters were being passed as a query string:

	if(location.href.indexOf('?') != -1)
	{
		var myQS = location.href.split('?')[1];
		var myVP = myQS.split('&');
		var iPsn = new Object();
		for(var p=0; p<myVP.length; p++)
		{
			eval('iPsn.'+myVP[p].split("=")[0]+
                        '="'+myVP[p].split("=")[1]+'"');
		}
	}
	else
	{
		alert("No Query String Supplied!");
	}

Then I could use the values in JavaScript freely in my HTML page, for instance, iPsn.uid would give the the uid of the person logged in, iPsn.firstname would give me the first name and so on. But the parameters do not seem to come through as a query string; if they do how can I access them?

Any one out there??

Thank you.

Luminis Version:

There are two hurdles, both of which I'm unfamiliar with:
1. jsp is server-side. javascript is client-side. I'm not sure that the iperson classes and so forth would be able to be called client-side.
2. You would need a way to call java inside javascript. I found a couple links about that.

http://www.rgagnon.com/javadetails/java-0170.html
shows someone calling java from within a javascript.

I also found this http://www.icesoft.com/developer_guides/icebrowser/htmlguide/javascript4...

Which says "You can access Java objects from JavaScript in a Web page using the LiveConnect API for JavaScript."

Good luck.

I'm not exactly sure if this is what you are looking for, but there are two ideas here based on what you want to do. If you just want javascript to store the data, then (since JSP is server side) just output the variables (escaped as necessary) into a normal script tag. There's no harm having a server-side language output variable in a client-side script.

If those variables will be changing once they are initially loaded on the page, you may want to look at AJAX. This would allow your javascript to request data from another script (i.e. a jsp script) and store the results in a javascript variable.

Thanks Jason and rbalara.

I overlooked to point out that in our current implementation, after we use JSP to obtain the values (of the attributes) POSTed we pass them onto JavaScript inside a Script tag such as var username = "<%= uid %>";

Since the iPerson attributes are POSTed, only a Server-Side language can access the values; JavaScript can only access GETed values.

This means that we are stuck with JSP. We managed to write a JSP script that checks for a valid session and then obtains the required attributes. In the JSP page we write output statements that output JavaScript such as %>var username = "<%= uid %>";var firstname = "<%= firstName %>"; ... .. We then call this JSP in JavaScript using AJAX and do an eval() of the responseText - eval(trim(ajax(JSP).responseText)). It seems to be working great.

Thank you both for your suggestions.