简体   繁体   English

为什么JNDI InitialContext可以在jsp中正常工作但在类中不能正常工作?

[英]Why JNDI InitialContext can work fine in jsp but not in a class?

I'm working with Database connection using JNDI. 我正在使用JNDI处理数据库连接。 I'm sure the deployment is fine. 我确信部署很好。 Because when I test it in the JSP below, it works fine. 因为当我在下面的JSP中测试它时,它工作正常。

</head> 
<body> 
<% 
try { 
    Context initCtx = new InitialContext();
    Context envCtx = (Context) initCtx.lookup("java:comp/env");
    DataSource ds = (DataSource)envCtx.lookup("jdbc/airlineticket");
    Connection conn = ds.getConnection();
out.println(conn); 
conn.close(); 
} catch (Exception e) { 
e.printStackTrace(); 
}
%> 
</body> 
</html>

success connection picture 成功连接图片

But when I use ConnectionFactory class, an error occurs: ConnectionFactory cannot be resolved. 但是当我使用ConnectionFactory类时,会发生错误: 无法解析ConnectionFactory。 I just put the connection code in the ConnectionFactory class, why this happened? 我只是把连接代码放在ConnectionFactory类中,为什么会这样? ConnectionFactory class: ConnectionFactory类:

package com.db;

import javax.naming.*;
import java.sql.*;
import org.apache.tomcat.jdbc.pool.DataSource;

public class ConnectionFactory {
    private ConnectionFactory() {};
public static Connection getConnection()
    {
        try {
            Context initCtx = new InitialContext();
            Context envCtx = (Context) initCtx.lookup("java:comp/env");
            DataSource ds = (DataSource)envCtx.lookup("jdbc/airlineticket");
            return ds.getConnection();
        }
        catch (NamingException e)
        {
            System.out.print("connection failed");
            return null;
        }
        catch(SQLException e)
        {
            e.printStackTrace();
            return null;
        }
    }
}

test JSP: 测试JSP:

  <%@ page language="java" import="java.util.*,com.db.ConnectionFactory" pageEncoding="UTF-8"%> 
</head> 
    <body>     
    <% 
    try {       
    Connection conn = ConnectionFactory.getConnection(); 
    out.println(conn); 
    conn.close(); 
    } catch (Exception e) { 
    e.printStackTrace(); 
    }
    %> 
    </body> 
    </html>

web.xml(/WEB-INF/web.xml): 的web.xml符(/WEB-INF/web.xml):

<resource-ref>
  <res-ref-name>jdbc/airlineticket</res-ref-name>
  <res-type>javax.sql.DataSource</res-type>
  <res-auth>Container</res-auth>
</resource-ref>

context.xml(/META-INF/context.xml): 的context.xml(/META-INF/context.xml):

<Context>
    <Resource name="jdbc/airlineticket" 
        auth="Container" 
        type="javax.sql.DataSource"
        username="Manager" 
        password="123456"
        driverClassName="org.mariadb.jdbc.Driver"
        url="jdbc:mariadb://localhost:3306/airlineticket"
        maxActive="50"
        masIdle="20"> </Resource>
</Context>
  • Are you able to build the application fine? 你能建立好应用程序吗?
  • Make sure you have deployed the class file properly. 确保已正确部署了类文件。

JSP will be recompiled automatically... whereas the java files, we have to build and deploy everytime, until unless if you are using JRebel.... JSP将自动重新编译...而对于java文件,我们必须每次都构建和部署,除非您使用JRebel ....

If you have done this already, I wish to see your complete logs... 如果您已经这样做了,我希望看到您的完整日志......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM