以下代码根据用户电子邮件从数据库中提取照片的名称。
package NonServletFiles;
import javax.sql.*;
import java.sql.*;
import javax.naming.*;
public class GetPhotosForTheUser {
public ResultSet getData(String email) {
ResultSet set = null;
try {
String sqlQuery = "select nameofthephoto from photocaptions where useremail='" + email + "'";
Context context = new InitialContext();
DataSource ds = (DataSource)context.lookup("java:comp/env/jdbc/photog"); // LINE 17
Connection connection = ds.getConnection();
PreparedStatement statement = connection.prepareStatement(sqlQuery);
set = statement.executeQuery();
while(set.next()){
System.out.println("Name Of The Photo : " + set.getString("NameOfThePhoto"));
}
}catch(Exception exc) {
exc.printStackTrace();
}
return set;
}
}
如果我从.jsp
文件中将此助手类称为:
<%
GetPhotosForTheUser gpftu = new GetPhotosForTheUser();
gpftu.getData("suhailgupta03@gmail.com");
%>
它在服务器控制台上输出正确的名称。
但如果我通过添加main
方法
在该助手类中,它抛出一个异常:
javax.naming.NamingException: Lookup failed for 'java:comp/env/jdbc/photog' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.url.pkgs=com.sun.enterprise.naming, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl} [Root exception is javax.naming.NamingException: Invocation exception: Got null ComponentInvocation ]
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:518)
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:455)
at javax.naming.InitialContext.lookup(InitialContext.java:411)
at NonServletFiles.GetPhotosForTheUser.getData(GetPhotosForTheUser.java:17)
at NonServletFiles.GetPhotosForTheUser.main(GetPhotosForTheUser.java:32)
Caused by: javax.naming.NamingException: Invocation exception: Got null ComponentInvocation
at com.sun.enterprise.naming.impl.GlassfishNamingManagerImpl.getComponentId(GlassfishNamingManagerImpl.java:873)
at com.sun.enterprise.naming.impl.GlassfishNamingManagerImpl.lookup(GlassfishNamingManagerImpl.java:742)
at com.sun.enterprise.naming.impl.JavaURLContext.lookup(JavaURLContext.java:172)
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:498)
... 4 more
为什么会这样?我正在使用 glassfish服务器和netbeans进行此开发。
答案 0 :(得分:3)
当您将其作为Web应用程序运行时,您将使用服务器详细信息初始化Context
Context context = new InitialContext(); // This is initialized when you run as web app
当你通过调用main方法作为独立程序运行时,同样不是这样
你可以通过初始化你的背景去除它。
Properties prop = new Properties();
prop.put(Context.INITIAL_CONTEXT_FACTORY, "your provider"); // like for websphere it is com.ibm.websphere.naming.WsnInitialContextFactory and weblogic weblogic.jndi.WLInitialContextFactory
prop.put(Context.PROVIDER_URL, "server path"); //
Context context = new InitialContext(prop);
注意:通常你不会这样写,而是你的代码会检查它是否在WEBMODE或TEST中运行,如果测试它会初始化Context,否则它只会使用普通的上下文。
这将初始化您的上下文,您将能够从main方法运行它。
编辑:从here
获取的Glassfish配置 Properties props = new Properties();
props.setProperty("java.naming.factory.initial",
"com.sun.enterprise.naming.SerialInitContextFactory");
props.setProperty("java.naming.factory.url.pkgs",
"com.sun.enterprise.naming");
props.setProperty("java.naming.factory.state",
"com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
// optional. Defaults to localhost. Only needed if web server is running
// on a different host than the appserver
props.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");
// optional. Defaults to 3700. Only needed if target orb port is not 3700.
props.setProperty("org.omg.CORBA.ORBInitialPort", "3700");
InitialContext ic = new InitialContext(props);
答案 1 :(得分:0)
使用main方法运行它时,您不会将其作为Web应用程序执行,因此您没有Web应用程序上下文。