MAVEN创建JAVA的Web工程

maven命令:http://blog.csdn.net/edward0830ly/article/details/8748986

1、创建MAVEN的Web工程

mvn archetype:generate -DgroupId=com.taobao.app -DartifactId=my-web-app -DarchetypeArtifactId=maven-archetype-webapp -DinteractivMode=false

目录如下:

@~/study/maven/my-web-app $ tree
.
├── pom.xml
├── src
│   └── main
│       ├── resources
│       └── webapp
│           ├── WEB-INF
│           │   └── web.xml
│           └── index.jsp
└── target

10 directories, 4 files

2、创建JAVA文件

在main目录创建src/main/java/classes/com/taobao/HelloWorld.java

  package com.taobao;                                                                                                                                         
  import java.io.*;
  import javax.servlet.*;
  import javax.servlet.http.*;
  public class HelloWorld extends HttpServlet{
  public void doGet(HttpServletRequest req,HttpServletResponse res)
  throws ServletException,IOException{
      res.setContentType("text/html");
      PrintWriter out=res.getWriter();
      out.println("");
      out.println("");
      out.println("");
      out.println("Hello,World!");
      out.println("");
  }
  }//endhere!

3、载入server-api.jar

修改pom.xml,添加依赖

    <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <type>jar</type>
            <scope>compile</scope>
    </dependency>

4、修改web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
 <web-app>
  <servlet>
  <servlet-name>hello</servlet-name>
  <servlet-class>com.taobao.HelloWorld</servlet-class>
  </servlet>
  <servlet-mapping>
  <servlet-name>hello</servlet-name>
  <url-pattern>/HelloWorld</url-pattern>
  </servlet-mapping>
  <display-name>Archetype Created Web Application</display-name>
</web-app>

5、打包

mvn clean package

6、将成功的war包拷贝到tomcat的webapp目录

进入打包的目录,target/my-web-app,查看到如下目录结构:

.
├── META-INF
├── WEB-INF
│   ├── classes
│   │   └── com
│   │       └── taobao
│   │           └── HelloWorld.class
│   ├── lib
│   │   └── servlet-api-2.5.jar
│   └── web.xml
└── index.jsp

7、查看数据

http://localhost:8080/my-web-app/HelloWorld

posted @ 2014-03-20 14:42  李秋  阅读(415)  评论(0编辑  收藏  举报