我是一个servlet编程的新手。
以下是我的样例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");//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>
我的问题
当我右键点击项目,然后选择运行为 -> 在服务器上运行
,选择服务器后,它要求我选择一个服务器,然后我选择要运行的服务器。在Eclipse窗口中打开了一个浏览器窗口,其地址URL为http://localhost:8080/Sample/
当我点击登录
时,它会给我一个错误,如下所示:
HTTP状态404 - /Sample/welcome
类型 状态报告
消息 /Sample/welcome
描述 请求的资源不可用。
Apache Tomcat/7.0.62
截图
为什么我会得到这样的错误?
其他细节
服务器:apache-tomcat-7.0.62 IDE:Eclipse Java EE kepler
我尝试解决问题的步骤
我尝试了以下步骤:
结果
我没有在WEB-INF
文件夹中看到.class
文件。
结果
在此路径“tomcat7\work\Catalina\localhost\your app”中,work
文件夹为空。
请让我知道是否需要发布更多详细信息。
IDE的目录结构
form action = Sample
,但它正在寻找Sample/welcome
。尝试先重命名你的Servlet或项目,不要保留它们的相同名称。然后看看会发生什么。 - gprathour