首页 技术 正文
技术 2022年11月16日
0 收藏 694 点赞 4,246 浏览 1855 个字

本文通过一个例子来介绍利用maven来构建一个web项目。开发工具:intellij idea。

一、新建maven项目

Maven(3)-利用intellij idea创建maven web项目

此处选择:Create from archetype。表示从已有的maven模版中创建项目,本次选择maven-archetype-webapp模版,选择该模版后,生成的项目会自带webapp所需要的文件。比较方便。当然也可以不选择这个选项,直接创建maven空白项目,后续手动添加web需要的文件。

Maven(3)-利用intellij idea创建maven web项目

填写本项目的maven坐标

Maven(3)-利用intellij idea创建maven web项目

二、配置maven项目结构

最终生成maven项目

Maven(3)-利用intellij idea创建maven web项目

上面的项目只包含了web-app所需要的目录结构和基本的maven项目元素:pom。标准的maven项目结构如下:

Maven(3)-利用intellij idea创建maven web项目

完善后的项目目录:

Maven(3)-利用intellij idea创建maven web项目

、添加一个Servlet

这个Servlet功能非常简单:输出一句话。

package com.cnblogs.kmpp;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 WebAppServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println( "WebAppServlet Executed" );
out.flush();
out.close();
}
}

上面的Servlet有对J2EE的依赖,需要在pom中增加对j2ee的依赖。

<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-servlet_3.0_spec</artifactId>
<version>1.0</version>
</dependency>

配置servlet

<!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> <servlet>
<servlet-name>WebApp</servlet-name>
<servlet-class>com.cnblogs.kmpp.WebAppServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>WebApp</servlet-name>
<url-pattern>/simple-servlet</url-pattern>
</servlet-mapping>
</web-app>

、运行web app

web项目的运行可以通过tomcat来实现,但是既然已经使用了maven可以通过一个maven插件来实现这个功能:jetty(Jetty 是一个开源的servlet容器,它为基于Java的web容器,例如JSP和servlet提供运行环境)。

运行jetty:将命令行定位到当前项目的目录下面,输入如下命令:

Maven(3)-利用intellij idea创建maven web项目

得如下结果:

Maven(3)-利用intellij idea创建maven web项目

这个时候jetty已经启动。

在浏览器中输入:http://localhost:8080/web-app/。

Maven(3)-利用intellij idea创建maven web项目

这个界面就是web-app默认的index.jsp页面。

Maven(3)-利用intellij idea创建maven web项目

上面显示的是默认输出,上面已经定义了一个简单的servlet且配置到了web.xml中。这个时候也可以运行。在浏览器中输入:http://localhost:8080/web-app/simple-servlet

Maven(3)-利用intellij idea创建maven web项目

上面页面的输出就是之前在WebAppServlet中所实现的功能。

至此,一个maven web项目已经建立完毕,且运行成功。


    					
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,581
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918