错误404:使用HelloWorld Servlet不能使用请求的资源 [英] Error 404: The requested resource is not available using HelloWorld servlet
问题描述
我正在编写Java Servlet,但我正在努力获取一个简单的 HelloWorld
示例以使其正常工作.
I am writing a Java Servlet, and I am struggling to get a simple HelloWorld
example to work properly.
HelloWorld.java
类为:
package crunch;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorld extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("Hello World");
}
}
我正在运行Tomcat v7.0,并且已经阅读了类似的问题,答复涉及更改 web.xml中的
.该部分实际上在我的系统中不存在,当我添加它时,仍然会出现相同的问题. invoker
servlet-mapping
部分
I am running Tomcat v7.0, and have already read similar questions, with responses referring to changing the invoker
servlet-mapping
section in web.xml
. This section actually doesn't exist in mine, and when I added it the same problem still occurred.
推荐答案
尝试一下(如果使用Java EE V6)
Try this (if the Java EE V6)
package crunch;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
@WebServlet(name="hello",urlPatterns={"/hello"}) // added this line
public class HelloWorld extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("Hello World");
}
}
现在通过 http://127.0.0.1:8080/yourapp/hello
其中8080是默认的Tomcat端口,而 yourapp
是您的应用的上下文名称
where 8080 is default Tomcat port, and yourapp
is the context name of your applciation
这篇关于错误404:使用HelloWorld Servlet不能使用请求的资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!