Java Eclipse的InitialContext问题

Java Eclipse的InitialContext问题,java,eclipse,Java,Eclipse,我收到以下错误消息: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial at javax.naming.spi.NamingManager.get

我收到以下错误消息:

javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
        at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)
        at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
        at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325)
        at javax.naming.InitialContext.lookup(InitialContext.java:392)
        at edu.neu.cs5200.Homework4.UserManager.getConnection(UserManager.java:31)
        at edu.neu.cs5200.Homework4.UserManager.createUser(UserManager.java:55)
        at edu.neu.cs5200.Homework4.UserManager.main(UserManager.java:95)
Exception in thread "main" java.lang.NullPointerException
        at edu.neu.cs5200.Homework4.UserManager.createUser(UserManager.java:58)
        at edu.neu.cs5200.Homework4.UserManager.main(UserManager.java:95)
在web.xml中(在web INF文件夹中)

我有这个:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Homework4</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

  <resource-ref>
        <description>Homework 4 MySQL Data Source</description>
        <res-ref-name>jdbc/DBHomework4</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
  </resource-ref>
</web-app>
  <Resource name="jdbc/DBHomework4" auth="Container" type="javax.sql.DataSource"
               maxActive="100" maxIdle="30" maxWait="10000"
               username="root" password="root"
               driverClassName="com.mysql.jdbc.Driver"
               url="jdbc:mysql://localhost:8889/Homework4"/>

家庭作业4
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
作业4 MySQL数据源
jdbc/DBHomework4
javax.sql.DataSource
容器
然后,在tomcat的context.xml中,我有以下内容:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Homework4</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

  <resource-ref>
        <description>Homework 4 MySQL Data Source</description>
        <res-ref-name>jdbc/DBHomework4</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
  </resource-ref>
</web-app>
  <Resource name="jdbc/DBHomework4" auth="Container" type="javax.sql.DataSource"
               maxActive="100" maxIdle="30" maxWait="10000"
               username="root" password="root"
               driverClassName="com.mysql.jdbc.Driver"
               url="jdbc:mysql://localhost:8889/Homework4"/>


所有这些都应该是正确的,但我仍然得到InitialContext错误。有什么想法吗?

您直接从main()运行应用程序,它应该在提供上下文的Servlet容器(Tomcat)中运行。您应该将应用程序部署到Tomcat,并从Servlet调用UserManager