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

<dependencies>
        <!--        servlet rely on-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/jstl-api -->
        <!--        jsp rely on-->
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.3</version>
        </dependency>
    </dependencies>

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="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>

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 {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter writer = resp.getWriter();
        writer.println("Hello Servlet");
    }

    @Override
    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>
        <servlet-name>hello</servlet-name>
        <servlet-class>com.kuang.servlet.HelloServlet</servlet-class>
    </servlet>
    <!--Servlet Request path for-->
    <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <!--The browser enters the path of this mapping-->
        <url-pattern>/hello</url-pattern>
    </servlet-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