天天看点

IDEA玩mybatis数据库连接池JNDI出现的Name * is not bound问题

javax.naming.NameNotFoundException: Name * is not bound

代码完全没有问题

①context.xml中:

<?xml version="1.0" encoding="UTF-8"?>
<Context>
    <Resource driverClassName="com.mysql.cj.jdbc.Driver"
        url="jdbc:mysql://localhost:3306/flower?serverTimezone=GMT%2B8"
        username="root"
        password=""
        maxActive="50"
        maxIdle="20"
        name="jdbc/test"
        auth="Container"
        maxWait="10000"
        type="javax.sql.DataSource"/>
</Context>

           

②ContextServlet中

@WebServlet(name = "contextServlet",urlPatterns = "/pool")
public class ContextServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            Context cxt=new InitialContext();
            DataSource ds=(DataSource) cxt.lookup("java:/comp/env/jdbc/test");
            Connection conn=ds.getConnection();
            PrintWriter pw= resp.getWriter();
            pw.print("sucess"); pw.flush();
            pw.close();
        } catch (NamingException | SQLException e) {
            e.printStackTrace();
        }
        }
            ...............................
           

问题所在

首先,在web项目中,不需配置web.xml,配置该处又会导致另外的错误

实际上问题就在于:

context.xml放到了web内的WEB-INF里,而没有放到web内新建META-INF文件夹内。