can applet connect to tomcat's datasource by using jndi?
Hi, All,good luck to you!
I'm wondering if I could use applet to connect to tomcat(5.5.9)'s datasource? Does anyone can solve this problem?
Below is my jsp test code:
//////////////////////////////////////test3.jsp/////////////////////////////////////////
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%@page import="javax.naming.*"%>
<%@page import="javax.sql.*"%>
<%@page import="java.util.*"%>
<%
int counts=0;
// database operation
Context ctx=null;
Connection cnn=null;
Statement stmt=null;
ResultSet rs=null;
Properties env =new Properties();
env.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.rmi.registry.RegistryContextFactory");//"org.apache.commons.dbcp.BasicDataSourceFactory"); //
env.put(javax.naming.Context.PROVIDER_URL,"rmi://localhost:1099");
ctx=new InitialContext(env);
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/connectDB");
cnn=ds.getConnection();
stmt=cnn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery("select count(*) from orders");
while(rs.next()){
counts=rs.getInt(1);
}
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>JNDI test</title>
</head>
<body>
pnrreg talbe's count is:<%=counts%>
</body>
</html>
test htp://localhost:8080/test3.jsp
the result test this jsp code is correct:
pnrreg talbe's count is:830
Because it use the JNDI's RMI to access remote datasource,so I guess maybe I can write a applet to access the same Tomcat's datasource by the same way,but I failed.
Below is my test applet code:
//////////////////////////////Applet2.java/////////////////////////////////////////////////////////
import java.awt.*;
import java.applet.*;
import java.sql.*;
import java.util.*;
import javax.sql.*;
import javax.naming.*;
publicclass Applet2extends Applet
{
publicvoid init()
{
resize(200,200);
}
publicvoid paint(Graphics g)
{
this.setBackground(Color.lightGray );
this.setForeground(Color.red);
int counts=0;
Context ctx=null;
Connection cnn=null;
Statement stmt=null;
ResultSet rs=null;
try
{
Properties env =new Properties();
env.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.rmi.registry.RegistryContextFactory");//"org.apache.commons.dbcp.BasicDataSourceFactory"); //
env.put(javax.naming.Context.PROVIDER_URL,"rmi://localhost:1099");
ctx=new InitialContext(env);
System.out.println("enviroment initial success!");
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/connectDB");
System.out.println("Datasource lookup success!");
cnn=ds.getConnection();
System.out.println("database pool connected!");
stmt=cnn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery("select count(*) from orders");
while(rs.next()){
counts=rs.getInt(1);
}
g.drawString(Integer.toString(counts),40,60);
System.out.println("draw success!");
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(cnn!=null)
cnn.close();
if(ctx!=null)
ctx.close();
}
catch(Exception ex){System.out.println(ex.getMessage());}
}
}
build applet class success,then write test html as below:
<!DOCTYPE HTML PUBLIC"-//IETF//DTD HTML//EN">
<HTML> <HEAD>
<TITLE>Test Applet2</TITLE>
</HEAD>
<BODY>
<CENTER>
<applet code=Applet2.classwidth=200 height=200 >
</applet>
</CENTER>
</BODY> </HTML>
unluckly, it failed,the console debug information is :
Enviroment initial success!
null
It means
ctx=new InitialContext(env);
excuted success,but
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/connectDB");
excuted failed.
Anyone one know Why?
Thanks a lot!

