一、Create New Project 【File→New→Project】→New Projecthtml
二、maven→group、artifactId、version便可java
一、统一源代码编码方式git
在pom中添加web
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties>
二、统一源代码与编译输出JDK版本apache
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.3</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build>
maven中心仓库地址:http://search.maven.org/api
三、打包时忽略测试tomcat
<!-- Test --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.18.1</version> <configuration> <skipTests>true</skipTests> </configuration> </plugin>
基本项目建立完毕app
a.在main下添加webapp目录框架
b.在webapp下添加WEB-INF目录webapp
c.在WEB-INF下添加web.xml文件
此时IDEA会出现
自动识别项目为web【即Servlet框架】项目,点击Configure,在点击ok便可
在web.xml中添加以下,使用servlet 3.0
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> </web-app>
一、打包设置【pom中】
<packaging>war</packaging>
二、添加java web 所需依赖Servlet 、JSP、JSTL等
<dependencies> <!-- Servlet --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <!-- JSP --> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> <scope>provided</scope> </dependency> <!-- JSTL --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> <scope>runtime</scope> </dependency> </dependencies>
三、增长tomcat插件
<!-- Tomcat --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <path>/${project.artifactId}</path> </configuration> </plugin>
至此java web搭建完毕
需求:写一个HelloServlet,接收Get类型的/hello请求,转发到/WEB-INF/jsp/hello.jsp页面,在hello.jsp页面上显示当前时间。
package com.lhx.chapter1;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author lihongxu6
* @since 2017/10/9 14:07
*/
@WebServlet("/hello")
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String currentTime = dateFormat.format(new Date());
req.setAttribute("currentTime", currentTime);
req.getRequestDispatcher("/WEB-INF/jsp/hello.jsp").forward(req, resp);
}
}
说明:使用WebServlet注解并配置请求路径,对外发布Servlet服务。
Servlet 3.0增长WebServlet配置后,在web.xml不用配置便可
在WEB-INF下创建jsp文件夹,下创建hello.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h1>Hello!</h1> <h2>当前时间是:${currentTime}</h2> </body> </html>
至此以编写完成
http://www.cnblogs.com/bjlhx/p/7059671.html
在pom中增长以下
<!-- Tomcat --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <path>/${project.artifactId}</path> </configuration> </plugin>
打开IDEA的maven面板,双击tomcat7:run命令便可。
访问此地址Running war on http://localhost:8080/lhx-chapter1/hello便可
以Debug方式访问
添加一个maven方式的Configuration配置
一、打开 Edit Configuration配置,找到maven
二、名称输入tomcat,Command Line输入:tomcat7:run便可
在根目录下增长.gitignore文件
# Maven # target/ # IDEA # .idea/ *.iml # Eclipse # .settings/ .metadata/ .classpath .project Servers/
在VCS中”Import into Version Control/Create Git Repository... “,点击ok,即建立本地仓库完成。
选中项目,右键→git→add将添加至本地仓库
选中项目,右键→git→Commit Directory...将添加至本地仓库
git add负责将文件内容存入blob对象,并更新index,git commit负责根据index生成tree对象,而后生成commit对象指向这个tree对象。
能够使用开源的Github或者开源中国http://git.oschina.net,创建项目
本地使用
git remote add origin < Git仓库地址>
git push -u origin master