2016-02-03 91 views
0

我在Eclipse Mars版本中测试tomcat v8。我创建一个动态Web项目,并创建一个servlet。然而,当我访问的HelloWorld的servlet,该网页是错误404,请如何解决Eclipse中的Java Servlet与Tomcat

enter image description here

的HelloWorld的Servlet

package com.test.servlets; 

/** 
* Servlet implementation class HelloWorld 
*/ 
@WebServlet("/HelloWorld") 
public class HelloWorld extends HttpServlet { 
    private static final long serialVersionUID = 1L; 

/** 
* @see HttpServlet#HttpServlet() 
*/ 
public HelloWorld() { 
    super(); 
    // TODO Auto-generated constructor stub 
} 

/** 
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 
*/ 
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
    // TODO Auto-generated method stub 
    response.setContentType("text/html"); 
    PrintWriter printWriter = response.getWriter(); 
    printWriter.println("<h1>Hello Friends!</h1>"); 
} 

/** 
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 
*/ 
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
    // TODO Auto-generated method stub 
} 

} 
+0

你可以尝试清洁和构建?并且还请正确检查URL – mmuzahid

+0

我的URL:localhost:8080/TomcatTest/HelloWorld – Frank2018

+0

请检查此链接http://www.coreservlets.com/Apache-Tomcat-Tutorial/tomcat-7-with-eclipse.html#undefined。在此链接中检出此行将ROOT(默认)Web应用程序复制到Eclipse中。 –

回答

0

首先,缩小问题。

该应用程序是否正确启动?它是否部署在上下文路径/ TomcatTest中?您可以使用Tomcat的管理器用户界面来检查这两件事情。要访问Manager UI,必须先启用它:在Eclipse中展开Tomcat 8服务器节点并查找文件/conf/tomcat-users.xml。添加以下行:

<user username="tomcat" password="password" roles="standard,manager-script, manager-ui" /> 

详情参见以下内容:

https://tomcat.apache.org/tomcat-8.0-doc/manager-howto.html#Configuring_Manager_Application_Access

重新启动服务器,并击中了以下内容:

http://localhost:808/manager/html

你应该应用中看到你列为跑步。如果列出但未运行,则需要查看Tomcat的日志文件,以查看阻止该应用启动的原因。

如果应用程序已列出并正在运行。检查你可以打http://localhost:8080/TomcatTest/index.jsp。如果是这样,那么应用程序将正确部署,并且问题会出现在servlet映射中。