1. 程式人生 > 其它 >IntelliJ IDEA 使用sftp快速同步web專案到伺服器

IntelliJ IDEA 使用sftp快速同步web專案到伺服器

目錄

前言

在Windows系統上寫完專案編譯構建後,釋出到linux服務端。

1、開發工具IntelliJ IDEA

2、SFTP傳輸(IntelliJ IDEA已經整合sftp)

實踐

下面寫一個HelloWorld程式來實踐

一、專案結構與程式碼

1、專案目錄結構

2、HelloWorld.java原始碼

// 匯入必需的 java 庫
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;

// 擴充套件 HttpServlet 類
public class HelloWorld extends HttpServlet {

    private String message;

    public void init() throws ServletException
    {
        // 執行必需的初始化
        message = "Hello World";
    }

    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
            throws ServletException, IOException
    {
        // 設定響應內容型別
        response.setContentType("text/html");

        // 實際的邏輯是在這裡
        PrintWriter out = response.getWriter();
        out.println("<h1>" + message + "</h1>");
    }

    public void destroy()
    {
        // 什麼也不做
    }
}

3、index.jsp原始碼

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>測試</title>
  </head>
  <body>
    我是HelloWorld主頁
  </body>
</html>

4、web.xml原始碼

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <servlet>
        <servlet-name>HelloWorld</servlet-name>
        <servlet-class>com.yfw.HelloWorld</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>HelloWorld</servlet-name>
        <url-pattern>/HelloWorld</url-pattern>
    </servlet-mapping>
</web-app>

二、專案配置

配置java編譯後的class檔案輸出路徑

設定熱部署

在 Java Web 開發中, 一般更新了 Java 檔案後要手動重啟 Tomcat 伺服器, 配置的話, 不論是更新 class 類,css檔案,html檔案,js檔案,還是更新 Spring 配置檔案都能做到立馬生效,大大提高開發效率。熱部署省去了重啟tomcat的時間。

配置sftp

1、開啟Deployment配置

2、配置sftp,並測試連線是否成功。

3、要同步的目錄路徑設定
下面的配置是:
把本地的C:\Users\Administrator\Desktop\Intellij_Project\out\artifacts\Intellij_Project_war_exploded 目錄和伺服器的
/opt/tomcat/webapps/sshtest目錄同步。

編譯並同步到伺服器

1、點選錘子圖示,編譯專案

2、同步編譯後的檔案到伺服器

選擇sshtest進行同步

訪問你的伺服器(可以看到專案已經同步到了伺服器)