First Java Web application (configuration of Maven and Tomcat)

Keywords: Java Maven Tomcat

How is the website accessed?

1. Enter a domain name and press enter

2. Check whether there is this domain name mapping under the C:\Windows\System32\drivers\etc\host configuration file of this machine;

① Yes: directly return the corresponding IP address. In this address, there are web programs we need to access, which can be accessed directly

② No: go to the DNS server and return if found. If not, return if not found

1, Introduction to Servlet

  • Servlet is a technology for Sun company to develop dynamic web

  • Sun provides an interface called Servlet in these API s. If you want to develop a Servlet program, you only need to complete two small steps:

    1. Write a class to implement the Servlet interface

    2. Deploy the developed java classes to the web server.

    The Java program that implements the Servlet interface is called Servlet

    get ready:

    Set Maven globally

2, HelloServlet

1. Build an empty Maven project (= = do not check create from archetype = =) and directly next.

2. Delete the src directory. In the future, we will build a module in this project. This empty project is the Maven main project

3. Add sevrlet dependency and jsp dependency in pom.xml

        <!--        servlet rely on-->
        <!-- -->
        <!--        jsp rely on-->

4. Create sub module

5. Change the web.xml of the sub module to the latest version (you can paste the web.xml content in Tomcat folder)

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns=""


6. Create a new java folder and resource folder in the main directory of the sub module

7. Set the java folder as the Sources folder and the resource folder as the resource folder

8. Write a common class to implement the Servlet interface. Here we directly inherit HttpServlet

(GenericServlet class implements Servlet interface, while HttpServlet class inherits GenericServlet class)

public class HelloServlet extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter writer = resp.getWriter();
        writer.println("Hello Servlet");

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);

9. Write Servlet mapping

We write a Java program, but to access it through the browser, and the browser needs to connect to the web server, so we need to register the Servlet we write in the web service and give it a path that the browser can access

Add Servlet and Servlet mapping in the web.xml file of the sub module

<!--register Servlet-->
    <!--Servlet Request path for-->
        <!--The browser enters the path of this mapping-->

10. Configure Tomcat (configure the path of project Publishing)

You can change your name

11. Start Tomcat and the test is successful

Posted by ybinds on Sun, 05 Dec 2021 21:37:47 -0800