Java - Servlet 404错误

5

我是一个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/

enter image description here

当我点击登录时,它会给我一个错误,如下所示:

HTTP状态404 - /Sample/welcome


类型 状态报告

消息 /Sample/welcome

描述 请求的资源不可用。


Apache Tomcat/7.0.62

截图

enter image description here

为什么我会得到这样的错误?

其他细节

服务器:apache-tomcat-7.0.62 IDE:Eclipse Java EE kepler

我尝试解决问题的步骤

我尝试了以下步骤:

  1. 运行Servlet时出现HTTP状态404错误

结果

我没有在WEB-INF文件夹中看到.class文件。

  1. Tomcat在Servlet中给出404错误

结果 在此路径“tomcat7\work\Catalina\localhost\your app”中,work文件夹为空。

请让我知道是否需要发布更多详细信息。

IDE的目录结构

enter image description here


最好能看到项目结构/树形图。可以通过IDE的屏幕截图来展示。 - Stan
@Stan,我已经添加了项目结构的截图。 - oldcode
谢谢。所以“Sample”是您的应用程序名称,servlet映射到应用程序内的“/Sample” URL。我认为您应该尝试将表单提交到URL“Sample/Sample”(将其放入Action属性中)。首先,您可以在浏览器中简单地尝试http://localhost:8080/Sample/Sample。 - Stan
3
很奇怪。你写了 form action = Sample,但它正在寻找 Sample/welcome。尝试先重命名你的Servlet或项目,不要保留它们的相同名称。然后看看会发生什么。 - gprathour
1
点击登录时,URL 将类似于 http://localhost:8080/Sample/Sample?name=StackOverflow,因为您的表单操作是 Sample。 - Hiren
显示剩余6条评论
2个回答

2

你的代码在tomcat 7和eclipse中对我有效。

也许你的index.html并不是你发布的那个,因为它指向的是“Sample/welcome”,而不是“Sample/Sample”。

提示:

  • 确保你发布的版本与你代码中的相同。修改内容并确保正确发布。
  • 直接在浏览器中加载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(你的样例类应该在那里),仔细检查你是否具有那些文件夹中的权限。
  • 将你的项目导出为WAR文件并直接部署到你的tomcat中,无需使用eclipse。
  • 清理/构建你的项目。
  • 在你的包资源管理器下的Servers / server.xml中,你应该声明一个上下文,例如<Context docBase="Sample" path="/Sample" reloadable="false" source="org.eclipse.jst.jee.server:Sample"/></Host> 如果你没有,再次检查那些文件夹中的权限。从头开始添加你的tomcat7服务器。

enter image description here


我已将<form action="Sample" method="get">更改为<form action="Sample/Sample]" method="get">,但仍然出现相同的错误。请告诉我是否有做错什么。 - oldcode
我尝试了你的第二个提示。我仍然得到相同的错误。可能出了什么问题? - oldcode
@Codester 使用 <form action="Sample" method="get"> 请填写您的姓名<input type="text" name="name"><br>,如果您看到了“请填写...”这句话,请让我知道。我认为服务器没有正确地刷新您所做的更改。 - PbxMan
<html> <body> <form action="Sample" method="get"> 请输入您的姓名<input type="text" name="name"><br> <input type="submit" value="登录"> </form> </body> </html> 我仍然收到相同的错误。 - oldcode

1
尝试这个方法,你不需要为任何servlet编写XML文件;)
只需要添加注解@WebServlet("/urlpattern"),并在JSP中从<form action="urlpattern">调用此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();  
}}

希望它有所帮助。

<form action="urlpattern" method="post/get"> - CocoCrisp

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接