The first Maven application

Outline

Using Maven to create a Java Web application

Maven project

Select File-> New->Project...

01

Select Mavenitems

02

Fill Project Information

03

Select Workspace

04

Directory Structure

Maven Java Web's basic structure is as follows:

├─src
│  ├─main
│  │  ├─java
│  │  ├─resources
│  │  └─webapp
│  │      └─WEB-INF
│  └─test
│      └─java

Structure Description:

  • src: Source directory
    • src/main/java: Java source directory
    • src/main/resources: Resource File Directory
    • src/main/webapp: Web Related Category
    • src/test:unit test

IDEA Maven project management

On the right side IDEA interface Maven Projectsoptions, Maven can manage a project throughout the life cycle, plug-ins, dependence

05

Improving Java Web program

Pom.xml configuration modifications, as follows:

06

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.bjio</groupId>
    <artifactId>Hello-Maven</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>war</packaging>

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

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>log4j-over-slf4j</artifactId>
            <version>1.7.25</version>
        </dependency>
    </dependencies>
</project>

Configuration instructions:

  • packaging: Packing way, here is the war package, expressed as a Java Web application
  • dependencies: The project relies configuration required for the entire project life cycle of dependence are configured here

Create a test Servlet

Create a Servletused test request

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Hello Servlet</title>
</head>
<body>
Hello Servlet
</body>
</html>

Configuring web.xml

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

    <servlet>
        <servlet-name>HelloServlet</servlet-name>
        <servlet-class>com.bjio.HelloServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>HelloServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

Create a class HelloServlet

package com.bjio;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class HelloServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("/index.jsp");

    }

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


    }
}

Test Run

Note: To configure the End Tomcat(IDEA have about how to configure Tomcat) to run direct, open the browser to access http: // localhost: 8080 is shown below:

07

Guess you like

Origin www.cnblogs.com/bjio/p/11681952.html