专业的编程技术博客社区

网站首页 > 博客文章 正文

Maven集成Jetty插件创建Web项目(maven jetty run)

baijin 2024-09-21 13:04:36 博客文章 3 ℃ 0 评论

一、简介

在这篇文章中,笔者将介绍使用Maven骨架插件创建简单的Web工程(Jetty插件)。

二、系统需求

  • Java SE Development Kit 7

  • Maven 3.3.9

三、创建Web项目步骤

这部分将介绍如何创建一个简单的Web工程

1. 创建空白的文件夹

在你熟悉的操作系统中创建一个空白的文件夹,例如:创建F:\web目录

2. 使用骨架创建工程

mvn archetype:generate -DgroupId=com.dhc.simpleweb -DartifactId=simple-webapp -Dpackage=com.dhc.simpleweb -DarchetypeArtifactId=maven-archetype-webapp -Dversion=1.0-SNAPSHOT -DinteractiveMode=false
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:3.0.1:generate (default-cli) > generate-sourc
[INFO]
[INFO] <<< maven-archetype-plugin:3.0.1:generate (default-cli) < generate-sourc
[INFO]
[INFO] --- maven-archetype-plugin:3.0.1:generate (default-cli) @ standalone-pom
[INFO] Generating project in Batch mode
[INFO] ------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype
[INFO] ------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.dhc.simpleweb
[INFO] Parameter: packageName, Value: com.dhc.simpleweb
[INFO] Parameter: package, Value: com.dhc.simpleweb
[INFO] Parameter: artifactId, Value: simple-webapp
[INFO] Parameter: basedir, Value: F:\web
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: F:\web\simple-webapp
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

使用如上图1的命令运行即可创建web工程,若出现如上图2则说明创建成功(前提:搭建好Maven环境,请查看笔者前一篇文章)。成功后可查看F:\web\simple-webapp目录下存在src目录和pom.xml文件。

3. 修改pom.xml文件

经过第2步创建好了web工程,本小节将讲解3个小配置,并且配置到pom.xml中

1.Maven编译插件

2.Maven Jetty插件

3.Servlet依赖和Junit4依赖

<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>com.dhc.simpleweb</groupId>
 <artifactId>simple-webapp</artifactId>
 <version>1.0-SNAPSHOT</version>
 <packaging>war</packaging>
 <name>simple-webapp Maven Webapp</name>
 <url>http://maven.apache.org</url>
 <dependencies>
<dependency>
 <groupId>javax.servlet</groupId>
 <artifactId>servlet-api</artifactId>
 <version>2.3</version>
 <scope>provided</scope>
 </dependency>
 <dependency>
 <groupId>junit</groupId>
 <artifactId>junit</artifactId>
 <version>4.12</version>
 <scope>test</scope>
 </dependency>
 </dependencies>
 <build>
 <finalName>simple-webapp</finalName>
<plugins>
 <plugin>
 <artifactId>maven-compiler-plugin</artifactId>
 <configuration>
 <source>1.7</source>
 <target>1.7</target>
 </configuration>
 </plugin>
 <plugin>
 <groupId>org.mortbay.jetty</groupId>
 <artifactId>maven-jetty-plugin</artifactId>
 <version>6.1.26</version>
 <configuration>
 <connectors>
 <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
 <port>80</port>
 </connector>
 </connectors>
 </configuration>
 </plugin>
 </plugins>
 </build>
</project>

4. 运行与测试

通过DOS命令切换到F:\web\simple-webapp目录下,运行如下命令:

mvn jetty:run

启动成功后,打开浏览器输入如下URL:

http://localhost/simple-webapp/index.jsp

四、创建Servlet步骤

1. 创建包名

在windows中发出如下命令,创建包名,并且切换到这个目录中

md F:\web\simple-webapp\src\main\java\com\dhc\web
cd src/main/java/com/dhc/web

2. 创建Servlet

创建SimpleServlet.java文件如下:

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

3. 配置Servlet

将Servlet配置到F:\web\simple-webapp\src\main\webapp\WEB-INF\web.xml中

<web-app>
 <display-name>Archetype Created Web Application</display-name>
 <servlet>
 <servlet-name>simple</servlet-name>
 <servlet-class>
 com.dhc.web.SimpleServlet
 </servlet-class>
 </servlet>
 <servlet-mapping>
 <servlet-name>simple</servlet-name>
 <url-pattern>/simple</url-pattern>
 </servlet-mapping>
</web-app>

4. 安装Servlet

安装Servlet执行如下命令(清理并且安装):

cd F:\web\simple-webapp
mvn clean install

5. 运行与测试

运行jetty执行如下命令:

mvn jetty:run

启动成功后,打开浏览器输入如下URL:

http://localhost/simple-webapp/simple

执行成功后在页面打印出“SimpleServlet Executed”。

五、总结

以上就是Maven集成Jetty插件纯手工创建Web项目的步骤,当然可以选择你熟悉的IDE进行开发。

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表