Maven (3)-use intellij idea to create a maven web project, intellijmaven
This article uses an example to describe how to use maven to build a web project. Development Tool: intellij idea.
1. Create a maven Project
Select Create from archetype. Create a project from an existing maven template. Select maven-archetype-webapp template this time. After this template is selected, the generated project will contain the files required by webapp. Convenient. You can also create a blank maven project without selecting this option, and manually add the files required by the web.
Enter maven coordinates for the project
2. Configure the maven project structure
Generate a maven Project
The above project only contains the directory structure required by the web-app and the basic maven project element: pom. The standard maven project structure is as follows:
The complete project directory:
3.Add a Servlet
This Servlet function is very simple: Output a sentence.
package com.cnblogs.kmpp;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;import java.io.PrintWriter;public class WebAppServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); out.println( "WebAppServlet Executed" ); out.flush(); out.close(); }}
The Servlet above has a dependency on J2EE and needs to add a dependency on j2ee in pom.
<dependency> <groupId>org.apache.geronimo.specs</groupId> <artifactId>geronimo-servlet_3.0_spec</artifactId> <version>1.0</version></dependency>
Configure servlet
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" ><web-app> <display-name>Archetype Created Web Application</display-name> <servlet> <servlet-name>WebApp</servlet-name> <servlet-class>com.cnblogs.kmpp.WebAppServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>WebApp</servlet-name> <url-pattern>/simple-servlet</url-pattern> </servlet-mapping></web-app>
ThuRun the web app
The running of web projects can be implemented through tomcat, but now maven has been used to implement this function through a maven plug-in: jetty (Jetty is an open-source servlet container, it provides runtime environments for Java-based web containers, such as JSP and servlet ).
Run jetty: locate the command line in the directory of the current project and enter the following command:
The result is as follows:
Jetty is started at this time.
Enter http: // localhost: 8080/web-app/in the browser /.
This interface is the default index. jsp page of web-app.
The above shows the default output. A simple servlet has been defined and configured in web. xml. This can also be run. Enter http: // localhost: 8080/web-app/simple-servlet in the browser
The above page output is previously implemented in WebAppServlet.
So far, a maven web project has been created and runs successfully.