xenia27
asked on
script value (ScriptEngine)
I try to get some information in my script from my java program by using ScriptEngineManager. Why values of the variables I would love to get were null? How can I get a variable's value from the script?
My script...
function googleMapCal(lat, lng, city, region, country)
{
var ClientLoc = google.loader.ClientLocation;
drawMap(ClientLoc);
lat = ClientLoc.latitude;
lng = ClientLoc.Longitude;
city = ClientLoc.address.city;
region = ClientLoc.address.region;
country = ClientLoc.address.country;
}
function drawMap(ClientLoc) {
if (google.loader.ClientLocation) {
var cl = google.loader.ClientLocation;
var location = [cl.address.city, cl.address.region, cl.address.country].join(', ');
ClientLoc = cl;
} else {
document.getElementById('cantfindyou').innerHTML = "Crap, I don't know. Good hiding!";
}
}
Where I try to get values....
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
Bindings vars = new SimpleBindings();
// Run Javascript
try{
engine.eval("function googleMapCal(lat, lng, city, region, country)" +
"{" +
"var ClientLoc = google.loader.ClientLocation;" +
"drawMap(ClientLoc);" +
"lat = ClientLoc.latitude;" +
"lng = ClientLoc.Longitude;" +
"city = ClientLoc.address.city;" +
"region = ClientLoc.address.region;" +
"country = ClientLoc.address.country;" +
"}", vars);
} catch (ScriptException ex){
ex.printStackTrace();
}
// Get JavaScript Variables
lat = (String)vars.get("lat");
lng = (String)vars.get("lng");
city = (String)vars.get("city");
country = (String)vars.get("country");
region = (String)vars.get("region");
System.out.println("You are from " + lat + "/" + lng + "/" + city + "/" + country + "/" + region);
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I got an error message "Source not found"...
What should I do? Did I define something wrong?
What should I do? Did I define something wrong?
Can you post your complete Java-main-Funktion and the complete error message.
ASKER
When I tried to run in debug mode, I got an error message pops up said I missed ";" somewhere in my file. Then, I tried to run the function where calls javascript function and I got..
2009/7/2
H 08:12:23 com.google.appengine.tools .developme nt.ApiProx yLocalImpl log
SEVERE: [1246522343671000] javax.servlet.ServletConte xt log: Exception while dispatching incoming RPC call
com.google.gwt.user.server .rpc.Unexp ectedExcep tion: Service method 'public abstract java.lang.String com.unet.googleproject.cli ent.Greeti ngService. greetServe r(java.lan g.String)' threw an unexpected exception: java.lang.NullPointerExcep tion
at com.google.gwt.user.server .rpc.RPC.e ncodeRespo nseForFail ure(RPC.ja va:360)
at com.google.gwt.user.server .rpc.RPC.i nvokeAndEn codeRespon se(RPC.jav a:546)
at com.google.gwt.user.server .rpc.Remot eServiceSe rvlet.proc essCall(Re moteServic eServlet.j ava:166)
at com.google.gwt.user.server .rpc.Remot eServiceSe rvlet.doPo st(RemoteS erviceServ let.java:8 6)
at javax.servlet.http.HttpSer vlet.servi ce(HttpSer vlet.java: 713)
at javax.servlet.http.HttpSer vlet.servi ce(HttpSer vlet.java: 806)
at org.mortbay.jetty.servlet. ServletHol der.handle (ServletHo lder.java: 487)
at org.mortbay.jetty.servlet. ServletHan dler$Cache dChain.doF ilter(Serv letHandler .java:1093 )
at com.google.apphosting.util s.servlet. Transactio nCleanupFi lter.doFil ter(Transa ctionClean upFilter.j ava:43)
at org.mortbay.jetty.servlet. ServletHan dler$Cache dChain.doF ilter(Serv letHandler .java:1084 )
at org.mortbay.jetty.servlet. ServletHan dler.handl e(ServletH andler.jav a:360)
at org.mortbay.jetty.security .SecurityH andler.han dle(Securi tyHandler. java:216)
at org.mortbay.jetty.servlet. SessionHan dler.handl e(SessionH andler.jav a:181)
at org.mortbay.jetty.handler. ContextHan dler.handl e(ContextH andler.jav a:712)
at org.mortbay.jetty.webapp.W ebAppConte xt.handle( WebAppCont ext.java:4 05)
at com.google.apphosting.util s.jetty.De vAppEngine WebAppCont ext.handle (DevAppEng ineWebAppC ontext.jav a:54)
at org.mortbay.jetty.handler. HandlerWra pper.handl e(HandlerW rapper.jav a:139)
at com.google.appengine.tools .developme nt.JettyCo ntainerSer vice$ApiPr oxyHandler .handle(Je ttyContain erService. java:306)
at org.mortbay.jetty.handler. HandlerWra pper.handl e(HandlerW rapper.jav a:139)
at org.mortbay.jetty.Server.h andle(Serv er.java:31 3)
at org.mortbay.jetty.HttpConn ection.han dleRequest (HttpConne ction.java :506)
at org.mortbay.jetty.HttpConn ection$Req uestHandle r.content( HttpConnec tion.java: 844)
at org.mortbay.jetty.HttpPars er.parseNe xt(HttpPar ser.java:6 44)
at org.mortbay.jetty.HttpPars er.parseAv ailable(Ht tpParser.j ava:205)
at org.mortbay.jetty.HttpConn ection.han dle(HttpCo nnection.j ava:381)
at org.mortbay.io.nio.SelectC hannelEndP oint.run(S electChann elEndPoint .java:396)
at org.mortbay.thread.Bounded ThreadPool $PoolThrea d.run(Boun dedThreadP ool.java:4 42)
Caused by: java.lang.NullPointerExcep tion
at com.unet.googleproject.ser ver.Greeti ngServiceI mpl.GetCli ent(Greeti ngServiceI mpl.java:4 8)
at com.unet.googleproject.ser ver.Greeti ngServiceI mpl.greetS erver(Gree tingServic eImpl.java :28)
at sun.reflect.NativeMethodAc cessorImpl .invoke0(N ative Method)
at sun.reflect.NativeMethodAc cessorImpl .invoke(Un known Source)
at sun.reflect.DelegatingMeth odAccessor Impl.invok e(Unknown Source)
at java.lang.reflect.Method.i nvoke(Unkn own Source)
at com.google.gwt.user.server .rpc.RPC.i nvokeAndEn codeRespon se(RPC.jav a:527)
... 25 more
2009/7/2
H 08:12:23 com.google.appengine.tools
SEVERE: [1246522343671000] javax.servlet.ServletConte
com.google.gwt.user.server
at com.google.gwt.user.server
at com.google.gwt.user.server
at com.google.gwt.user.server
at com.google.gwt.user.server
at javax.servlet.http.HttpSer
at javax.servlet.http.HttpSer
at org.mortbay.jetty.servlet.
at org.mortbay.jetty.servlet.
at com.google.apphosting.util
at org.mortbay.jetty.servlet.
at org.mortbay.jetty.servlet.
at org.mortbay.jetty.security
at org.mortbay.jetty.servlet.
at org.mortbay.jetty.handler.
at org.mortbay.jetty.webapp.W
at com.google.apphosting.util
at org.mortbay.jetty.handler.
at com.google.appengine.tools
at org.mortbay.jetty.handler.
at org.mortbay.jetty.Server.h
at org.mortbay.jetty.HttpConn
at org.mortbay.jetty.HttpConn
at org.mortbay.jetty.HttpPars
at org.mortbay.jetty.HttpPars
at org.mortbay.jetty.HttpConn
at org.mortbay.io.nio.SelectC
at org.mortbay.thread.Bounded
Caused by: java.lang.NullPointerExcep
at com.unet.googleproject.ser
at com.unet.googleproject.ser
at sun.reflect.NativeMethodAc
at sun.reflect.NativeMethodAc
at sun.reflect.DelegatingMeth
at java.lang.reflect.Method.i
at com.google.gwt.user.server
... 25 more
this is where I try to call the javascript function...
public class GreetingServiceImpl extends RemoteServiceServlet implements
GreetingService {
public String greetServer(String input) {
String serverInfo = getServletContext().getServerInfo();
String userAgent = getThreadLocalRequest().getHeader("User-Agent");
Locale clientRequest = getThreadLocalRequest().getLocale();
String clientHostAddr = getThreadLocalRequest().getRemoteAddr();
String lat = new String();
String lng = new String();
String city = new String();
String country = new String();
String region = new String();
GetClient(lat, lng, city, country, region);
return "Hello, " + input + "!<br><br>I am running " + serverInfo
+ ".<br><br>It looks like you are using:<br>" + userAgent
+ ".<br><br>You are from:<br>" + city + "/" + country + " " + region
+ ".<br><br>Your Lat/Lng:<br>" + lat + "/" + lng;
}
public void GetClient(String lat, String lng, String city, String country, String region)
{
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("javascript");
Bindings vars = new SimpleBindings();
Invocable invocableEngine = (Invocable)engine;
// Run Javascript
try{
engine.eval("function getInfo(Lat){"
+ " getLat(Lat); } ", vars
);
lat = vars.get("Lat").toString();
System.out.println("Lat: " + lat);
} catch (ScriptException ex){
ex.printStackTrace();
}
}
}
this is where I have my javascript...in a html file...
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<!-- The HTML 4.01 Transitional DOCTYPE declaration-->
<!-- above set at the top of the file will set -->
<!-- the browser's rendering engine into -->
<!-- "Quirks Mode". Replacing this declaration -->
<!-- with a "Standards Mode" doctype is supported, -->
<!-- but may lead to some differences in layout. -->
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!-- -->
<!-- Consider inlining CSS to reduce the number of requested files -->
<!-- -->
<link type="text/css" rel="stylesheet" href="xxx.css">
<!-- -->
<!-- Any title is fine -->
<!-- -->
<title>Web Application Starter Project</title>
<!-- -->
<!-- This script loads your compiled module. -->
<!-- If you add any GWT meta tags, they must -->
<!-- be added before this line. -->
<!-- -->
<script type="text/javascript" language="javascript" src="xxx/xxxt.nocache.js"></script>
<style type="text/css">
body {
padding: 0;
margin: 0;
background-color: #4f4e45;
font-family: Tahoma, Arial, sans-serif;
}
#header {
height: 30px;
background-color: #111;
color: #EEE;
font-size: 10pt;
padding-top: 4px;
border-bottom: 1px solid #333;
}
#main {
margin: 20px auto;
width: 610px;
}
#main h2 {
font-weight: normal;
margin: 0;
color: #ff9600;
font-size: 16pt;
font-family: Tahoma, Arial, sans-serif;
padding-bottom: 1em;
}
a {
color: #fff;
}
#cantfindyou {
font-size: 18pt;
}
#map {
display: none;
height: 300px;
border: 1px solid black;
margin: 10px 4px;
}
</style>
</head>
<!-- -->
<!-- The body can have arbitrary html, or -->
<!-- you can leave the body empty if you want -->
<!-- to create a completely dynamic UI. -->
<!-- -->
<body>
<!-- OPTIONAL: include this if you want history support -->
<iframe src="javascript:''" id="__gwt_historyFrame" tabIndex='-1' style="position:absolute;width:0;height:0;border:0"></iframe>
<h1>Web Application Starter Project</h1>
<table align="center">
<tr>
<td colspan="2" style="font-weight:bold;">Please enter your name:</td>
</tr>
<tr>
<td id="nameFieldContainer"></td>
<td id="sendButtonContainer"></td>
</tr>
</table>
<script type="text/javascript" src="http://www.google.com/jsapi?key=ABQIAAAAJjoQAkGmq5qsa0PKk9Y6nxQdF2TG78juI7gyCxAZXlcasg4lQxTlaYHDB6IKUESWIPTqiJvXPeiwhg"></script>
<script type="text/javascript">
functionn getInfo(Lat)
{
getLat(Lat);
}
function getLat(Lat)
{
if (google.loader.ClientLocation) {
var cl = google.loader.ClientLocation;
var location = [cl.address.city, cl.address.region, cl.address.country].join(', ');
}
Lat = cl.latitude;
}
</script>
</body>
</html>
ASKER
Any help?
Is there any example which contains both javascript and java examples? All I can find is examples in java about how I can use ScriptEngineManager.
By the way, I try to create a web application with google wizard and eclipse. Please help me~
Is there any example which contains both javascript and java examples? All I can find is examples in java about how I can use ScriptEngineManager.
By the way, I try to create a web application with google wizard and eclipse. Please help me~
ASKER