We will create a web project:
1. Create a project MVN archetype: Create-dgroupid = org. sonatype. mavenbook. ch05-dartifactid = simple-webapp-dpackagename = org. sonatype. mavenbook-darchetypeartifactid = Maven-Archetype-Web
The red part indicates that the project is a web project. The result is: <packaging> war </packaging> In the Pom. xml file.
2. You can use the maven jetty plug-in to run your web application in Maven. Therefore, you need to configure the maven jetty plug-in the Pom. xml file of the project.
<Build> <br/> <finalname> simple-webapp </finalname> <br/> <plugins> <br/> <plugin> <br/> <groupid> Org. mortbay. jetty </groupid> <br/> <artifactid> Maven-jetty-plugin </artifactid> <br/> </plugin> <br/> </plugins> <br/> </build>
Use MVN jetty: run the project started jetty server (run MVN compile to compile your project before starting the jetty plug-in)
Http: // localhost: 8080/simple-webapp/view the default index. jsp page. Hello world. The index. jsp is located under webapp.
Add the package and servlet. However, compiling with MVN compile will fail, because the project does not have a J2EE library currently!
To compile a servlet, we need to add the servlet API as the project dependency. Servlet specification description is a jar file that can be downloaded from the site of Sun Microsystems to http://java.sun.com/products/servlet/download.html. After downloading the JAR file, you need to install it in ~ /. M2/Repository Maven local repository.
But now we don't have to worry about it: we can use Apache Geronimo. The configuration is as follows:
<Dependency> <br/> <groupid> Org. apache. geronimo. specs </groupid> <br/> <artifactid> geronimo-servlet_2.4_spec </artifactid> <br/> <version> 1.1.1 </version> <br/> <scope> provided </Scope> <br/> </dependency>
It is also worth noting that our dependency uses the provided range. This range indicates that the mavenjar file has been "supplied" by the container and therefore does not need to be included in the war.
<Dependency> <br/> <groupid> Org. apache. geronimo. specs </groupid> <br/> <artifactid> geronimo-jsp_2.0_spec </artifactid> <br/> <version> 1.1 </version> <br/> <scope> provided </Scope> <br/> </dependency>
After adding the servlet specification dependency, run MVN clean install and then run MVN jetty: Run.
Run servlet...