1、新建maven project,然后选择webapp的框架
2、写一个Servlet
package com.onyas.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.onyas.user.model.User;
public class HelloServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
req.setAttribute("user", new User("admin","123","管理员"));
req.getRequestDispatcher("/hello.jsp").forward(req, resp);
}
}
3、在src/main/webapp/WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>com.onyas.servlet.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/hello.do</url-pattern>
</servlet-mapping>
</web-app>
4、在src/main/webap下新建hello.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Hello---->${user.nickname }
</body>
</html>
5、修改因为有引入User类,所以要添加对其它模块的依赖, 如果这时候报找不到自己写的jar包时,是因为没有把自己写的模块安装到本地仓库中,需要在自己的模块pom.xml中执行clean install.
<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>
<parent>
<groupId>com.onyas.user</groupId>
<artifactId>user-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../user-parent/pom.xml</relativePath>
</parent>
<artifactId>user-web</artifactId>
<packaging>war</packaging>
<name>user-web Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</schttp://localhost:8080/user-web/hello.doope>
</dependency>
<!-- 对servlet的依赖 -->
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5.20110712</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>user-services</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
<finalName>user-web</finalName>
</build>
</project>
6、在pom.xml上右键run as -->maven build..-->输入clean package..会在target包下生成war包,把这个war包拷贝到tomcat下webapp目录下,启动tomcat就可以了
在 浏览器中输入http://localhost:8080/user-web/hello.do,即可看到结果。
7、这样做每次修改后都要执行clean package,然后拷贝war包,然后运行tomcat,很麻烦,所以现在有一个插件,搜索copy maven plugin就可以。
更好的一种做好是用jetty发布,首先在父pom.xml中配置对jetty插件:
<!-- 配置jetty的插件 -->
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<webApp>
<contextPath>/hello</contextPath>
</webApp>
<!-- 配置监听端口 -->
<connectors>
<connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
<port>8082</port>
<maxIdleTime>60000</maxIdleTime>
</connector>
</connectors>
</configuration>
</plugin>
然后在web项目中加入jetty插件:
<build>
<finalName>user-web</finalName>
<plugins>
<!-- 添加jetty插件 -->
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
在web项目的pom.xml中右键 run as -->maven builder..->clean compile jetty:run,就会看到控制台启动jetty,启动完成后,即可在浏览器中试验