[英]Can't get servlets to work in Tomcat 6
I am a Java beginner and trying to make my first example work. 我是Java初学者,正在尝试使我的第一个示例工作。
I have installed Tomcat6.0 and using Eclipse on Windows . 我已经安装了Tomcat6.0并在Windows上使用Eclipse 。
I have placed HelloWorld
folder in webapps
. 我已将HelloWorld
文件夹放置在webapps
。 In WEB-INF
have placed classes folder and web.xml
. 在WEB-INF
中放置了类文件夹和web.xml
。
When I place this as the URL: http://localhost:8080/HelloWorld/HelloWorld
当我将其放置为URL时: http://localhost:8080/HelloWorld/HelloWorld
I get the following error: 我收到以下错误:
HTTP Status 404: The requested resource () is not available. HTTP状态404:请求的资源()不可用。
When I try http://localhost:8080
it works fine and gives access to Tomcat's home page and I can execute the example from there. 当我尝试http://localhost:8080
它可以很好地工作并可以访问Tomcat的主页,我可以从那里执行示例。
My web.xml
is: 我的web.xml
是:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- <!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd"> -->
<web-app>
<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>
</web-app>
My HelloWorld.java is : 我的HelloWorld.java是:
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 {
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
pw.println("<html>");
pw.println("<head><title>Hello World</title></title>");
pw.println("<body>");
pw.println("<h1>Hello World</h1>");
pw.println("</body></html>");
}
}
Please help. 请帮忙。 I am stuck on this from two days. 我从两天开始就一直坚持下去。 EDIT: Solved the problem. 编辑:解决了问题。 Thanks. 谢谢。 I added HelloWorld in web.xml and it worked. 我在web.xml中添加了HelloWorld,它可以正常工作。 Thanks for the help. 谢谢您的帮助。
What you need to have for this to work is the following: 为此,您需要具备以下条件:
1) Create a folder HellowWorld inside $CATALINA_HOME/webapps
directory 1)在$CATALINA_HOME/webapps
目录中创建一个文件夹HellowWorld
2) Create a folder named WEB-INF
inside the HellowWorld directory and place in the web.xml
exactly as you have given it to us. 2)在HellowWorld目录中创建一个名为WEB-INF
的文件夹,并将其完全按照您给我们的名称放置在web.xml
。
3) Place HellowWorld.class
(not the .java) inside WEB-INF/classes
(exactly as you have given it to us) 3)将HellowWorld.class
(而不是.java)放置在WEB-INF/classes
(完全是您提供给我们的)
4) Then Start your tomcat server that listens on port 8080 (preferable a clean installation). 4)然后启动侦听端口8080的tomcat服务器(最好是全新安装)。
5) Call http://localhost:8080/HelloWorld/HelloWorld
(directly from browser's url or from a form with action=get
) 5)调用http://localhost:8080/HelloWorld/HelloWorld
(直接从浏览器的网址或具有action=get
的表单中)
6) Now it should work fine (works on my tomcat7) 6)现在应该可以正常工作了(可以在我的tomcat7上运行)
If you have anything different in your configuration then that is what causes the problem. 如果您的配置有任何不同,那就是导致问题的原因。
请将HelloWorld文件夹放在webapps文件夹之外。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.