閑來無事做,自己動(dòng)手創(chuàng)建一個(gè)Maven-Web工程,同時(shí)也算是鞏固一下基礎(chǔ)吧,廢話不多說直接進(jìn)入正題。



創(chuàng)建完成后是這個(gè)樣子的

這時(shí)候報(bào)錯(cuò)是因?yàn)?The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
表示沒有servlet-api的依賴包
我們只需要在pom.xml文件中引入。如下圖所示。






點(diǎn)擊finish 這樣一個(gè)Java源文件就建好了,同上幾步建立自己的test文件。
最終建好的工程結(jié)構(gòu)變成這樣子。

一般建好之后Jdk都是默認(rèn)1.5的,在這里我選擇自己的1.8版本。具體的更換Library的過程就不在贅述。
第三步:創(chuàng)建servlet

然后在自己創(chuàng)建的servlet的doGet方法加入如下代碼

第四步:修改pom文件
這時(shí)候還沒有配置tomcat插件
在pom.xml文件中加入
<build>
<sourceDirectory><u>src</u>/main/java</sourceDirectory>
<plugins>
<plugin>
<artifactId><u>maven</u>-compiler-<u>plugin</u></artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!-- Embedded Apache <u>Tomcat</u> required for testing war -->
<plugin>
<u><groupId>org.apache.tomcat.maven</groupId></u>
<u><artifactId>tomcat7-maven-plugin</artifactId></u>
<u><version>2.2</version></u>
<configuration>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
第五步:運(yùn)行工程
然后右鍵工程名maven-web ->鼠標(biāo)選擇Maven->點(diǎn)擊Update Project
最后右鍵工程名maven-web -> Run As ->第二個(gè)Maven build ->
填寫 tomcat7:run –>Run



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.huser</groupId>
<artifactId>maven-web</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>maven-web 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>servlet-api</artifactId>
<version>2.5</version>
<!-- 編譯測(cè)試期間有效 -->
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!-- Embedded Apache Tomcat required for testing war -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
</project>