在Eclipse下安装Maven插件
点击Help -->Eclipse Marketplace
在搜索栏中输入maven,点击回车
在搜索列表中,找到Maven Integration for Eclipse,点击Install安装。
点击Window -->Properties -->Maven -->Installations --> add 添加本地安装的Maven并选中 -->Apply
再点击User Settings -->Global Settings -->Browse...选择Maven目录下conf文件夹下的setting.xml文件 -->Apply -->OK
点击File --> New --> Other,选择Maven Project
点击Next-->Next,在Filter中输入web,选择下面选项
点击Next,修改Group Id和Artifact Id,点击Finish完成创建。
添加项目文件
HelloServlet.java
package com.basefas;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("Hello World!");
out.flush();
out.close();
}
}
pom.xml
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.basefas</groupId>
<artifactId>helloweb</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>helloweb Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>
javax.servlet
</groupId>
<artifactId>
javax.servlet-api
</artifactId>
<version>
3.0.1
</version>
<scope>
provided
</scope>
</dependency>
<dependency>
<groupId>
javax.servlet.jsp
</groupId>
<artifactId>
jsp-api
</artifactId>
<version>
2.1
</version>
<scope>
provided
</scope>
</dependency>
<dependency>
<groupId>
jstl
</groupId>
<artifactId>
jstl
</artifactId>
<version>
1.2
</version>
<scope>
provided
</scope>
</dependency>
</dependencies>
<build>
<finalName>helloweb</finalName>
<plugins>
<plugin>
<groupId>
org.apache.tomcat.maven
</groupId>
<artifactId>
tomcat6-maven-plugin
</artifactId>
<version>
2.2
</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>
org.apache.tomcat.maven
</groupId>
<artifactId>
tomcat7-maven-plugin
</artifactId>
<version>
2.2
</version>
</plugin>
</plugins>
</build>
</project>
在运行的三角图标的下拉选项中选择Run Configurations...
在Maven Build一栏点击鼠标右键,选择New
在Base directory选择Browse Workspace...选择目标项目
在Goals中输入tomcat7:run,点击Run,程序就可以运行了
在浏览器中输入 http://localhost:8080/helloweb
就可以看到Hello World!显示在浏览器中了
Lv. up!