1. 新建maven项目,选择maven骨架maven-archetype-webapp来建立web项目
2. 选择next,输入groupid:MavenWebTest, artifactid:cn.test
3. 建好后在项目区域出现的MavenWebTest的目录结构
如下图:
4. 默认建立没有java的目录,我们来建立一个文件夹来存放java类
5. 修改项目pom文件
加入servlet-api.jar,另再加入jetty插件的配置,使得当web项目开发完后能用Jetty容器来运行。
pom的配置信息如下:访问端口为9111
<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>cn.test</groupId>
<artifactId>MavenWebTest</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>mavenWeb 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>3.0-alpha-1</version>
</dependency>
</dependencies>
<build>
<finalName>mavenWeb</finalName>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>8.1.3.v20120416</version>
<configuration>
<webApp>
<contextPath>/</contextPath>
</webApp>
<connectors>
<connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
<port>9111</port>
<maxIdleTime>60000</maxIdleTime>
</connector>
</connectors>
<scanTargetPatterns>
<scanTargetPattern>
<directory>src/main/webapp/WEB-INF</directory>
</scanTargetPattern>
</scanTargetPatterns>
</configuration>
</plugin>
</plugins>
</build>
</project>
6. 建立servlet类
public class HttpServletDemo extends HttpServlet { /**
*
*/
private static final long serialVersionUID = 4230239235404655360L; public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException {
response.setContentType("text/html;charset=GB2312");
PrintWriter printWriter = response.getWriter();
String title = "HelloServlet";
String heading1 = "HelloServlet的doGet方法的输出:";
printWriter = response.getWriter();
printWriter.print("<HTML><HEAD><TITLE>" + title + "</TITLE>");
printWriter.print("</HEAD><BODY>");
printWriter.print(heading1);
printWriter.println("<h1><p>:您好</h1>");
printWriter.print("</body></HTML>");
printWriter.write("");
printWriter.flush();
printWriter.close();
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException {
doGet(request, response);
}
}
7. 配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>TomcatWeb</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>MavenWeb</servlet-name>
<servlet-class>maven.servlet.HttpServletDemo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MavenWeb</servlet-name>
<url-pattern>/servlet/HttpServletDemo</url-pattern>
</servlet-mapping>
</web-app>
8. 配置jetty来运行web项目
配置好后点击右下角的Run按钮来运行
9. 浏览器访问
程序运行后,在浏览器输入:http://localhost:9111,即可看到后台返回的信息