eclipse Apache Tomcat 7 - 请求的资源不可用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27892629/
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-09-19 22:19:24  来源:igfitidea点击:

Apache Tomcat 7 - The requested resource is not available

javaeclipsetomcatservlets

提问by bork

I'm getting an error in my browser when trying to run a servlet. It says

尝试运行 servlet 时,我的浏览器出现错误。它说

The requested resource is not available.

请求的资源不可用。

and

"HTTP Status 404-". The console doesn't print any errors but one warning: " [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:CDM' did not find a matching property."

“HTTP 状态 404-”。控制台不打印任何错误,但有一个警告:“ [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:CDM' 没有找到匹配属性。”

It feels like I've tried everything to get it to work so I'm open to any suggestion that will make my code work.

感觉就像我已经尝试了一切让它工作,所以我愿意接受任何能让我的代码工作的建议。

StudentResultRegistered.java

学生结果注册.java

package com.cdm;


import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.*;




@WebServlet(name="studentResultRegistered",urlPatterns={"/herpDerp"})
public class StudentResultRegistered extends HttpServlet {



    @Override
  public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
      throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();

    String s=request.getParameter("studentID");  
    String k=request.getParameter("kursKod");  
    String t=request.getParameter("termin");  
    String p=request.getParameter("provNr"); 
    String b=request.getParameter("betyg"); 



    try{
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost/school_server", "testuser", "testuser");



        String sql = "INSERT INTO regStudentResult (studentID, kursKod, termin, provNr, betyg)" +
                "VALUES (?, ?, ?, ?, ?)";

        PreparedStatement ps = con.prepareStatement(sql);

        ps.setString(1,s);  
        ps.setString(2,k);  
        ps.setString(3,t);  
        ps.setString(4,p);
        ps.setString(5,b);

        int i=ps.executeUpdate();  
        if(i>0)  
        out.print("success");  
    }catch (Exception e2) {System.out.println(e2);}  

    out.close();  
    } 


}//studentResultRegistered

index.html

索引.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Course Data Management</title>
</head>
<body>
<h1>Registrera Resultat</h1>

<FORM ACTION="/studentResultRegistered" method="get">
  StudentID:
  <INPUT TYPE="TEXT" NAME="studentID" VALUE=""><BR>

  Kurskod:
  <INPUT TYPE="TEXT" NAME="kursKod" VALUE=""><BR>

  Termin:
  <INPUT TYPE="TEXT" NAME="termin" VALUE=""><BR>

  ProvNr:
  <INPUT TYPE="TEXT" NAME="provNr" VALUE=""><BR>

  Betyg:
  <INPUT TYPE="TEXT" NAME="betyg" VALUE=""><BR>


    <INPUT TYPE="SUBMIT" VALUE="Registrera Resultat">
</FORM>

</body>
</html>

web.xml

网页.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_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>CDM</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>  
  </welcome-file-list>



 <servlet>
    <servlet-name>studentResultRegistered</servlet-name>
    <servlet-class>com.cdm;</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>studentResultRegistered</servlet-name>
    <url-pattern>/herpDerp</url-pattern>
</servlet-mapping>

</web-app>

回答by yurgis

I think you need a full class name not just a package:

我认为你需要一个完整的类名而不仅仅是一个包:

<servlet-class>com.cdm.StudentResultRegistered</servlet-class>

EDIT:

编辑:

Additional problems to be fixed:

需要修复的其他问题:

  1. index.html should be placed in the web folder (parent of WEB-INF)
  2. should be accessed as http://localhost/index.htmlor http://localhost/CDM/index.htmldepending on how the web context is configured (see deploy webapp from Eclipse to Tomcat root context). Note that index.html is not required in the url, as it is already specified as a default page in web.xml
  3. Action form url should match the servlet path - not the servlet name.
  1. index.html 应该放在 web 文件夹中(WEB-INF 的父文件夹)
  2. 应该作为http://localhost/index.htmlhttp://localhost/CDM/index.html取决于 Web 上下文的配置方式来访问(请参阅将 webapp 从 Eclipse 部署到 Tomcat 根上下文)。请注意,url 中不需要 index.html,因为它已在 web.xml 中指定为默认页面
  3. 操作表单 url 应该匹配 servlet 路径 - 而不是 servlet 名称。

回答by NickJ

Just saw your comment saying the URL in your browser is

刚看到你的评论说浏览器中的 URL 是

localhost/CDM/WEB-INF/index.html

WEB-INF is a hidden directory - Tomcat (or any other Servlet container) will never serve content from that directory, it should contain confirguation, classes and jars.

WEB-INF 是一个隐藏目录 - Tomcat(或任何其他 Servlet 容器)永远不会从该目录提供内容,它应该包含配置、类和 jar。

Your index.html file should be in the application root (in CDM directory) and your URL should be

您的 index.html 文件应该在应用程序根目录中(在 CDM 目录中)并且您的 URL 应该是

localhost/CDM/index.html