请教:Applet和数据库连接,以及Applet和JSP页面之间的值传递如何实现呢?(在线等待) 当然不还是用request.getParameter("name")就可以了。要不就用Session来传值! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 能说具体点吗?最好能给个程序片断,谢谢了。APPLET中的变量也可以用request.getParameter("name")来获得吗? jsp 中加入<APPLET CODEBASE = "." CODE = "gt_fx.aplt_main.class" WIDTH = 595 HEIGHT = 350 HSPACE = 0 VSPACE = 0 ALIGN = top><param name= mapType value="1"></APPLET>java 中获得参数getParameter("mapType ", "1"); 和数据库相连,通过自己写javabean 那就有一个javabean和applet参数传递的问题啦,他们之间要怎么传递呢? <param name= mapType value="1">这句里面的value="1"什么意思呀? value="1" 表示mapType的值!你可以动态的定义它! 本文着重阐述了网页开发中,通过灵活使用从JavaScript语言中访问Java的方法、从JavaScript中访问JavaScript小程序的方法与变量,以及在Java Applet小程序中使用JavaScript等技术,实现这几种网页开发语言的互相补充,以开发更完美的Web应用程序。JavaScript是用于HTML环境的开发语言,提供了能够响应Web页面事件的脚本,可以完全访问浏览器窗口的各个方面,善于合并HTML、Java Applet小程序、插入件、服务器方程序和其他Web组件,形成完全集成的Web应用程序。而Java是功能强大的著名开发语言,能够支持Web应用程序的开发,用于开发高级Web对象,并提供实现嵌入Web页面的可执行内容,具有更强的编程功能,可以作为JavaScript功能的良好补充,只不过这些功能被限制在有限的浏览器窗口区中。Java与JavaScript可以互相补充,以开发功能更完美的Web应用程序。本文归纳了笔者编程中曾使用过的,涉及到有关JavaScript与Java、Java Applet小程序之间互访的问题的一些方法,仅供参考。一、从JavaScript中访问Java方法 在HTML脚本中可以用JavaScript直接调用Java中的方法,只需要在调用语句前用“java.lang.”加以表示即可。具体语句用法如下例所示:java.Lang.System.Out.println(“Hello!”)但此种方法只适用于Netscape Navigator,一般不提倡在Web应用程序中使用。二、从JavaScript中访问Java Applet小程序中的方法和变量通过JavaScript提供的Applet对象,JavaScript代码可以访问Java的变量和方法,具体步骤和用法示例如下。需要注意的是,JavaScript提供的applet对象具有name属性,而没有方法和事件处理器。步骤:1) 将要访问的Java Applet小程序的方法和变量声明为Public,且必须在Public类中声明;2) Java Applet小程序要先装入才能访问,尽管applet对象没有定义onLoad事件,但可以在HTML文档的〈body〉标志中,指定使用Window对象的onLoad事件处理器;3) Java Applet小程序可以用JavaScript的applet对象来访问。示例:“mytest.htm”<html><head><script language="JavaScript">function accessApplet(){ setTimeout("changeText('I like JavaScript!')",1000);setTimeout("changeText('I like JavaScript and Java!')",2000);setTimeout("changeText('I like Java!')",3000);setTimeout("accessApplet()",4000);}function changeText(s){window.document.testText.setText(s) //此处调用JavaApplet小程序的setText()方法//注意,小程序的名字必须为〈applet〉标志 name属性所标识}</script></head><body onload="accessApplet()"> //通过调用accessApplet()装入小程序<applet code="Testtext.class" name="testText" width=450 height=150></applet></body></html>“Testtext.java”import java.applet.*;……public class Testtext extends Applet{ ……public void setText(String s) //setText()必须声明为“public”{text=s;repaint();}}三、在Java Applet小程序中使用JavaScriptLive Connect提供了Java与JavaScript的接口,可以允许在Java Applet小程序中使用JavaScript。具体步骤如下:1) 在HTML脚本中使用〈APPLET〉标志的MAYSCRIPT属性,以允许Java Applet小程序访问脚本;2) 将netscape. JavaScript包导入Java Applet小程序;3) 在Java Applet小程序中用JSObject类的getWindow( )方法创建JavaScript窗口的句柄;4) 在Java Applet小程序中用JSObject类的getMember( )方法访问JavaScript对象;5) 在Java Applet小程序中用JSObject类的eval( )方法调用JavaScript方法。示例:“ReadForm. Java”import netscape.javascript.JSObject;import netscape.javascript.JSException; //可允许在小程序中处理异常事件……win=JSObject.getWindow(this); // 获取JavaScript窗口句柄,引用当前文档窗口doc=(JSObject)win.getMember("document"); // 访问JavaScript对象form=(JSObject)doc.getMember("textForm");textField=(JSObject)form.getMember("textField");text=(String) textField.getMember("value"); //获取文本区的值……win.eval("alert(\"This alert comes from Java!\")");// 调用JavaScript的alert()方法……“User.htm”……<BODY><form name="textForm"><P>Enter text and then click display text:<input type="text" name="textField" size="20"></P></FORM><applet code="ReadForm.class" width=200 height=100 name="readApp" MAYSCRIPT>//必须使用MAYSCRIPT属性标志</APPLET></BODY> 哦,这意思啊,呵呵。这样是让Applet获得了JSP页面中的变量了,如果要再把一些变量从Applet传回给JSP页面呢? to:虎子非常感谢,说的很详细呀,我仔细研究一下,呵呵。不过jsp和JavaScript还是有区别的,jsp中的方法有吗? 我是这样做的,用了3步。1. 在jsp内获得输入页面内的值的:<td><input type="test" name="valhot" size=20></td><tr><%try{hot=request.getParameter("valhot");//先获得字符串类型mesg="";if(hot==null){ mesg="不能为空!"; out.println(mesg); } else{ valhot=Double.valueOf(hot).doubleValue(); //获得双精度类型 }%><br>} catch(Exception e){e.printStackTrace();}2. 然后将获得数据传递到applet:<applet codebase = "." code = "project.classname.class" name = "TestApplet" width = "830" height = "410"><!--向applet传递参数--><Param name="valhot" value=<%=valhot%>>/></applet>3. 再在applet中取得数值:hot=getParameter("valhot"); //取的字符串类型valhot=Double.valueOf(hot).doubleValue(); //转化为双精度类型这样就可以利用数值在applet中进行计算了。 有高手发表意见吗?我想实现在jsp页面内循环计算数据,每循环一次,就把数据传递给applet一次,这样能实现吗?如果能,该如何实现,请帮忙!! 闄ら潪浣犵殑applet鏈夎冻澶熺殑鍙傛暟鍙橀噺锛屼緵寰幆鏃舵尐涓祴鍊硷紝 没有其他人回答了吗?怎么样再从applet把参数传回给jsp页面呀? 使用applet与servlet的HTTP隧道通信可以解决楼主的所有问题 再给你一份Applet与servlet的示例程序: String m_hostName=getCodeBase().getHost(); String m_servletName="http://"+m_hostName+":8080/birdWeb3S/vehicleservlet";//设置Servlet的路径 URL servletURL=new URL(m_servletName); HttpSendMessage m_message=new HttpSendMessage(servletURL);//初始化中间层 //Applet中的示范程序 public void AlarmSet(String strAlarmID,String strVehicleVin) { try { Properties pts=new Properties(); pts.put("AlarmSet","ok"); pts.put("AlarmID",strAlarmID); pts.put("VehicleVin",strVehicleVin); InputStream in=m_message.sendGetMessage(pts);//采用DoGet传递参数 InputStream in=m_message.sendPostMessage(pts);//采用DoPost传递参数 DataInputStream out=new DataInputStream(in);//接收Servlet返回的数据流 } catch(IOException ex) { } } //中间层用于Applet与Servlet之间的通讯 class HttpSendMessage extends Object{ URL Servlet=null; String args=null; public HttpSendMessage(URL Servlet)//中间层的构造函数 { this.Servlet=Servlet; } public InputStream sendGetMessage() throws IOException//DoGet无参数传递 { return sendGetMessage(null); } public InputStream sendGetMessage(Properties args) throws IOException//DoGet参数传递 { String argString=""; if(args!=null) { argString="?"+toEncodedString(args);//转换参数传递类型便于Sevlet的DoGet接收参数及参数解码 } URL url=new URL(Servlet.toExternalForm()+argString); URLConnection con=url.openConnection(); con.setUseCaches(false); InputStream in=con.getInputStream(); return in; } public InputStream sendPostMessage() throws IOException//DoPost无参数传递 { return sendPostMessage(null); } public InputStream sendPostMessage(Properties args) throws IOException//DoPost参数传递 { String argString=""; if(args!=null) { argString="?"+toEncodedString(args); } URL url=new URL(Servlet.toExternalForm()+argString); URLConnection con=url.openConnection(); con.setDoInput(true);//用于接收数据流,可以不用。 con.setDoOutput(true);//用于传递数据流 con.setUseCaches(false); con.setRequestProperty("Content-Type","application/x-www-form-urlencoded");//设置为DoPost传递参数 DataOutputStream out=new DataOutputStream(con.getOutputStream()); //out.writeBytes(argString);//在不传递文件流时不用 //out.flush(); out.close(); InputStream in=con.getInputStream(); return in; } //参数解码 private String toEncodedString(Properties args) { StringBuffer buf=new StringBuffer(); Enumeration names=args.propertyNames(); while(names.hasMoreElements()) { String name=(String)names.nextElement(); String value=args.getProperty(name); buf.append(URLEncoder.encode(name)+"="+URLEncoder.encode(value)); if(names.hasMoreElements()) { buf.append("&"); } } return buf.toString(); }} 楼上的就是例子了,再给你一个文档,可以参考一下http://ecapital.myetang.com/javaservlet/tutorial/ser03/servlet10.htm 如何让程序一直保持运行的状态? JSP里点击删除链接时弹出提示的问题 struts property 怎么得到值? url路径问题 关于样式表中的滚动条的下箭头问题 jbuilderX的servlet如何升级???? JSP设计 jfreechart如何显示中文汉字? tomcat中设置session有效期限的方法? 请问Jtree类可以用于JSP中吗,现在的需求是这样... 有谁能告诉我javax.servlet包在哪里有! 请问servlet,需要什么配置
最好能给个程序片断,谢谢了。
APPLET中的变量也可以用request.getParameter("name")来获得吗?
<APPLET
CODEBASE = "."
CODE = "gt_fx.aplt_main.class"
WIDTH = 595
HEIGHT = 350
HSPACE = 0
VSPACE = 0
ALIGN = top
>
<param name= mapType value="1">
</APPLET>java 中获得参数
getParameter("mapType ", "1");
JavaScript是用于HTML环境的开发语言,提供了能够响应Web页面事件的脚本,可以完全访问浏览器窗口的各个方面,善于合并HTML、Java Applet小程序、插入件、服务器方程序和其他Web组件,形成完全集成的Web应用程序。而Java是功能强大的著名开发语言,能够支持Web应用程序的开发,用于开发高级Web对象,并提供实现嵌入Web页面的可执行内容,具有更强的编程功能,可以作为JavaScript功能的良好补充,只不过这些功能被限制在有限的浏览器窗口区中。
Java与JavaScript可以互相补充,以开发功能更完美的Web应用程序。本文归纳了笔者编程中曾使用过的,涉及到有关JavaScript与Java、Java Applet小程序之间互访的问题的一些方法,仅供参考。
一、从JavaScript中访问Java方法
在HTML脚本中可以用JavaScript直接调用Java中的方法,只需要在调用语句前用“java.lang.”加以表示即可。具体语句用法如下例所示:
java.Lang.System.Out.println(“Hello!”)
但此种方法只适用于Netscape Navigator,一般不提倡在Web应用程序中使用。
二、从JavaScript中访问Java Applet小程序中的方法和变量
通过JavaScript提供的Applet对象,JavaScript代码可以访问Java的变量和方法,具体步骤和用法示例如下。需要注意的是,JavaScript提供的applet对象具有name属性,而没有方法和事件处理器。
步骤:
1) 将要访问的Java Applet小程序的方法和变量声明为Public,且必须在Public类中声明;
2) Java Applet小程序要先装入才能访问,尽管applet对象没有定义onLoad事件,但可以在HTML文档的〈body〉标志中,指定使用Window对象的onLoad事件处理器;
3) Java Applet小程序可以用JavaScript的applet对象来访问。
示例:
“mytest.htm”
<html>
<head>
<script language="JavaScript">
function accessApplet()
{ setTimeout("changeText('I like JavaScript!')",1000);
setTimeout("changeText('I like JavaScript and Java!')",2000);
setTimeout("changeText('I like Java!')",3000);
setTimeout("accessApplet()",4000);
}
function changeText(s)
{
window.document.testText.setText(s)
//此处调用JavaApplet小程序的setText()方法
//注意,小程序的名字必须为〈applet〉标志 name属性所标识
}
</script>
</head>
<body onload="accessApplet()">
//通过调用accessApplet()装入小程序
<applet code="Testtext.class" name="testText" width=450 height=150>
</applet>
</body>
</html>
“Testtext.java”
import java.applet.*;
……
public class Testtext extends Applet
{ ……
public void setText(String s) //setText()必须声明为“public”
{
text=s;
repaint();
}
}
三、在Java Applet小程序中使用JavaScript
Live Connect提供了Java与JavaScript的接口,可以允许在Java Applet小程序中使用JavaScript。具体步骤如下:
1) 在HTML脚本中使用〈APPLET〉标志的MAYSCRIPT属性,以允许Java Applet小程序访问脚本;
2) 将netscape. JavaScript包导入Java Applet小程序;
3) 在Java Applet小程序中用JSObject类的getWindow( )方法创建JavaScript窗口的句柄;
4) 在Java Applet小程序中用JSObject类的getMember( )方法访问JavaScript对象;
5) 在Java Applet小程序中用JSObject类的eval( )方法调用JavaScript方法。
示例:
“ReadForm. Java”
import netscape.javascript.JSObject;
import netscape.javascript.JSException; //可允许在小程序中处理异常事件
……
win=JSObject.getWindow(this); // 获取JavaScript窗口句柄,引用当前文档窗口
doc=(JSObject)win.getMember("document"); // 访问JavaScript对象
form=(JSObject)doc.getMember("textForm");
textField=(JSObject)form.getMember("textField");
text=(String) textField.getMember("value"); //获取文本区的值
……
win.eval("alert(\"This alert comes from Java!\")");
// 调用JavaScript的alert()方法
……
“User.htm”
……
<BODY>
<form name="textForm">
<P>Enter text and then click display text:
<input type="text" name="textField" size="20"></P>
</FORM>
<applet code="ReadForm.class" width=200 height=100 name="readApp" MAYSCRIPT>
//必须使用MAYSCRIPT属性标志
</APPLET>
</BODY>
这样是让Applet获得了JSP页面中的变量了,如果要再把一些变量从Applet传回给JSP页面呢?
非常感谢,说的很详细呀,我仔细研究一下,呵呵。
不过jsp和JavaScript还是有区别的,jsp中的方法有吗?
1. 在jsp内获得输入页面内的值的:
<td><input type="test" name="valhot" size=20></td><tr>
<%
try{
hot=request.getParameter("valhot");//先获得字符串类型
mesg="";
if(hot==null){
mesg="不能为空!";
out.println(mesg);
}
else{
valhot=Double.valueOf(hot).doubleValue(); //获得双精度类型
}
%><br>
}
catch(Exception e){
e.printStackTrace();
}2. 然后将获得数据传递到applet:<applet
codebase = "."
code = "project.classname.class"
name = "TestApplet"
width = "830"
height = "410"
>
<!--向applet传递参数-->
<Param name="valhot" value=<%=valhot%>>
/>
</applet>3. 再在applet中取得数值:hot=getParameter("valhot"); //取的字符串类型
valhot=Double.valueOf(hot).doubleValue(); //转化为双精度类型这样就可以利用数值在applet中进行计算了。
我想实现在jsp页面内循环计算数据,每循环一次,就把数据传递给applet一次,这样能实现吗?
如果能,该如何实现,请帮忙!!
闄ら潪浣犵殑applet鏈夎冻澶熺殑鍙傛暟鍙橀噺锛屼緵寰幆鏃舵尐涓祴鍊硷紝
怎么样再从applet把参数传回给jsp页面呀?
String m_hostName=getCodeBase().getHost();
String m_servletName="http://"+m_hostName+":8080/birdWeb3S/vehicleservlet";//设置Servlet的路径
URL servletURL=new URL(m_servletName);
HttpSendMessage m_message=new HttpSendMessage(servletURL);//初始化中间层 //Applet中的示范程序
public void AlarmSet(String strAlarmID,String strVehicleVin)
{
try
{
Properties pts=new Properties();
pts.put("AlarmSet","ok");
pts.put("AlarmID",strAlarmID);
pts.put("VehicleVin",strVehicleVin);
InputStream in=m_message.sendGetMessage(pts);//采用DoGet传递参数
InputStream in=m_message.sendPostMessage(pts);//采用DoPost传递参数
DataInputStream out=new DataInputStream(in);//接收Servlet返回的数据流
}
catch(IOException ex)
{
}
} //中间层用于Applet与Servlet之间的通讯
class HttpSendMessage extends Object{
URL Servlet=null;
String args=null;
public HttpSendMessage(URL Servlet)//中间层的构造函数
{
this.Servlet=Servlet;
} public InputStream sendGetMessage() throws IOException//DoGet无参数传递
{
return sendGetMessage(null);
} public InputStream sendGetMessage(Properties args) throws IOException//DoGet参数传递
{
String argString="";
if(args!=null)
{
argString="?"+toEncodedString(args);//转换参数传递类型便于Sevlet的DoGet接收参数及参数解码
}
URL url=new URL(Servlet.toExternalForm()+argString);
URLConnection con=url.openConnection();
con.setUseCaches(false);
InputStream in=con.getInputStream();
return in;
} public InputStream sendPostMessage() throws IOException//DoPost无参数传递
{
return sendPostMessage(null);
} public InputStream sendPostMessage(Properties args) throws IOException//DoPost参数传递
{
String argString="";
if(args!=null)
{
argString="?"+toEncodedString(args);
} URL url=new URL(Servlet.toExternalForm()+argString);
URLConnection con=url.openConnection();
con.setDoInput(true);//用于接收数据流,可以不用。
con.setDoOutput(true);//用于传递数据流 con.setUseCaches(false);
con.setRequestProperty("Content-Type","application/x-www-form-urlencoded");//设置为DoPost传递参数
DataOutputStream out=new DataOutputStream(con.getOutputStream()); //out.writeBytes(argString);//在不传递文件流时不用
//out.flush();
out.close();
InputStream in=con.getInputStream();
return in;
}
//参数解码
private String toEncodedString(Properties args)
{
StringBuffer buf=new StringBuffer();
Enumeration names=args.propertyNames(); while(names.hasMoreElements())
{
String name=(String)names.nextElement();
String value=args.getProperty(name);
buf.append(URLEncoder.encode(name)+"="+URLEncoder.encode(value));
if(names.hasMoreElements())
{
buf.append("&");
}
}
return buf.toString();
}
}
http://ecapital.myetang.com/javaservlet/tutorial/ser03/servlet10.htm