Java - Servlet 404错误 [英] Java - Servlet 404 error
问题描述
以下是我的示例servlet代码
Sample.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); //将返回值
pw.println(Welcome+ name);
pw.close();
}
}
Web.xml / p>
< 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
<!DOCTYPE html PUBLIC - // W3C // DTD XHTML 1.0 Transitional // ENhttp://www.w3.org/TR/xhtml1/DTD /xhtml1-transitional.dtd\">
< html xmlns =http://www.w3.org/1999/xhtml>
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = utf-8/>
< title> Untitled Document< / title>
< / head>
< body>
< form action =Samplemethod =get>
输入你的名字< input type =textname =name>< br>
< input type =submitvalue =login>
< / form>
< / body>
< / html>
我的问题
当我右键单击我的项目,然后单击运行为 - >在服务器上运行
,我选择服务器,它要求我选择一个服务器,然后选择要运行的服务器。在eclipse窗口中,打开一个浏览器窗口,该窗口的地址URL为
结果
我没有看到 .class
WEB-INF
文件夹。
- 直接在浏览器中查看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 =Samplepath =/ Sampleoadable =falsesource =org.eclipse.jst.jee.server:Sample/>< / Host> code>
如果您不仔细检查这些文件夹中的权限。再次启动tomcat7服务器。 - 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=Random directly 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 tomcat without 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.
I am a newbie in servlet programming.
The following is my sample servlet code
Sample.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
<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
<!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/
When I click on login
it gives me an error like this,
HTTP Status 404 - /Sample/welcome
type Status report
message /Sample/welcome
description The requested resource is not available.
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
Steps I tried to solve the problem
I tried these,
Result
I did not see the .class
files in WEB-INF
folder.
Result
In this path "tomcat7\work\Catalina\localhost\your app",the work
folder was empty.
Please let me know if I need to post any more details.
Directory structure from IDE
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.
Tips:
这篇关于Java - Servlet 404错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!