Tomcat6 +乌班图 +服务程序
我正在尝试制作servlet。
我已经在 ubuntu 上安装了 tomcat6 以及管理示例和文档。我能够运行提供的示例。但是当我尝试制作自己的 servlet 时,它不起作用。
我按照以下步骤
在根目录下创建了
-ROOT
----myapp
------WEB-INF
---------classes
两个文件,其中一个是index.html,其中有一个按钮和表单上的操作来调用servlet。第二个是.java 文件。我编译了 .java 文件并且 .class 完成了。所以现在树看起来像
-ROOT
----myapp
------index.html
------WEB-INF
---------classes
-----------TestServ.java
-----------TestServ.class
现在我使用 http://localhost:8080/myapp 在浏览器中打开它
它显示带有带有按钮的index.html页面。但是当我点击按钮时,它显示
错误 404: http://localhost:8080/myapp/TestServ 找不到!
我不知道我哪里出了问题。我也设置了CATALINA_HOME。但这个问题仍然持续存在。
I am trying to make servlet.
I have installed tomcat6 on ubuntu with admin examples and docs. I am able to run the examples provided. But when i try to make my own servlet it doesnt work.
I did following steps
Under the ROOT i create folder with
-ROOT
----myapp
------WEB-INF
---------classes
I made two files one is index.html which have a button and action on form to call the servlet. Second is .java file. I compiled the .java file and .class is done. So now tree look like
-ROOT
----myapp
------index.html
------WEB-INF
---------classes
-----------TestServ.java
-----------TestServ.class
Now i open this in browser using http://localhost:8080/myapp
It shows up with index.html page with button. But when i click on the button it says
Error 404:
http://localhost:8080/myapp/TestServ not found !!
I dont know where m going wrong. I have set the CATALINA_HOME too. But still this problem continue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要在WEB-INF目录中创建一个web.xml,并在web.xml中定义servlet映射,以便将myapp/TestServ URL转发到TestServ servlet类。
这里是一个描述 web.xml 的页面,其中包含您需要设置的示例和元素向上。对于您的班级,这些元素可能看起来像这样:
You need to create a web.xml in the WEB-INF directory, and define the servlet mapping in web.xml, so that the myapp/TestServ URL is forwarded to the TestServ servlet class.
Here is a page describing web.xml, and has the example and elements you need to set up. For your class, these elements will probably look something like this:
您不应该在 ROOT 下部署任何代码。
默认包中不应有任何 Java 类。尝试将 TestServ.java 放入包中。
您的部署不应包含任何 .java 文件。
您必须在 web.xml 中正确注册您的 servlet。包括到特定 URL 的映射。
最好的方法是创建一个名为 myapp.war 的 WAR 文件,其中包括 WEB-INF/classes 和 WEB-INF/lib 以及适合您情况的 web.xml。将其放入 Tomcat /webapps 并启动容器。如果您已正确注册 servlet,您应该能够通过 http://localhost:8080/myapp/ 访问它测试服务。
我仔细阅读了部署文档。
You shouldn't be deploying any of your code under ROOT.
You shouldn't have any Java class in the default package. Try putting your TestServ.java in a package.
Your deployment should NOT include any .java files.
You've got to register your servlet properly in web.xml. Include a mapping to a particular URL.
Your best best is to create a WAR file named myapp.war, which includes WEB-INF/classes and WEB-INF/lib and a web.xml for your situation. Put that in the Tomcat /webapps and start the container. If you've registered your servlet properly you should be able to access it via http://localhost:8080/myapp/TestServ.
I'd read the deployment docs carefully.