我是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>我的问题
当我右键单击我的项目,然后单击Run as -> Run on server,我选择服务器,它要求我选择一个服务器,然后选择要运行的服务器。在eclipse窗口中,将打开一个浏览器窗口,其地址URL为http://localhost:8080/Sample/。

当我点击login时,会出现这样的错误,
HTTP状态404 -/Sample/欢迎
类型状态报告
消息/Sample/欢迎
description请求的资源不可用。
Apache /7.0.62
截图

为什么我会犯这样的错误?
其他细节
服务器:apache-tomcat-7.0.62 IDE: Eclipse开普勒
我试图解决这个问题的步骤
我试过这些,
结果
我没有在.class文件夹中看到WEB-INF文件。
结果在这个路径“tomcat7 7\work\Catalina\localhost\your app”中,work文件夹为空。
如果我需要更多的细节,请告诉我。
IDE的目录结构

发布于 2015-06-16 12:45:49
在tomcat 7和eclipse中,您的代码确实适用于我。也许你的index.html和你发布的不一样,因为它的目标是样本/欢迎,而不是样本/样本。
提示:
<Context docBase="Sample" path="/Sample" reloadable="false" source="org.eclipse.jst.jee.server:Sample"/></Host>。重新添加您的tomcat7服务器。

发布于 2015-06-16 12:02:39
尝试这样做,您不需要为任何servlet编写XML文件;)
只需添加注释@WebServlet("/urlpattern")并从<form action="urlpattern">调用JSP内部的URL-模式即可。
除非你在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();
}}希望能帮上忙。
https://stackoverflow.com/questions/30866782
复制相似问题