我创建了动态项目eclipse并在java资源中添加了包,然后在其中添加了一个Servlet。这是我的课程,我只是在doGet方法中获得了一个问候世界。 package com.helloworldserverlet.serverlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 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.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
现在,当我尝试运行它时,我会给出错误404,要求找不到资源。我是否必须在web.xml中定义servlet。目前我的web.xml是
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>HelloWorld</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
先谢谢
答案 0 :(得分:1)
当我尝试运行它时,我会给出错误404
可能有很多原因,最常见的原因是:
您的应用程序上下文路径是&#34; app&#34;你用它来运行它
http://localhost:8080/HelloWorld
正确的做法是
http://localhost:8080/app/HelloWorld
您的应用程序上下文路径是&#34; /&#34;你用它来运行它
http://localhost:8080/app/HelloWorld
正确的做法是
http://localhost:8080/HelloWorld
您只是拼写了servlet路径错误,路径映射区分大小写。
注意:强>
&#34;应用&#34;可以是你指定的任何名称,我只是用于演示。请向我们提供更多信息,例如您完整的展开项目树和您放置网址的浏览器位置栏。
答案 1 :(得分:0)
您需要在web.xml
中设置更多配置。我有一个来自Spring的示例,在我的配置中我有:
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
...
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
不应该太不同。我认为您应该更改org.springframework.web.servlet.DispatcherServlet
com.helloworldserverlet.serverlet.HelloWorld
,或者更改您的Servlet类。