404请求的资源不可用Eclipse / Tomcat

时间:2016-05-28 10:43:54

标签: java eclipse jsp tomcat servlets

我想我到目前为止已经阅读了关于这个主题的每个问题,我真的尝试了很多解决方案,如果我忽略了任何问题,那就很抱歉。 我正在使用Eclipse和Tomcat 8。 Tomcat已经配置为服务器,MySQL-connector .... jar在WEB-INF / lib文件夹中,web.xml只在/ WEB-INF中,而索引在/ WebContent中

的index.html:

<!DOCTYPE html>
<html>
<form action="Servlets/Start" method="post">
<font face="verdana" size="2">
 Enter Table Name :<input type="text" name="table"> 
                   <input type="submit" value="Display">
  </font>
</form>


</html

start.java:

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
/**
 * Servlet implementation class Start
 */
@WebServlet("/Start")
public class Start extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
 * @see HttpServlet#HttpServlet()
 */
public Start() {
    super();
    // TODO Auto-generated constructor stub
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse respond) throws ServletException, IOException {
PrintWriter pw=respond.getWriter(); 
respond.SetContentType("text/html");
String tb=request.getParameter("table");
try
{
    Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection con=DriverManager.getConnection("jdbc::oracle::thin:@localhost:music","root","1234");    
    Statement st=con.createStatement();
    System.out.println("connection established successfully!");
    ResultSet rs=st.executeQuery("SELECT * FROM"+tb);

    pw.println("<table border=1>");
        while(rs.next())
        {
            pw.println("<tr><td>"+rs.getInt(1)+"</td></td>+rs.getString(2)+</td>"+"<td>"+rs.getString(3)+"</td></tr>");
        }
    pw.println("</table>");
    pw.close();
}
catch (Exception e){
e.printStackTrace();
}
    // TODO Auto-generated method stub
    response.getWriter().append("Served at: ").append(request.getContextPath());
}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    doGet(request, response);
}

}

的web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>Servlets</display-name>
<servlet> 
<servlet-name>Start</servlet-name> 
<servlet-class>start.Start</servlet-class> 
</servlet> 
<servlet-mapping> 
<servlet-name>Start</servlet-name> 
<url-pattern>/Start</url-pattern> 
</servlet-mapping>
     <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>

现在每当我在服务器上运行index.html时,一切正常,但是按下按钮后它指定了404错误,我不知道为什么会发生这种情况。 我尝试在index.html中使用/ start作为动作,这导致只是指向/ Start的错误消息,但是当我使用Servlets / Start时,它指向Servlets / Servlets / Start,如果这有帮助的话。 输入localhost:8080 / Servlets会提示我输入index.html,然后导致同样的问题

当我慢慢变得非常沮丧时,我想请你帮忙,先谢谢!

2 个答案:

答案 0 :(得分:1)

我已经复制了您的源代码并在eclipse中创建了一个Web项目。 enter image description here

Start.java(名为start的包内)如下: -

package start;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Start
 */
@WebServlet("/Start")
public class Start extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Start() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse respond) throws ServletException, IOException {
        PrintWriter pw = respond.getWriter();
        respond.setContentType("text/html");
        String tb = request.getParameter("table");
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            Connection con = DriverManager.getConnection("jdbc::oracle::thin:@localhost:music", "root", "1234");
            Statement st = con.createStatement();
            System.out.println("connection established successfully!");
            ResultSet rs = st.executeQuery("SELECT * FROM" + tb);

            pw.println("<table border=1>");
            while (rs.next()) {
                pw.println("<tr><td>" + rs.getInt(1) + "</td></td>+rs.getString(2)+</td>" + "<td>" + rs.getString(3)
                        + "</td></tr>");
            }
            pw.println("</table>");
            pw.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        // TODO Auto-generated method stub
        // response.getWriter().append("Served at:
        // ").append(request.getContextPath());
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }
}

已修改web.xml以删除servlet映射,因为它在类本身上以@WebServlet的形式存在。内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>Servlets</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>
</web-app>

index.html(在WebContent文件夹中)如下:

<!DOCTYPE html>
<html>
<form action="Start" method="post">
<font face="verdana" size="2">
 Enter Table Name :<input type="text" name="table"> 
                   <input type="submit" value="Display">
  </font>
</form>
</html>

结果是我可以在http://localhost:8080/Servlets/访问网络应用程序,点击显示按钮,也可以转到下一页。

enter image description here

答案 1 :(得分:0)

在index.html部分中,将操作更改为操作=&#34;开始&#34;。 在web.xml中,将完整的限定类名称放在