Eclipse Java EE上的Tomcat 7正在显示HTTP状态404,并且请求的资源不可用。 1)我不知道我做得对吗?我已经阅读了大量关于视线的帖子并试图做可能做的事情,但没有任何改变。在某些情况下,我将源代码作为可下载的文本文件返回。
这是我的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();
}
}
}
以下是我的/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>
以下是我的/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>
控制台输出是:
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 Server\100\Tools\Binn\;c:\Program Files\Microsoft SQL Server\100\Tools\Binn\;c:\Program Files\Microsoft SQL Server\100\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 Server\100\Tools\Binn\;c:\Program Files\Microsoft SQL Server\100\Tools\Binn\;c:\Program Files\Microsoft SQL Server\100\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 Server\110\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 Server\110\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
非常感谢您的帮助。
答案 0 :(得分:1)
由于您没有提供您正在使用的URL,我猜您可能错过了URL中的Context Root。理想情况下,您应该使用类似于此的URL来访问您的servlet:
http://<server>:<port>/<context_root>/TennisdbaseServlet.do
其中context_root
通常是应用程序的名称(WAR文件)。例如,如果您的WAR文件是MyApplication.war
,那么您必须使用以下URL:
http://<server>:<port>/MyApplication/TennisdbaseServlet.do
但是,您可以选择使用Tomcat上的Context.xml文件覆盖它。例如,Context.xml中的<Context>
标记下面会将context_root设置为SomeApp
<Context crossContext="true" docBase="SomeApp" path="" reloadable="true" >
然后,要访问同一个应用程序,您可以使用URL:
http://<server>:<port>/SomeApp/TennisdbaseServlet.do
答案 1 :(得分:0)
当j2ee
视图以JSP
的形式提供时,为什么你只使用servlet。尝试用JSP做。同时我认为你的servlet访问模式不正确有点像http://localhost:port/app-name/dbservlet
这样的事情。你也没有用url-pattern
/dbservlet
定义任何servlet。下面是一个很好的教程,你可以从这开始
http://www.mkyong.com/servlet/a-simple-servlet-example-write-deploy-run/
答案 2 :(得分:0)
我真的不明白下面的代码怎么可以编译:
Context envContext = null;
try {
Context envContext = new InitialContext();
首先你应该总是摆脱编译时错误,然后尝试调用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();
}
}
}
首先尝试运行它,然后再调试错误,如果有的话......