一、IDEA创建项目

1、基本项目创建

1.1、基本步骤

1、Create New Project 【File→New→Project】→New Project

2、maven→group、artifactId、version即可

1.2、调整maven配置

1、统一源代码编码方式

在pom中添加

  1. <properties>
  2. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  3. </properties>

2、统一源代码与编译输出JDK版本

  1. <build>
  2. <plugins>
  3. <plugin>
  4. <groupId>org.apache.maven.plugins</groupId>
  5. <artifactId>maven-compiler-plugin</artifactId>
  6. <version>3.3</version>
  7. <configuration>
  8. <source>1.8</source>
  9. <target>1.8</target>
  10. </configuration>
  11. </plugin>
  12. </plugins>
  13. </build>

maven中心仓库地址:http://search.maven.org/

3、打包时忽略测试

  1. <!-- Test -->
  2. <plugin>
  3. <groupId>org.apache.maven.plugins</groupId>
  4. <artifactId>maven-surefire-plugin</artifactId>
  5. <version>2.18.1</version>
  6. <configuration>
  7. <skipTests>true</skipTests>
  8. </configuration>
  9. </plugin>

基本项目创建完毕

2、转为java web项目

2.1、转化步骤

a.在main下添加webapp目录

b.在webapp下添加WEB-INF目录

c.在WEB-INF下添加web.xml文件

此时IDEA会出现

  

自动识别项目为web【即Servlet框架】项目,点击Configure,在点击ok即可

在web.xml中添加如下,使用servlet 3.0

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns="http://java.sun.com/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  5. version="3.0">
  6. </web-app>

2.2、添加javaweb的maven依赖

1、打包设置【pom中】

  1. <packaging>war</packaging>

2、添加java web 所需依赖Servlet 、JSP、JSTL等

  1. <dependencies>
  2. <!-- Servlet -->
  3. <dependency>
  4. <groupId>javax.servlet</groupId>
  5. <artifactId>javax.servlet-api</artifactId>
  6. <version>3.1.0</version>
  7. <scope>provided</scope>
  8. </dependency>
  9. <!-- JSP -->
  10. <dependency>
  11. <groupId>javax.servlet.jsp</groupId>
  12. <artifactId>jsp-api</artifactId>
  13. <version>2.2</version>
  14. <scope>provided</scope>
  15. </dependency>
  16. <!-- JSTL -->
  17. <dependency>
  18. <groupId>javax.servlet</groupId>
  19. <artifactId>jstl</artifactId>
  20. <version>1.2</version>
  21. <scope>runtime</scope>
  22. </dependency>
  23. </dependencies>

3、增加tomcat插件

  1. <!-- Tomcat -->
  2. <plugin>
  3. <groupId>org.apache.tomcat.maven</groupId>
  4. <artifactId>tomcat7-maven-plugin</artifactId>
  5. <version>2.2</version>
  6. <configuration>
  7. <path>/${project.artifactId}</path>
  8. </configuration>
  9. </plugin>

至此java web搭建完毕

3、简单web应用【原生】

需求:写一个HelloServlet,接收Get类型的/hello请求,转发到/WEB-INF/jsp/hello.jsp页面,在hello.jsp页面上显示当前时间。

3.1、编写Servlet类

  1. package com.lhx.chapter1;
  2.  
  3. import javax.servlet.ServletException;
  4. import javax.servlet.annotation.WebServlet;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import java.io.IOException;
  9. import java.text.DateFormat;
  10. import java.text.SimpleDateFormat;
  11. import java.util.Date;
  12.  
  13. /**
  14. * @author lihongxu6
  15. * @since 2017/10/9 14:07
  16. */
  17. @WebServlet("/hello")
  18. public class HelloServlet extends HttpServlet {
  19. @Override
  20. protected void doGet(HttpServletRequest req, HttpServletResponse resp)
  21. throws ServletException, IOException {
  22. DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  23. String currentTime = dateFormat.format(new Date());
  24. req.setAttribute("currentTime", currentTime);
  25. req.getRequestDispatcher("/WEB-INF/jsp/hello.jsp").forward(req, resp);
  26. }
  27. }

说明:使用WebServlet注解并配置请求路径,对外发布Servlet服务。

  Servlet 3.0增加WebServlet配置后,在web.xml不用配置即可

3.2、编写jsp页面

在WEB-INF下建立jsp文件夹,下建立hello.jsp

  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  2. <html>
  3. <head>
  4. <title>Title</title>
  5. </head>
  6. <body>
  7. <h1>Hello!</h1>
  8. <h2>当前时间是:${currentTime}</h2>
  9. </body>
  10. </html>

至此以编写完成

4、web启动

方式一、使用集成的tomcat

http://www.cnblogs.com/bjlhx/p/7059671.html

方式二、使用tomcat的maven插件

在pom中增加如下

  1. <!-- Tomcat -->
  2. <plugin>
  3. <groupId>org.apache.tomcat.maven</groupId>
  4. <artifactId>tomcat7-maven-plugin</artifactId>
  5. <version>2.2</version>
  6. <configuration>
  7. <path>/${project.artifactId}</path>
  8. </configuration>
  9. </plugin>

打开IDEA的maven面板,双击tomcat7:run命令即可。

  

访问此地址Running war on http://localhost:8080/lhx-chapter1/hello即可

以Debug方式访问

添加一个maven方式的Configuration配置

1、打开 Edit Configuration配置,找到maven

2、名称输入tomcat,Command Line输入:tomcat7:run即可

5、git的使用

5.1、编写.gitignore文件

在根目录下增加.gitignore文件

  1. # Maven #
  2. target/
  3. # IDEA #
  4. .idea/
  5. *.iml
  6. # Eclipse #
  7. .settings/
  8. .metadata/
  9. .classpath
  10. .project
  11. Servers/

5.2、提交本地git

  在VCS中”Import into Version Control/Create Git Repository... “,点击ok,即创建本地仓库完成。

  选中项目,右键→git→add将添加至本地仓库

  选中项目,右键→git→Commit Directory...将添加至本地仓库

    git add负责将文件内容存入blob对象,并更新index,git commit负责根据index生成tree对象,然后生成commit对象指向这个tree对象。

5.3、推送至远程git

可以使用开源的Github或者开源中国http://git.oschina.net,建立项目

本地使用

  1. git remote add origin < Git仓库地址>
  2. git push -u origin master

001-web基本程序搭建的更多相关文章

  1. 简单java web应用程序搭建与部署

    1. 准备工作 工具:tomcat.editplus.jdk.windows操作系统 操作:在windows操作系统上安装jdk.tomcat.editplus,配置JAVA_HOME,Path,CL ...

  2. net core体系-web应用程序-4asp.net core2.0 项目实战(任务管理系统)-2项目搭建

    系统要求 首先建议采用 Windows 10 专业版/企业版/教育版,且必须是64位操作系统,原因是docker装起来比较方便,Win7装起来比较麻烦,且不确定是否有其他问题(自己没有实践过) 其次W ...

  3. 基于EasyUI的Web应用程序及过去一年的总结

    前言 在这家公司服务了一年时间,一个多月之前已经提交了离职申请,好在领导都已经批准了,过几天就办理手续了,在此感谢领导的栽培与挽留,感谢各位同事在工作中的给我的帮助,感谢师傅(在我心中当他是我师傅,跟 ...

  4. ASP.NET MVC3入门教程之第一个WEB应用程序

    本文转载自:http://www.youarebug.com/forum.php?mod=viewthread&tid=91&extra=page%3D1 上一节,我们已经搭建好了AS ...

  5. Node.js高级编程读书笔记 - 4 构建Web应用程序

    Outline 5 构建Web应用程序 5.1 构建和使用HTTP中间件 5.2 用Express.js创建Web应用程序 5.3 使用Socket.IO创建通用的实时Web应用程序 5 构建Web应 ...

  6. CentOS下Web服务器环境搭建LNMP一键安装包

    CentOS下Web服务器环境搭建LNMP一键安装包 时间:2014-09-04 00:50来源:osyunwei.com 作者:osyunwei.com 举报 点击:3797次 最新版本:lnmp- ...

  7. 一步一步学习SignalR进行实时通信_9_托管在非Web应用程序

    原文:一步一步学习SignalR进行实时通信_9_托管在非Web应用程序 一步一步学习SignalR进行实时通信\_9_托管在非Web应用程序 一步一步学习SignalR进行实时通信_9_托管在非We ...

  8. office web apps 部署-搭建office web apps服务器

    二.搭建office web apps服务器 相关文件可以去焰尾迭分享的百度网盘下载,下载地址:http://pan.baidu.com/s/1o6tCo8y#path=%252Foffice%252 ...

  9. (译)Web是如何工作的(2):客户端-服务器模型,以及Web应用程序的结构

    原文地址:https://medium.freecodecamp.org/how-the-web-works-part-ii-client-server-model-the-structure-of- ...

  10. 03、NetCore2.0下Web应用之搭建最小框架

    03.NetCore2.0下Web应用之搭建最小框架 这里我们不使用VS2017或者CLI命令的方式创建Asp.Net Core 2.0网页应用程序,而是完全手工的一点点搭建一个Web框架,以便更好的 ...

随机推荐

  1. php常见的类库-文件操作类

    工作中经常用php操作文件,因此把常用文件操作整理出来: class hylaz_file{ /** * Read file * @param string $pathname * @return s ...

  2. 在ubuntu下安装ns2-allinone-2.35.tar.gz

    1.软件下载 首先先下载ns-allinone-2.35.tar.gz (下载路径http://sourceforge.net/projects/nsnam/files/),将其放到你/home/my ...

  3. 设置一个label显示多种颜色,多种字体大小

    UILabel* label = [[UILabel alloc] init]; label.frame = CGRectMake(0, 100, 200, 100); label.textColor ...

  4. centos6.5下redis的安装与配置

    参照官网描述(https://redis.io/download),linux下redis安装步骤如下: $ wget http://download.redis.io/releases/redis- ...

  5. openWRT自学---如何开发新的用户态模块-helloworld

    以http://www.gargoyle-router.com/wiki/doku.php?id=openwrt_coding为参考文档 1.要获得openWRT的sdk环境.只要在Backfire的 ...

  6. 切比雪夫多项式(Chebyshev Polynomials)

    切比雪夫多项式在逼近理论中有重要的应用.这是因为第一类切比雪夫多项式的根(被称为切比雪夫节点)可以用于多项式插值.相应的插值多项式能最大限度地降低龙格现象,并且提供多项式在连续函数的最佳一致逼近. 参 ...

  7. 黑马day11 事务的四大特性

    1.事务的四大特性:一个事务具有的最主要的特性.一个设计良好的数据库能够为我们保证这四大特性. 1.1原子性:原子性是指事务是一个不可切割的工作单位,事务中的操作要么都发生要么都不发生. 1.2一致性 ...

  8. poj 2226(最小覆盖)

    题目链接:http://poj.org/problem?id=2226 思路:将连续的横向水洼看成X集合中的一个点,连续的纵向水洼看成Y集合中的一个点,而将每个水点看成一条边,它连接了所在的X集合中的 ...

  9. OpenCV学习笔记十:opencv_superres模块

    一,简介: 该库用于图像超分辨率重建.即通过硬件或软件的方法提高原有图像的分辨率,通过一系列低分辨率的图像来得到一幅高分辨率的图像.

  10. OpenCV学习笔记四:ImgProc模块

    一,简介 这个模块包含一系列的常用图像处理算法. 二,分析 此模块包含的文件如下图: 其导出算法包括如下: /*********************** Background statistics ...