Sharing data between pre-existing data in a JSP and the GWT module

22 views
Skip to first unread message

Suri

unread,
Nov 19, 2008, 11:40:00 AM11/19/08
to Google Web Toolkit
Hi,
This must've been asked a million times and I tried looking but the
only answer I got was JSNI in most cases. I just want to be sure its
the only way. Assuming I was passing data to my JSP after my action
has completed. Lets say a value for a studentID.
Now in my GWT module I'll need this studentID so that I can then load
up information about that student using GWT code.
In the JSP I have 2 options

1.
<script language=javscript>
var studentID = <%=request.getAttribute("studentID")%>
</script>

OR
2.
<input type="hidden" value=<%=request.getAttribute("studentID")% />

In the case of (1) I see that we would basically HAVE to use JSNI to
be able to retrieve the value to GWT and then store it and use it.
My question is how if possible would the 2nd option implementable. Do
I access the DOM in some form from the GWT client code to be able to
access the value of the studentID (I assume before doing this I would
have had to have given the <input..> field an id attribute?)

Thanks for any input
Suri

Suri

unread,
Nov 19, 2008, 2:46:20 PM11/19/08
to Google Web Toolkit
As an update question as well,

Does anyone know if this is correct syntax and if not what would be
the correct way to go about this.
I'm trying to copy the value of my javascript variable set in the JSP
to the GWT client.


-------- JSP FILE --------------
<script language="javascript" type="text/javascript">

var someValue = <%= (String) request.getAttribute("someValue")
%>

</script>
------------------------------------ GWT ENTRY POINT CLIENT CLASS
---------------------

public class SomeClass
{
private String someValue;

public onModuleLoad()
{
this.someValue = getSomeValue;
}

public native String getSomeValue() /*- {
return someValue;
} -*/

}
----------------------------------------------------------------------------------------------------------------


Thanks.

dolc...@gmail.com

unread,
Nov 19, 2008, 3:03:19 PM11/19/08
to Google Web Toolkit
Suri,
I'm pretty sure you can just create the input and "adopt" the element
in GWT.

Suri

unread,
Nov 19, 2008, 3:29:03 PM11/19/08
to Google Web Toolkit
Hi there,
I'm not sure how you would do that. Please explain.
Thanks
Suri

olivier nouguier

unread,
Nov 20, 2008, 3:43:17 AM11/20/08
to Google-We...@googlegroups.com
public native String getSomeValue() /*- {
return $wnd.someValue.
}*-/;
But be aware of "undef" !!!
should be:
public native String getSomeValue() /*- {
if($wnd.someValue){
return $wnd.someValue.
}
return null;
}*-/;


More generic:

public static native String jnsiGetString(String jsVar, String
defaultValue)/*-{
var value = eval('$wnd.'+jsVar);
if(value){
return value;
}
return defaultValue;
}-*/;

and

public String getSomeValue(){
return jnsiGetString("someValue", null);
}

HIH
--
Si l'ignorance peut servir de consolation, elle n'en est pas moins illusoire.

Suri

unread,
Nov 20, 2008, 9:31:29 AM11/20/08
to Google Web Toolkit
Hi Olivier,
I actually saw your post later on another thread and used the default
solution. Thanks for that. Pretty neat.
A different question now, I observed that after obtaining the value I
wanted, I could not convert it to a Long type. i.e

in my client java code after retrieving the value using the native
method above I try something like

Long longVal = Long.parseLong("returnedValue");

And that seems to throw an error in the browser. I do remember reading
about some issues with Long datatype and javascript, however any
explanation is welcome. I'd like to understand it better.
Thanks
Suri



On Nov 20, 3:43 am, "olivier nouguier" <olivier.nougu...@gmail.com>
wrote:

olivier nouguier

unread,
Nov 20, 2008, 10:08:27 AM11/20/08
to Google-We...@googlegroups.com

Suri

unread,
Nov 20, 2008, 11:34:57 AM11/20/08
to Google Web Toolkit
Thanks Olivier. That makes sense. I'll have to use the String and
validate it at the server side I guess.

Suri

On Nov 20, 10:08 am, "olivier nouguier" <olivier.nougu...@gmail.com>
wrote:
> There is no long datatype in javascript !
>
> http://www.google.com/search?q=GWT+long+emulation&sourceid=navclient-...

Suri

unread,
Nov 20, 2008, 4:19:48 PM11/20/08
to Google Web Toolkit
Hi Olivier,
I just thought I'd let you know. The script you have works great
however needs one additional modification.

public static native String jnsiGetString(String jsVar, String
defaultValue)/*-{
var value = eval('$wnd.'+jsVar);
if(value){
return value.toString();
}
return defaultValue;
}-*/;

and

public String getSomeValue(){
return jnsiGetString("someValue", null);

}

Notice the .toString(). This is so that the value being returned is
truly converted to a String datatype. As part of doing some work I had
used the script but the variable that was being referenced, happened
to have a numeric value. Thus, when trying to do an RPC, the system
kept giving me a ClassCastException although I was sure I was passing
a String (it also passed the instanceof test) but once I put
the .toString() it worked correctly. I guess something funky happens
there no idea what. e.g
--- JSP-----------------
<script language="javascript" type="text/javascript">
// someValue happens to be an integer although its sent as a String
in Java e.g "123456"
var testVar = <%= request.getAttribute("someValue") %>
</script>
---------------------------

----- Client Code ------------

.......
String testValue = getString("testVar", "0");
// Although this executes correctly and works fine, it acts up later
in the code unless the .toString() method is called in the javascript
as shown before.
------------------------------------

Thanks

Suri
> > >> > --------JSPFILE --------------
> > >> > <script language="javascript" type="text/javascript">
>
> > >> >         var someValue = <%= (String) request.getAttribute("someValue")
> > >> > %>
>
> > >> > </script>
> > >> > ------------------------------------ GWT ENTRY POINT CLIENT CLASS
> > >> > ---------------------
>
> > >> > public class SomeClass
> > >> > {
> > >> >  private String someValue;
>
> > >> >  public onModuleLoad()
> > >> >  {
> > >> >    this.someValue = getSomeValue;
> > >> >  }
>
> > >> >  public native String getSomeValue() /*- {
> > >> >   return someValue;
> > >> >  } -*/
>
> > >> > }
> > >> > ----------------------------------------------------------------------------------------------------------------
>
> > >> > Thanks.
>
> > >> > On Nov 19, 11:40 am, Suri <sviswanad...@gmail.com> wrote:
> > >> >> Hi,
> > >> >> This must've been asked a million times and I tried looking but the
> > >> >> only answer I got was JSNI in most cases. I just want to be sure its
> > >> >> the only way. Assuming I was passing data to myJSPafter my action
> > >> >> has completed. Lets say a value for a studentID.
> > >> >> Now in my GWT module I'll need this studentID so that I can then load
> > >> >> up information about that student using GWT code.
> > >> >> In theJSPI have 2 options

olivier nouguier

unread,
Nov 21, 2008, 3:55:10 AM11/21/08
to Google-We...@googlegroups.com
Hi Suri,
Thanks for this tip !
But IMHO in your case I think that the problem is in the hosted page:
Try replacing:
var testVar = <%= request.getAttribute("someValue") %>
by:
var testVar = "<%= request.getAttribute("someValue") %>"

Notice the quote !
Then the generate js will be:
var testVar = "12345"; // a string
Instead of
var testVar = 12345; // an int

However your solution seems pretty to me and it's better check twice
those JS type.

Best regards
Olivier.

Suri

unread,
Nov 21, 2008, 9:24:24 AM11/21/08
to Google Web Toolkit
Aha. You're right too Olivier. Thanks for the tip. Tottally missed
that. Appreciate it.

On Nov 21, 3:55 am, "olivier nouguier" <olivier.nougu...@gmail.com>
wrote:

Vijay

unread,
Nov 26, 2008, 9:06:45 AM11/26/08
to Google Web Toolkit
I was searching for something like this for quite some time, thanks a
million !!!
Reply all
Reply to author
Forward
0 new messages