How to write a servlet program JavaWeb ------

1.

Here Insert Picture Description

2.

Here Insert Picture Description

3.

Here Insert Picture Description

4.

Here Insert Picture Description

5. Add dependent servlet

<dependencies>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>4.0.1</version>
    </dependency>
</dependencies>

6.

Here Insert Picture Description

7.

Here Insert Picture Description

8.

Here Insert Picture Description

9.

Here Insert Picture Description

10.

Here Insert Picture Description

11.

Here Insert Picture Description

12. web.xml changes to the latest version

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0"
         metadata-complete="true">
  
</web-app>

13. New resources and two packages java

Here Insert Picture Description

14.

Here Insert Picture Description

15. The construction of the package, write code

Here Insert Picture Description

public class HelloServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("进入doGet方法");

        PrintWriter writer = resp.getWriter();
        writer.print("I MISS YOU");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

16. The preparation of the servlet mapping Web.xml

<servlet>
    <servlet-name>helloServlet</servlet-name>
    <servlet-class>com.yang.servlet.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>helloServlet</servlet-name>
    <url-pattern>/hello</url-pattern>
</servlet-mapping>

17.

Here Insert Picture Description

18.

Here Insert Picture Description

19.

Here Insert Picture Description

20.

Here Insert Picture Description

21. Start Tomcat to enter index.jsp

Here Insert Picture Description

22. The access path we set when mapping

Here Insert Picture Description

Published 80 original articles · won praise 7 · views 4767

Guess you like

Origin blog.csdn.net/y18791050779/article/details/104879988