天天看點

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檔案夾内。