eclipse Java - Servlet 404 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30866782/
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
Java - Servlet 404 error
提问by GeekDroid
I am a newbie in servlet programming.
我是 servlet 编程的新手。
The following is my sample servlet code
以下是我的示例 servlet 代码
Sample.Java
示例.Java
public class Sample extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter pw=res.getWriter();
String name=req.getParameter("name");//will return value
pw.println("Welcome "+name);
pw.close();
}
}
Web.xml
网页.xml
<web-app>
<servlet>
<servlet-name>sampleservlet</servlet-name>
<servlet-class>Sample</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>sampleservlet</servlet-name>
<url-pattern>/Sample</url-pattern>
</servlet-mapping>
</web-app>
Index.html
索引.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form action="Sample" method="get">
Enter your name<input type="text" name="name"><br>
<input type="submit" value="login">
</form>
</body>
</html>
My problem
我的问题
When I right click on my project and then click Run as -> Run on server
, I select the server, it asks me for to select a server and then I select the server to run. In the eclipse window a browser window is opened which has the address URL as http://localhost:8080/Sample/
当我右键单击我的项目然后单击时Run as -> Run on server
,我选择了服务器,它要求我选择一个服务器,然后我选择要运行的服务器。在 eclipse 窗口中打开一个浏览器窗口,其 URL 地址为http://localhost:8080/Sample/
When I click on login
it gives me an error like this,
当我点击login
它时会出现这样的错误,
HTTP Status 404 - /Sample/welcome
HTTP 状态 404 - /Sample/welcome
typeStatus report
类型状态报告
message/Sample/welcome
留言/样品/欢迎
descriptionThe requested resource is not available.
描述请求的资源不可用。
Apache Tomcat/7.0.62
Apache Tomcat/7.0.62
Screenshot
截屏
Why am I getting an error like this ?
为什么我会收到这样的错误?
Other details
其他详情
Server:apache-tomcat-7.0.62 IDE: Eclipse Java EE kepler
服务器:apache-tomcat-7.0.62 IDE:Eclipse Java EE kepler
Steps I tried to solve the problem
我试图解决问题的步骤
I tried these,
我试过这些,
Result
结果
I did not see the .class
files in WEB-INF
folder.
我没有看到.class
文件WEB-INF
夹中的文件。
ResultIn this path "tomcat7\work\Catalina\localhost\your app",the work
folder was empty.
结果在这个路径“tomcat7\work\Catalina\localhost\your app”中,work
文件夹是空的。
Please let me know if I need to post any more details.
如果我需要发布更多详细信息,请告诉我。
Directory structure from IDE
IDE 的目录结构
采纳答案by PbxMan
Your code does work for me in tomcat 7 and eclipse. Perhaps your index.html is not the same as the one your are posting because it's aimnig at Sample/welcome instead of Sample/Sample.
您的代码在 tomcat 7 和 eclipse 中对我有用。也许您的 index.html 与您发布的不同,因为它针对 Sample/welcome 而不是 Sample/Sample。
Tips:
提示:
- Make sure your index.html published version is the same as the one you have in your code. Modify the content and make sure it's publishing properly.
- Load http://localhost:8080/Sample/Sample?name=Randomdirectly in your browser to see that the servlet is working propertly.
- Double click on Tomcat7 on your servers. Go to your Server path. e.g. C:\Java\workspace.metadata.plugins\org.eclipse.wst.server.core\tmp2\wtpwebapps\Sample\WEB-INF\classes (your Sample class should be there) double check if you have permissions in those folders.
- Export your project as a WAR and deploy it directly in your tomcatwithout eclipse.
- Clean/Build your project
- On your Package Explorer under Servers/server.xml a you should have a declared context e.g.
<Context docBase="Sample" path="/Sample" reloadable="false" source="org.eclipse.jst.jee.server:Sample"/></Host>
If you don't double check permisions in those folders. Add your tomcat7 server from the start again.
- 确保您的 index.html 发布版本与您在代码中的版本相同。修改内容并确保其正确发布。
- 直接在浏览器中加载http://localhost:8080/Sample/Sample?name=Random以查看 servlet 是否正常工作。
- 双击服务器上的 Tomcat7。转到您的服务器路径。例如 C:\Java\workspace.metadata.plugins\org.eclipse.wst.server.core\tmp2\wtpwebapps\Sample\WEB-INF\classes(你的 Sample 类应该在那里)仔细检查你是否在这些文件夹中有权限.
- 将您的项目导出为 WAR 并直接在您的tomcat 中部署它,无需 Eclipse。
- 清理/构建您的项目
- 在 Servers/server.xml 下的 Package Explorer 上,您应该有一个声明的上下文,例如,
<Context docBase="Sample" path="/Sample" reloadable="false" source="org.eclipse.jst.jee.server:Sample"/></Host>
如果您不仔细检查这些文件夹中的权限。再次从头添加您的 tomcat7 服务器。
回答by Liger
Try this and you don't need to write the XML file for any servlet ;)
试试这个,你不需要为任何 servlet 编写 XML 文件;)
Just add the annotation @WebServlet("/urlpattern")
and call this URL-pattern inside the JSP from <form action="urlpattern">
只需添加注释@WebServlet("/urlpattern")
并从 JSP 中调用此 URL 模式即可<form action="urlpattern">
Only if you work on Eclipse.
仅当您使用 Eclipse 时。
@WebServlet("/Sample")
public class Sample extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter pw = res.getWriter();
String name = req.getParameter("name"); //will return value
pw.println("Welcome "+name);
pw.close();
}}
Hope it helps.
希望能帮助到你。