Maven学习系列--08一个HelloWorld的web应用

来源:互联网 发布:美国零售销售数据公布 编辑:程序博客网 时间:2024/05/19 15:19

一、准备Web 项目

1.1 创建项目

使用 mvn archetype:generate -DgroupId=com.johnnie.maven -DartifactId=web_hello_world -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false 命令创建一个Web项目,该命令会自动创建一些标准的 web 目录结构。运行结果如下:
note8_generate1
note8_generate2

1.1.1 项目结构图:

note8_init

1.1.2 打开 pom.xml,可以看到它有一个地方和以往直接使用“mvn archetype:generate”创建项目的不同之处:

note8_pom

注:1. 命令解释:  mvn archetype:generate    固定格式-DgroupId                 组织标识(包名)-DartifactId              项目名称-DarchetypeArtifactId     1) 值为 maven-archetype-quickstart:创建一个Java Project;  2) 值为 maven-archetype-webapp:创建一个Web Project-DinteractiveMode         是否使用交互模式2. 此处必须得加上参数 -DarchetypeArtifactId=maven-archetype-webapp,让 Maven 以 Web 项目的模板来创建该项目。若是直接使用 "mvn archetype:generate" 命令,后面的步骤会很头疼,各种bug,我就深受其害。

1.2 查看自动生成的 index.jsp 文件

[源码]
note8_indexJsp

1.3 查看自动生成的 web.xml 文件

note8_web

1.4 配置 Jetty 插件:使用 Maven Jetty 插件在 Maven 中运行 web应用

若是以往,我们需要下载 Jetty 或 Tomcat 或 JBoss,然后复制应用程序 WAR文件至 webapps/ 目录,才能运行。而使用 Maven 就不必如此麻烦,只需在 pom.xml 中配置 Jetty 插件,就可以使用相关命令来启动 Web 应用。

<project>...  <build>    <finalName>web_hello_world</finalName>    <plugins>      <!-- 配置 Maven Jetty 插件 -->      <plugin>        <groupId>org.mortbay.jetty</groupId>        <artifactId>maven-jetty-plugin</artifactId>        <version>6.1.9</version>      </plugin>    </plugins>  </build>...</project>

1.5 启动 Jetty:mvn jetty:run

1.5.1 执行命令 mvn jetty:run

note8_1

1.5.2 打开浏览器,输入 http://localhost:8080/web_hello_world:

note8_index

1.6 编辑 HelloWorldServlet

1.6.1 按下图新建文件夹和文件:

note8_servlet

1.6.2 HelloWorldServlet.java:

package com.johnnie.maven;import java.io.*;import javax.servlet.*;import javax.servlet.http.*;public class SimpleServlet extends HttpServlet {    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        PrintWriter out = response.getWriter();        out.println( "HelloWorldServlet Executed" );        out.flush();        out.close();    }}

1.7 配置 Servlet:修改 web.xml

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>  <display-name>Archetype Created Web Application</display-name>  <!-- 配置 HelloWorldServlet -->  <servlet>    <servlet-name>hello</servlet-name>    <servlet-class>com.johnnie.maven.HelloWorldServlet</servlet-class>  </servlet>  <servlet-mapping>    <servlet-name>hello</servlet-name>    <url-pattern>/hello</url-pattern>  </servlet-mapping></web-app>

二、准备运行

2.1 编译项目:mvn compile

2.2.1 首先配置 pom.xml 添加对 Servlet 的依赖

<!-- 添加 Servlet 依赖 jar --><dependency>  <groupId>org.apache.geronimo.specs</groupId>  <artifactId>geronimo-servlet_2.4_spec</artifactId>  <version>1.1.1</version></dependency>

若不添加该依赖,则会编译报错,如下:
note8_err

2.2.2 执行命令 mvn compile

note8_compile

2.2 运行项目

2.2.1 先执行 mvn clean install 命令

note8_mvn_ins1
note8_mvn_ins2

2.2.2 运行 Jetty:命令 mvn jetty:run

note8_run1
note8_run2

2.2.3 访问 HelloWorldServlet

浏览器地址栏中输入 http://localhost:8080/web_hello_world/hello, 结果如下:
note8_hello

三、使用外部服务器运行

刚刚是使用嵌入式的 Jetty (即:Maven 的 Jetty 插件)充当容器运行 Web 项目的。那么,既然我们生成了一个 war 文件,是否可以使用外部的服务器来运行该项目呢?现在我们来试一下

3.1 打开 Eclipse 将 web_hello_world.war 导入到 workspace 中

note8_eclipse

3.2 部署到 JBoss 服务器上,运行

note8_jboss
note8_jboss_hw
note8_jboss_h

1 0
原创粉丝点击