嵌入网页中applet 怎么连接数据库

FG2006 2009-05-02 06:23:12
问一下大家,嵌入到网页中的applet用网页打开时能否连接到数据库,并非在IDE下面,如果能的话,怎么连接?驱动怎么引入啊?先谢谢大家了
...全文
74 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
MT502 2009-05-05
  • 打赏
  • 举报
回复
在applet里操作数据库没什么特别的,和平常一样,就是要在policy文件里配置允许到数据库的socket链接,最简单就是加上permission java.security.AllPermission;了
但是这种方式不安全,因为数据库的连接信息包括密码都会被下载到客户端,因为要被applet用到。
比较好的是applet和servlet通信,由servlet去操作数据库
applet像这样:
ObjectOutputStream outputToServlet = null;
ObjectInputStream inputFromServlet = null;
try {
URL servlet = new URL(urladdress);
URLConnection servletConnection = servlet.openConnection();

//inform the connection that will send output and accept input
servletConnection.setDoInput(true);
servletConnection.setDoOutput(true);

//Don't use a cached version of URL connection.
servletConnection.setUseCaches (false);
servletConnection.setDefaultUseCaches (false);

//Specify the content type that will send binary data
servletConnection.setRequestProperty ("Content-Type", "application/octet-stream");

//send the object to the servlet using serialization
outputToServlet = new ObjectOutputStream(servletConnection.getOutputStream());
outputToServlet.writeObject(Some_Query_Data);
outputToServlet.flush();

//accept the object from the servlet using reserialization
inputFromServlet = new ObjectInputStream(servletConnection.getInputStream());
Some_Result = inputFromServlet.readObject();
//处理结果
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
outputToServlet.close();
inputFromServlet.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


servlet像这样:
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
{
ObjectInputStream inputFromApplet = null;
inputFromApplet = new ObjectInputStream(request.getInputStream());
Some_Query_Data = (String)inputFromApplet.readObject();
//处理输入,查询

ObjectOutputStream outputToApplet = null;
try {
outputToApplet = new ObjectOutputStream(response.getOutputStream());
outputToApplet.writeObject(Some_Result);
outputToApplet.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
outputToApplet.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
yangxiao_jiang 2009-05-05
  • 打赏
  • 举报
回复
可以用rmi,来进行数据库的操作
liuzhou0117 2009-05-05
  • 打赏
  • 举报
回复
可以考虑JSP+JavaBean
FG2006 2009-05-03
  • 打赏
  • 举报
回复
不是很明白,“用http请求数据库的内容”具体怎么能呢,要用到tomcat这种服务器吗?
meadking 2009-05-02
  • 打赏
  • 举报
回复
applet嵌入到网页中,就相当于在客户端了。
它在IE的客户机中运行,有个沙箱jvm,你可以打开java的Console看调试结果。
所以说,你要链接服务器的数据库,那么服务器和客户端的通信方式是HTTP,那么只有用http请求数据库的内容了。
你的applet请求特定的服务器url,获取文本,在客户端显示

13,100

社区成员

发帖
与我相关
我的任务
社区描述
Java J2ME
社区管理员
  • J2ME社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧