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); } }
<?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>
<%@ 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>
<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>
在 浏览器中输入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>
<build> <finalName>user-web</finalName> <plugins> <!-- 添加jetty插件 --> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> </plugin> </plugins> </build>