Eclipse Java EE 和 Tomcat 7:错误 404,请求的资源不可用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20268093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 00:43:08  来源:igfitidea点击:

Eclipse Java EE and Tomcat 7: Error 404, requested resource not available

javaeclipseservletstomcat7

提问by user2979063

Tomcat 7 on Eclipse Java EE is displaying HTTP STATUS 404, and THE REQUESTED RESOURCE IS NOT AVAILABLE. 1) i dont know what i am not doing right? I have read loads of post on the sight and attempted to do what was possible to do, yet nothing has change. In some cases i got back my source code as a downloadable text file.

Eclipse Java EE 上的 Tomcat 7 显示 HTTP 状态 404,并且请求的资源不可用。1)我不知道我做错了什么?我已经阅读了大量关于视线的帖子,并试图做可能做的事情,但没有任何改变。在某些情况下,我将源代码作为可下载的文本文件取回。

This is my servlet program below:

这是我的 servlet 程序如下:

package dbservlet;

 //all imports here

public class TennisdbaseServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public TennisdbaseServlet() {}

public void doGet(HttpServletRequest request,
   HttpServletResponse response)
   throws ServletException, IOException {

   Context envContext = null;
   try {

       Context envContext = new InitialContext(); 
       DataSource ds = (DataSource)envContext.lookup("java:/comp/env/jdbc/postgres"); 
       Connection con = ds.getConnection();

       Statement stmt = con.createStatement();
       String query = "select * from tennis_teams";
       ResultSet rs = stmt.executeQuery(query);

       PrintWriter out = response.getWriter();
       response.setContentType("text/html");
       out.print("<center><h1>Tennis Teams Info</h1></center>");
       out.print("<html><body><table border=\"1\" cellspacing=10 cellpadding=5>");
       out.print("<th>TEAM NUMBER</th>");
       out.print("<th>PLAYER NO</th>");
       out.print("<th>DIVISION</th>");

       while(rs.next())
       {
           out.print("<tr>");
           out.print("<td>" + rs.getInt("teamno") + "</td>");
           out.print("<td>" + rs.getString("playerno") + "</td>");
           out.print("<td>" + rs.getDouble("division") + "</td>");
           out.print("</tr>");
       }
       out.print("</table></body></html>");
   }  catch (SQLException e) {
       e.printStackTrace();
   } catch (NamingException e) {
       e.printStackTrace();
   }
  }

}

}

And following is my /META-INF/Context.xml code:

以下是我的 /META-INF/Context.xml 代码:

<?xml version="1.0" encoding="UTF-8"?>

<!-- The contents of this file will be loaded for web application -->

<Context crossContext="true" docBase="TennisdbaseServlet" path="" reloadable="true" >
  <!-- Default set of monitored resources -->
   <WatchedResource>WEB-INF/web.xml</WatchedResource>
   <Resource name="jdbc/postgres" auth="Container"
      type="javax.sql.DataSource" 
      driverClassName="org.postgresql.Driver"
      url="jdbc:postgresql://localhost:5432/tennisDB"
      username="ssssss" 
      password="ssssss" 
      maxActive="20" 
      maxIdle="10"
      maxWait="-1"/>
</Context>

And also below is my /Web_INF/web.xml :

下面是我的 /Web_INF/web.xml :

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns   /j2ee/web-app_2_4.xsd">

<resource-ref>
<description>postgreSQL DataSource for Tennis Database Application</description>
<res-ref-name>jdbc/postgres</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

<display-name>TennisDatabasePool</display-name>

<servlet>
<servlet-name>TennisdbaseServlet</servlet-name>
<servlet-class>dbservlet.TennisdbaseServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TennisdbaseServlet</servlet-name>
<url-pattern>/TennisdbaseServlet.do</url-pattern>
</servlet-mapping>
</web-app>

The console output is:

控制台输出是:

Nov 28, 2013 1:42:29 PM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in   production environments was not found on the java.library.path: C:\Program Files\Java   \jre7\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Program Files\Common  Files\Microsoft Shared\Microsoft Online Services;C:\Program Files (x86)\Common Files\Microsoft Shared\Microsoft Online Services;C:\Program Files\Common Files\Microsoft Shared\Windows Live;C:\Program Files (x86)\Common Files\Microsoft Shared\Windows Live;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static\;C:\Program Files (x86)\Java\jre6\bin;C:\Program Files (x86)\Windows Live\Shared;c:\Program Files (x86)\Microsoft SQL Server0\Tools\Binn\;c:\Program Files\Microsoft SQL Server0\Tools\Binn\;c:\Program Files\Microsoft SQL Server0\DTS\Binn\;PATH=C:\Program Files\Java\jdk1.7.0_45\bin;C:\Program Files\Common Files\Microsoft Shared\Microsoft Online Services;C:\Program Files (x86)\Common Files\Microsoft Shared\Microsoft Online Services;C:\Program Files\Common Files\Microsoft Shared\Windows Live;C:\Program Files (x86)\Common Files\Microsoft Shared\Windows Live;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static\;C:\Program Files (x86)\Java\jre6\bin;C:\Program Files (x86)\Windows Live\Shared;c:\Program Files (x86)\Microsoft SQL Server0\Tools\Binn\;c:\Program Files\Microsoft SQL Server0\Tools\Binn\;c:\Program Files\Microsoft SQL Server0\DTS\Binn\;PATH=C:\Program Files\Java\jdk1.7.0_45\bin;%PATH%;C:\Program Files\System Center Operations Manager 2007\;C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\;C:\Program Files\Microsoft SQL Server0\Tools\Binn\;C:\Program Files\System Center Operations Manager 2007\;C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\;C:\Program Files\Microsoft SQL Server0\Tools\Binn\;C:\Program Files (x86)\QuickTime\QTSystem\;C:\Program Files (x86)\Graphviz2.30\bin;C:\Program Files (x86)\MySQL\MySQL Utilities 1.3.4\;C:\Program Files\Java\jdk1.7.0_45\bin;C:\Program Files\Java\jre7\bin;.
Nov 28, 2013 1:42:33 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8010"]
Nov 28, 2013 1:42:33 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Nov 28, 2013 1:42:33 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 5546 ms
Nov 28, 2013 1:42:33 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Nov 28, 2013 1:42:33 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.47
Nov 28, 2013 1:42:39 PM org.apache.catalina.startup.HostConfig deployDescriptor
INFO: Deploying configuration descriptor C:\Users\Windows\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\conf\Catalina\localhost\TennisDatabasePool.xml
Nov 28, 2013 1:42:40 PM org.apache.catalina.startup.SetContextPropertiesRule begin
WARNING: [SetContextPropertiesRule]{Context} Setting property 'source' to 'org.eclipse.jst.jee.server:TennisDatabasePool' did not find a matching property.
Nov 28, 2013 1:42:43 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8010"]
Nov 28, 2013 1:42:43 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Nov 28, 2013 1:42:43 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 10091 ms

Your help is highly appreciated.

非常感谢您的帮助。

回答by Pat

As you have not provided the URL that you are using, I am guessing that you may be missing the Context Root in your URL. Ideally, you should use a URL similar to this for accessing your servlet:

由于您没有提供您正在使用的 URL,我猜您可能在您的 URL 中缺少上下文根。理想情况下,您应该使用类似于此的 URL 来访问您的 servlet:

http://<server>:<port>/<context_root>/TennisdbaseServlet.do

where context_rootis usually the name of your application (WAR file). For example, if your WAR file is MyApplication.war, then you have to use below URL:

其中context_root通常是应用程序的名称(WAR 文件)。例如,如果您的 WAR 文件是MyApplication.war,那么您必须使用以下 URL:

http://<server>:<port>/MyApplication/TennisdbaseServlet.do

However, you have the option to override this using Context.xml file on Tomcat. For example, below <Context>tag in Context.xml would set the context_root to SomeApp

但是,您可以选择使用 Tomcat 上的 Context.xml 文件来覆盖它。例如,<Context>在 Context.xml 中的下方标记会将 context_root 设置为SomeApp

<Context crossContext="true" docBase="SomeApp" path="" reloadable="true" >

Then, to access the same application, you can use URL:

然后,要访问相同的应用程序,您可以使用 URL:

http://<server>:<port>/SomeApp/TennisdbaseServlet.do

回答by Deepak

When with the j2eeview is provided as JSPwhy are u doing it with servlets only .Try to do it with JSP.Also i think your access pattern of the servlet is not correct this should be some thing like this http://localhost:port/app-name/dbservlet.Also u have not defined any servlet with url-pattern/dbservlet. Below is a good tutorial you can start with.
http://www.mkyong.com/servlet/a-simple-servlet-example-write-deploy-run/

当提供j2ee视图时,JSP为什么你只用 servlet 来做它。尝试用 JSP 来做。另外我认为你的 servlet 访问模式不正确,这应该是这样的http://localhost:port/app-name/dbservlet。另外你还没有定义任何 servlet与url-pattern/dbservlet. 下面是一个很好的教程,你可以开始。
http://www.mkyong.com/servlet/a-simple-servlet-example-write-deploy-run/

回答by Anirudh

I don't really understand how could the below code even compile:

我真的不明白下面的代码是如何编译的:

Context envContext = null;
   try {

       Context envContext = new InitialContext(); 

First you should always get rid of the compile time errors and then try to call the servlet: below is the a compiler error free code:

首先,您应该始终摆脱编译时错误,然后尝试调用 servlet:以下是无编译器错误的代码:

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.activation.DataSource;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sun.corba.se.pept.transport.Connection;


public class TennisdbaseServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public TennisdbaseServlet() {}

public void doGet(HttpServletRequest request,
   HttpServletResponse response)
   throws ServletException, IOException {

   Context envContext = null;
   try {

       envContext =  new InitialContext(); 
       DataSource ds = (DataSource) envContext.lookup("java:/comp/env/jdbc/postgres"); 
       Connection con = (Connection) ((Statement) ds).getConnection();

       Statement stmt = ((java.sql.Connection) con).createStatement();
       String query = "select * from tennis_teams";
       ResultSet rs = stmt.executeQuery(query);

       PrintWriter out = response.getWriter();
       response.setContentType("text/html");
       out.print("<center><h1>Tennis Teams Info</h1></center>");
       out.print("<html><body><table border=\"1\" cellspacing=10 cellpadding=5>");
       out.print("<th>TEAM NUMBER</th>");
       out.print("<th>PLAYER NO</th>");
       out.print("<th>DIVISION</th>");

       while(rs.next())
       {
           out.print("<tr>");
           out.print("<td>" + rs.getInt("teamno") + "</td>");
           out.print("<td>" + rs.getString("playerno") + "</td>");
           out.print("<td>" + rs.getDouble("division") + "</td>");
           out.print("</tr>");
       }
       out.print("</table></body></html>");
   }  catch (SQLException e) {
       e.printStackTrace();
   } catch (NamingException e) {
       e.printStackTrace();
   }
  }
}

Try running it with this first and then debug the errors henceforth if any!!

尝试先运行它,然后调试此后的错误(如果有)!