2015-06-16 113 views
3

我是servlet编程的新手。Java - Servlet 404错误

以下是我的样本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/

enter image description here

当我点击login它给了我这样的错误,

HTTP状态404 - /采样/欢迎


状态报告

消息 /样品/欢迎

描述所请求的资源不可用。


的Apache Tomcat/7.0.62

截图

enter image description here

为什么会出现这样的错误?

其他细节

服务器:Apache-Tomcat的7.0。62 IDE:Eclipse的Java EE的开普勒

步骤我试图解决这个问题

我尝试了这些,

  1. Getting HTTP Status 404 error when trying to run servlet

结果

我没有请参阅中的.class文件文件夹。

  • Tomcat giving 404 error with servlets
  • 结果 在此路径 “tomcat7 \工作\卡塔利娜\本地主机\应用程式” 时,work文件夹是空的。

    请让我知道,如果我需要发布任何更多的细节。从IDE

    目录结构

    enter image description here

    +0

    这将是有益的,看看项目结构/树。也许从IDE的屏幕截图 – Stan

    +0

    您是否在'web.xml'文件中输入了一个条目? –

    +0

    @Stan我添加了项目结构的截图。 –

    回答

    2

    你的代码在tomcat 7和eclipse中对我有用。 也许您的index.html与您发布的index.html不一样,因为它是针对Sample/welcome而不是Sample/Sample。

    提示:

    • 确保您的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而不是日食。
    • 清理/构建您的项目
    • 在Servers/server.xml下的Package Explorer中,您应该有一个声明的上下文,例如<Context docBase="Sample" path="/Sample" reloadable="false" source="org.eclipse.jst.jee.server:Sample"/></Host> 如果你不仔细检查这些文件夹中的权限。再次从头开始添加您的tomcat7服务器。

    enter image description here

    +0

    我改变了'

    '到'',我得到了上述相同的错误。请让我知道如果我做错了什么。 –

    +0

    我试过了你的第二个技巧。我仍然收到同样的错误。什么可能是错的? –

    +0

    @Codester使用 请输入您的名字
    反而让我知道如果您看到“Please,Enter ...“我认为服务器没有正确刷新您的更改。 – PbxMan

    0

    试试这个,你不需要编写任何servlet的XML文件;)

    只需添加注释@WebServlet(“/ urlpattern“),并在JSP中调用这个url模式<form action="urlpattern">

    只有在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(); 
    }} 
    

    希望它有帮助。

    +0