我使用Tomcat 8.0,但在尝试运行应用程序时(http://localhost:8080/example1/services/Calculator)在URL中,我总是提示HTTP状态404。1.我转到Tomcat服务器8.0属性,并将位置更改为"Tomcat 8.0v localhost-server"。
-
我还双击Tomcat服务器,并将服务器位置更改为"使用Tomcat安装"。
-
我复制了ROOT文件夹并粘贴到我的项目文件夹中(proj文件夹.metadata.plugins\org.eclipse.wst.server.core\tmp0)。
然而,结果与这里的图片相同。HTTP状态错误
如果能在这个问题上得到帮助,我将不胜感激。
尝试更改web.xml以包含以下
<servlet-mapping>
<servlet-name>YOUR-APP-NAME</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
url模式中的/将告诉Tomcat在应用程序根文件夹中查找任何url(您可以更改项目名称以匹配主目录的名称)。例如,如果您想访问您的应用程序http://localhost:8080/Calculator,然后将Eclipse中的项目重命名为Calculator。
现在您只需要解析传递给servlet的实际URL(在doGet和doPost方法中)
例如
public void doGet(HttpServletRequest request, HttpServletResponse
response) throws IOException, ServletException {
String url = request.getRequestURL().toString();
String page = url.substring(url.lastIndexOf("/"));
if("some_page".equals(page)){
//do something
}else if("some_other_page".equals(page)){
//do something else
}else{
//open default page
getServletContext().getRequestDispatcher("/Home.jsp").forward(request, response);
}
}