suchen

Heim  >  Fragen und Antworten  >  Hauptteil

java - servlet3.1注解不生效

我新建了个servlet项目:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

<code>package hello;

 

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 HelloServlet

 */

@WebServlet("/Hello")

public class HelloServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

 

    /**

     * Default constructor.

     */

    public HelloServlet() {

        // 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);

    }

 

}

</code>

然后访问http://localhost:8080/hello/H...显示404

但访问别的jsp页面正常
然后我加了个web.xml文件:

1

2

3

4

5

6

7

8

<code><?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"

  version="3.1">

 

</web-app>

</code>

就能访问了

但是我再新建个servlet文件,新建的却还是无法访问

1

2

3

<code>@WebServlet("/World")

public class WorldServlet extends HttpServlet {

</code>

为什么会这样啊?难道不能用注解了?
我用的tomcat 8.5 , Java 1.8 , servlet 3.1 , eclipse 4.6.0 (Neon)
求解注解为什么不能用啊


项目结构:

伊谢尔伦伊谢尔伦2809 Tage vor678

Antworte allen(2)Ich werde antworten

  • PHPz

    PHPz2017-04-18 10:51:22

    把你整个项目结构发出来看下

    Antwort
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-18 10:51:22

    开始放错图片,以修正

    Antwort
    0
  • StornierenAntwort