Java Http 状态 404 -/请求的资源不可用

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

Http status 404 -/the requested resource is not available

javatomcatservlets

提问by Kiranyls

I have written a servlet program for login page ..am using tomcat server...after i run on the server am getting the above mentioned error... Below is my servlet code.

我已经为登录页面编写了一个 servlet 程序..我正在使用 tomcat 服务器...在我在服务器上运行后出现上述错误...下面是我的 servlet 代码。

package demo;

包演示;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
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 LoginServlet
 */

public class LoginServlet extends HttpServlet {

    static private String dbUrl="jdbc:mysql://localhost:3306/employee";
    static private String dbUn="root";
    static private String dbPwd="root";
    static private Connection ConObj;
    static private Statement StmtObj;
    static private ResultSet RsObj; 


  public void service(HttpServletRequest request,HttpServletResponse response)throws IOException


  {
      try {
        Class.forName("com.mysql.jdbc.driver");

      ConObj=DriverManager.getConnection(dbUrl, dbUn, dbPwd);
      StmtObj=ConObj.createStatement();
      response.setContentType("text/html");
      PrintWriter out=response.getWriter();
      out.write("<html><body>");
      out.write("<h2>");
      String ActLogName=request.getParameter("Logname");
      String ActPwd=request.getParameter("Pwd");
      String SqlQuery="select * from users where username='"+ActLogName+"' and password='"+ActPwd+"'";
      RsObj=StmtObj.executeQuery(SqlQuery);
      if(RsObj.next()==true)
      {
          String ExpLogName=RsObj.getString("username");
          String ExpPwd=RsObj.getString("password");
          if(ActLogName.equals(ExpLogName)&& ActPwd.equals(ExpPwd))
          {
              out.write("Login Success");
          }
      }
      else
      {
          out.write("Login Failed");
      }
      out.write("</h2>");
      out.write("</body></html>");
      }
      catch (ClassNotFoundException|SQLException exp) {

            exp.printStackTrace();
        }

      finally{
          try {
            RsObj.close();
            StmtObj.close();
            ConObj.close();
        } catch (SQLException e) {

            e.printStackTrace();
        }

      }


  }
}

Below is my html code

下面是我的html代码

<!DOCTYPE html>
<html>
<head>

<title>login page</title>
</head>
<body>
<form action="http://localhost:8080/FlipKart/loginpage">

LoginName :<input type="text" name="Logname"><br>
Password :<input type="password" name="Pwd"><br>
<input type="submit" value="Login">
<input type="button" value="cancel">

 </form>

</body>
</html>

Below is my web.xml code

下面是我的 web.xml 代码

<web-app>
<servlet>
<servlet-name>loginserv</servlet-name>
<servlet-class>LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>loginserv</servlet-name>
<url-pattern>/loginpage</url-pattern>]
</servlet-mapping>
</web-app>

Can somebody help me to the resolve this issue.. Thanks in advance..:)

有人可以帮我解决这个问题.. 提前致谢..:)

采纳答案by Alexandre Lavoie

You are overriding the wrong method, override doPost()instead and modify your form to include method="post"like this :

您正在覆盖错误的方法,doPost()改为覆盖并修改您的表单以包含method="post"如下内容:

public void doPost(HttpServletRequest request,HttpServletResponse response)throws IOException
{
    //...
}

and this

和这个

<form action="http://localhost:8080/FlipKart/loginpage" method="post">

Also, if you don't want to always include the full path of the context manually, you can read this.

此外,如果您不想总是手动包含上下文的完整路径,您可以阅读此内容

回答by popalka

Wrong reference to class in web.xml Use full name of Class with package name

web.xml 中对类的错误引用 使用类的全名和包名

<servlet>
<servlet-name>loginserv</servlet-name>
<servlet-class>demo.LoginServlet</servlet-class>
</servlet>

回答by Sujit Joshi

Did you restart the server after making the package name change? Tomcat reads web.xml only on startup. Besides, make sure your classes are under webapps/WEB-INF/classesfolder.

更改包名称后是否重新启动了服务器?Tomcat 仅在启动时读取 web.xml。此外,请确保您的课程在webapps/WEB-INF/classes文件夹下。

回答by Mukus

It looks to me as though your application context needs to be changed. Perhaps it is being set to root. So it is not hitting /FlipKart

在我看来,您的应用程序上下文似乎需要更改。也许它被设置为root。所以它没有击中 /FlipKart

Look at thisstackoverflow link for more information on setting the application context.

查看stackoverflow 链接以获取有关设置应用程序上下文的更多信息。

That is for tomcat 7. I know you can edit the META-INF/context.xml for previous versions. If you don't have that folder and file just create it.

那是针对 tomcat 7 的。我知道您可以编辑以前版本的 META-INF/context.xml。如果您没有该文件夹和文件,只需创建它。

EDIT Check your tomcat log to see where it is getting deployed or if something caused it to not get deployed at all. If you're running from an IDE, you may be able to see from the console.

编辑检查您的 tomcat 日志以查看它在哪里被部署,或者是否有什么原因导致它根本没有被部署。如果您从 IDE 运行,您可能能够从控制台看到。