Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

javascript -> html -> jsp

Status
Not open for further replies.

zeero

Programmer
Aug 4, 2004
60
0
0
US
Hi all, I'm trying to pass a string from a javascript function into jsp all on the same page. I'm not sure how to go about this. Would it be something like?

Code:
<html>
<head>
<form name=frm>
<input type="hidden" name="variable" value="???">

<script type="text/javascript">
function myfunction()
{
 //statements here
}
</head>
<body onLoad="myfunction()">
<%
String var = request.getParameter("variable");
...
%>
</form>
</body>
</html>

I'm not sure what to do
 
remember that javascript executes client side, but JSP/Java executes server side, so you can get variables from JSP tp Javascript like so :

<script ...>
var abc = '<%= someValue %>';
</script>

but you cannot *pass* Javascript values to Java/JSP ... because the JSP has been transformed to HTML before the javascript executes.

--------------------------------------------------
Free Database Connection Pooling Software
 
thank you sedj, you're always of good help. One last question for you regarding this topic however. I have some old code that I have to write from. There's 2 files, an html and a jsp. In the html it has:

Code:
<HTML>
<HEAD>

<form name=frm>
<input type="hidden" name="addr" value="<%= path %>">

</HEAD>

<script language="javascript">

function adr_register()

{
	document.frm.addr.value = getAddressFunction();

        var url = "update.jsp";

        document.forms[0].method = "POST";

        document.forms[0].action = url;

        document.forms[0].submit();
}
</script>
<BODY>
...

and so the update.jsp it's passed to just gets it like this:
Code:
String getaddr = request.getParameter("addr");

so I guess my question is, how to simulate how these 2 files are working all into one jsp page only. Also, the value <% path %> in the html hidden form, I have no idea where that's coming from since there aren't include files associated. It doesn't make any since but it works somehow.
 
zeero :

That HTML page is quite badly written, see my rewrite which is a bit better.

Where "path" comes from though I have no idea - the page will not compile if it is a ".jsp" page - I expect it is OK when saved as a ".html" file because tomcat will not parse it.

You also will get Javascript errors because the function getAddressFunction() is not declared in that page, nor are any other ".js" files included.

Code:
<% 
	String getaddr = request.getParameter("addr");
%>

<html>
  <head>
	<script language="javascript">

	   function adr_register() {
		document.frm.addr.value = getAddressFunction();
		var url = "update.jsp";
		document.forms[0].method = "POST";
		document.forms[0].action = url;
		document.forms[0].submit();
	   }
	</script>
  </head>
  <body>

	
	<%
		if (getaddr != null && getaddr.length() != 0) {
	%>
			The value fo getaddr is <%= getaddr %>
	<%
		} else {
	%>

			<form name="frm">
				<input type="hidden" name="addr" value="">
			</form>	
	<%
		}
	
	%>
	
	
  </body>
</html>

--------------------------------------------------
Free Database Connection Pooling Software
 
thanks sedj, yeah the getAddressFunction() is just a dummy variable I wrote for posting purposes only, it's really a custom function that will be declared after I re-write it.
 
By the way, zeero, although you cannot pass a JavaScript variable to the JSP that generated the script, you can use IFRAMEs on a page as housings for JSPs.

You can either send the JavaScript variable in a URL parameter:

document.frames['myIframe'].location = "doSomething.jsp?var="+value;

...or, if the initial SRC of the IFRAME is a form, you can set a form element variable to whatever you like and then submit that form so that it does something.

In both cases, the document in the IFRAME can then "return" a value to the parent using JavaScript. You can thereby reach up and call a function or fill the innerHTML of a DIV, etc.

This still pretty much means having a second file (something to run in the IFRAME). I don't know why you're trying to condense to one file.

--Dave
 
I need all this done on one jsp page because this is a welcome screen that pulls a username in a database and displays it on a screen, it uses a javascript function to get a Mac address and matches the mac address with the room number to know what name to pull from.
so this code:

Code:
        document.frm.addr.value = getAddressFunction();
        var url = "update.jsp";
        document.forms[0].method = "POST";
        document.forms[0].action = url;
        document.forms[0].submit();

can't be used. I was trying to do something like:

Code:
<BODY onload="getAddressFunction()">
but I need the address string to check with the room number. Sorry, I'm just confused how to go about this.
 
You can still use the IFRAME method.

On the welcome screen, the IFRAME tags would contain the attribute: width="0%" so it won't be seen.

Code:
<iframe id='addressFrame' width='0%'></iframe>

In the JavaScript, in a function triggered from the BODY tag's ONLOAD event, you can grab the identifying information you need, then send it to the IFRAME:

Code:
//assuming the variables 'macAddress' and 'roomNo' already have values from your JavaScript
document.frames['addressFrame'].location = 'getName.jsp?mac='+macAddress+'&room='+roomNo;

Then, in your JSP (getName.jsp), do whatever data pull you need, and then build a mini web page with script that does whatever you need it to do. For example:

Code:
<!--after the data pull...-->
<html>
<head>
<script>
parent.callFunction('<%=userName%>'); //for example
//You could also do something like:
// parent.document.divId.innerHTML = '<%=userName%>';
</script>
</head>
</html>

I don't know if I'm being entirely clear. Do you follow what I'm trying to suggest here?

--Dave
 
but could I do all this in the getName.jsp? I guess what I mean is that getName.jsp is the actual welcome screen where everything needs to be done. When the page is brought up, it is supposed to have their first name in the corner, that name is pulled from a database since it's a jsp page. What makes it unique is identifying with the mac address which is a custom function that I can only call with a javascript function. Basically I know how to pull from the DB and display on the screen, and i know how to check and match the query data, etc. the problem im having is getting the mac adress as soon as the page is loaded, taken it as a string, matching with the database, then pulling the correct record from the db to be displayed. In fact, I'm sure I'm going about this in a bad way, I'm just limited on getting that mac address because it's only a js function.
 
Are you talking about a MAC address as in the ethernet card's unique id ?

You can't be, so I will say this : why must this "mac address" be resolved using javascript - surely you can do it serverside.

There must be something you're not telling us.

--------------------------------------------------
Free Database Connection Pooling Software
 
zeeero,

using my method, the page would load, the JavaScript would grab the mac address, the iframe would secretly run a data pull to get the user name and then use JavaScript to populate the innerHTML of a DIV on the welcome screen and, to the user, it would look like the page opened with his/her name on it. At least, it should be about that fast.

--Dave
 
hey guys!

can i use <jsp:forward...> inside a JavaScript?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top