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!

[6930 byte] By [coolfireda] at [2007-10-2 1:48:04]
# 1

If I where you I whould:

1. put an ex.printStackTrace() to find out some more information about the raised Exception.

2. Make sure my rmi is not disallowing connections trought http, because when you set up an http client on an rmi the security policy file must be configured in order to enable IPs to connect (even if it is your own IP).

Hope this helps

Answer provided by http://www.consultoriajava.com, java consultancy providers

consultoriajava.coma at 2007-7-15 19:29:18 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2

Ok, I follow you use ex.printStackTrace() ,and I get the information as below:

Enviroment initial success!

javax.naming.ServiceUnavailableException [Root exception is java.rmi.ConnectException: Connection refused to host: 127.0.0.1; nested exception is:

java.net.ConnectException: Connection refused: connect]

at com.sun.jndi.rmi.registry.RegistryContext.lookup(Unknown Source)

at com.sun.jndi.rmi.registry.RegistryContext.lookup(Unknown Source)

at javax.naming.InitialContext.lookup(Unknown Source)

at Applet2.paint(Applet2.java:34)

at sun.awt.RepaintArea.paintComponent(Unknown Source)

at sun.awt.RepaintArea.paint(Unknown Source)

at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)

at java.awt.Component.dispatchEventImpl(Unknown Source)

at java.awt.Container.dispatchEventImpl(Unknown Source)

at java.awt.Component.dispatchEvent(Unknown Source)

at java.awt.EventQueue.dispatchEvent(Unknown Source)

at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.run(Unknown Source)

Caused by: java.rmi.ConnectException: Connection refused to host: 127.0.0.1; nested exception is:

java.net.ConnectException: Connection refused: connect

at sun.rmi.transport.tcp.TCPEndpoint.newSocket(Unknown Source)

at sun.rmi.transport.tcp.TCPChannel.createConnection(Unknown Source)

at sun.rmi.transport.tcp.TCPChannel.newConnection(Unknown Source)

at sun.rmi.server.UnicastRef.newCall(Unknown Source)

at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)

... 16 more

Caused by: java.net.ConnectException: Connection refused: connect

at java.net.PlainSocketImpl.socketConnect(Native Method)

at java.net.PlainSocketImpl.doConnect(Unknown Source)

at java.net.PlainSocketImpl.connectToAddress(Unknown Source)

at java.net.PlainSocketImpl.connect(Unknown Source)

at java.net.SocksSocketImpl.connect(Unknown Source)

at java.net.Socket.connect(Unknown Source)

at java.net.Socket.connect(Unknown Source)

at java.net.Socket.<init>(Unknown Source)

at java.net.Socket.<init>(Unknown Source)

at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(Unknown Source)

at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(Unknown Source)

... 21 more

how can I setup a http client on rmi security file to pemission remote IP to connect?

I'm using tomcat5.5.9 + jdk1.5.0_05 under win2k OS plateform.

thanks a lot!

coolfireda at 2007-7-15 19:29:18 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3

after I have test my older project applet(test ok with tomcat4 before) under tomcat 5.5.9, it occurs the same problem, connect refused. so I think that's the answer, tomcat 5.5.9 forbid foreign class use socket communication(local jsp can do). it needs start tomcat service with policy arguments. so I add permission to catalina.policy file

grant codeBase "file:${catalina.home}/webapps/ROOT/-" {

permission java.security.AllPermission;

};

but I found there was no explain how to start tomcat with policy file,

I know before tomcat5.5.7, user can execute cmd line:

catalina start -security

to start tomcat service,

but tomcat5.5.9 use tomcat5w.exe to config, how to config this with tomcat5w.exe?

I don't know and no document about it, Do you know it?

good lucky to you!

coolfireda at 2007-7-15 19:29:18 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4
You can set up a policy file for the jvm with tomcat, just modify the launch secuence including the D directive which allows you to specify a policy file.
consultoriajava.coma at 2007-7-15 19:29:18 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 5

yes.I add the following line to tomcat5w->java->java options:

-Djava.security.SecurityManager

-Djava.security.policy== D:/Apache Software Foundation/Tomcat 5.5/conf/catalina.policy

and catalina.policy:

grant codeBase "file:${catalina.home}/webapps/ROOT/WEB-INF/classes/-" {

permission java.net.SocketPermission "*:*", "*";

};

but the same error elso exists

coolfireda at 2007-7-15 19:29:18 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...