Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

converting Java strings to JavaScript strings

2,990 views
Skip to first unread message

Peter Michaux

unread,
Jan 19, 2008, 4:48:30 PM1/19/08
to dev-tech-js-...@lists.mozilla.org
Hi,

I'm experimenting with server-side JavaScript using Rhino. I have
written some bits of JavaScript that wrap Java objects. One example is
a HttpServletRequest. If I have a request object and do

var path = request.getPathInfo();

the path variable seems to have a java.lang.String value. When I try
to call any of the JavaScript string methods I get an error. It seems
I need to perform some type conversion or wrapping. What I've been
doing just to get by is

var path = request.getPathInfo() + '';

This seems non-optimal. What is the best way to make this type
conversion or wrap the java.lang.String?

I've tried exposing Context.javaToJS as a global function in my
"shell" with the following code

public static Object javaToJS(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
{
return Context.javaToJS(args[0], getTopLevelScope(thisObj));
}


When I call this function in JavaScript with a Java string argument
the returned result is not a JavaScript string.

What to do?

Thanks,
Peter

Norris Boyd

unread,
Jan 19, 2008, 7:19:12 PM1/19/08
to

Easiest is to call String(), like String(request.getPathInfo()).

--N

darrel karisch

unread,
Jan 29, 2008, 3:50:15 PM1/29/08
to
look at PrimitiveWrapFactory for the behavior that you're looking for
0 new messages