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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Assigning a value to a JSP variable from a Script

Status
Not open for further replies.

blueindian1

Programmer
Apr 24, 2001
150
0
0
US
Hi,

Is it possible to assign a value to a JSP variable with a script? I want to do something like the following:

Code:
<% String str = &quot;jsp&quot;; %>

<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
<!--
if (document.all) {
  <%str = &quot;modifed_by_the_script&quot;; // assignment only when condition is met%>
  alert(&quot;Internet Explorer Detected&quot;);
}
// -->
</SCRIPT>

The way I have it, the string has a new value assigned regardless of whether the &quot;if&quot; condition is met

Thanks,

Rich
 
Nope, you can't really.

Can't think why you'd want to though? For browser detection, try:
Code:
<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
<!--
n = (document.all) ? 0 : 1;
if (n) {
  alert(&quot;This is Netscape!&quot;);
} else {
  alert(&quot;This is IE!&quot;);
}
//-->
</SCRIPT>
The JSP engine ignores any conditional operators used in your JavaScript code so you can't embed any JSP scriptlets between the if statement for example. Take the following:
Code:
<% String str; %>
<script>
if (xyz) {
  <% str = &quot;Test 1&quot;; %>
} else {
  <% str = &quot;Test 2&quot;; %>
}
</script>
Will always set str to &quot;Test 2&quot; in your compiled JSP.

If you really need this kind of functionality, you may consider using Resin-JavaScript from Caucho.com -
Hope this helps,

Tim
--
Tim <tim@planetedge.co.uk>
 
thanks!

the reason that i need to do it is because i need to know the browser type on the first page of my site after login. i can't use the login page to get it b/c the back end for the login process in a black box provided by i2, so i don't have access to that code. i would like to get it it without having to post the first page immediatily after a user logs in ...but it looks like that is what i will have to do.
 
one more thing...

i need to render different JSP depending on the browser...which is why I need for the JSP to know the browser...not just the Script.

:) thanks again
 
Well, you can get the browser type by simply parsing the string 'ua' in the following example:
Code:
<%

String ua = request.getHeader(&quot;user-agent&quot;);

out.println(ua);

%>

Based on whether 'ua' contains 'MSIE' or 'NAV' will tell you the browser type.

I suggest you use <jsp:forward> or <jsp:include> to display the appropriate HTML based on the browser type.

HTH,

Tim
--
Tim <tim@planetedge.co.uk>
 
Tim,

You are the man! I can't believe it's so easy....I've been looking forever!

Thanks,

Rich
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top