Java servlet 错误请求的资源 () 不可用

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

servlet error The requested resource () is not available

javaservlets

提问by user2502227

I am new to servlets .I am using eclipse juno for this.I am having a trouble in running my program..My code is

我是 servlets 的新手。我为此使用了 eclipse juno。我在运行我的程序时遇到了麻烦..我的代码是

package sTraining;

import java.io.*;
import javax.servlet.*;

public class Servlet1 implements Servlet{
ServletConfig config=null;

public void init(ServletConfig config){
this.config=config;
System.out.println("servlet is initialized");
}

public void service(ServletRequest req,ServletResponse res)
throws IOException,ServletException{

res.setContentType("text/html");

PrintWriter out=res.getWriter();
out.print("<html><body>");
out.print("<b>hello simple servlet</b>");
out.print("</body></html>");

}
public void destroy(){System.out.println("servlet is destroyed");}
public ServletConfig getServletConfig(){return config;}
public String getServletInfo(){return "copyright 2007-1010";}

} 

I am getting this error[http://localhost:8080/Test/WEB-INF/classes/sTraining/Servlet1.java][1] although i have this thing in my web .xml file

我收到这个错误[http://localhost:8080/Test/WEB-INF/classes/sTraining/Servlet1.java][1] 虽然我的 web .xml 文件中有这个东西

<servlet>
    <description></description>
    <display-name>Servlet1</display-name>
    <servlet-name>Servlet1</servlet-name>
    <servlet-class>servlet.Servlet1</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Servlet1</servlet-name>
    <url-pattern>/Servlet1</url-pattern>
  </servlet-mapping>

why this is not running? My code is fine. First time when I run this page it run, but running this program after my second program it did not run and that second program also not run.

为什么这不运行?我的代码没问题。当我第一次运行这个页面时,它运行,但在我的第二个程序之后运行这个程序它没有运行,第二个程序也没有运行。

回答by Sotirios Delimanolis

Why are you accessing

你为什么访问

http://localhost:8080/Test/WEB-INF/classes/sTraining/Servlet1.java 

? You should be accessing

? 你应该访问

http://localhost:8080/Test/Servlet1

Read the above as

阅读以上内容

[protocol or scheme] :// [host] : [port] / [context] / [servlet mapping]

Also, according to the source code you've posted. The Servlet1class is in package sTraining. Your web.xmlshould therefore have

另外,根据您发布的源代码。该Servlet1班是包sTrainingweb.xml因此你应该有

<servlet>
    <description></description>
    <display-name>Servlet1</display-name>
    <servlet-name>Servlet1</servlet-name>
    <servlet-class>sTraining.Servlet1</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>Servlet1</servlet-name>
    <url-pattern>/Servlet1</url-pattern>
</servlet-mapping>


A ServletContainer will not make anything in the WEB-INFfolder available to client requests.

一个Servlet集装箱不会让任何东西WEB-INF提供给客户端请求的文件夹。



What you are doing is not great practice. Your class should probably extend HttpServletto get some standard HTTP behavior. You also shouldn't be writing HTML in Java code. Try reading the tutorial and references we have on Stackoverflow, here.

你正在做的不是很好的练习。您的类可能应该扩展HttpServlet以获得一些标准的 HTTP 行为。您也不应该在 Java 代码中编写 HTML。尝试阅读我们在 Stackoverflow 上的教程和参考资料,这里

回答by KhAn SaAb

Put ./Servlet1in your form action attribute

放入./Servlet1您的表单操作属性

<form action="./Servlet1">
....
</form>

and check your web.xmlyour package name is different

并检查您web.xml的包裹名称是否不同

    <servlet>
    <description></description>
    <display-name>Servlet1</display-name>
    <servlet-name>Servlet1</servlet-name>
    <servlet-class>sTraining.Servlet1</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Servlet1</servlet-name>
    <url-pattern>/Servlet1</url-pattern>
  </servlet-mapping>