over 7 years ago
不用錢加減用的平民版啊~~!!
使用開啟新專案
新專案內容長這樣
先編輯
group 'TomcatTest'
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'idea'
apply plugin: 'com.bmuschko.tomcat'
compileJava.options.encoding = 'UTF-8'
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.bmuschko:gradle-tomcat-plugin:2.2.2'
}
}
repositories {
mavenCentral()
}
dependencies {
def tomcatVersion = '7.0.59'
tomcat "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}",
"org.apache.tomcat.embed:tomcat-embed-logging-juli:${tomcatVersion}",
"org.apache.tomcat.embed:tomcat-embed-jasper:${tomcatVersion}"
testCompile group: 'junit', name: 'junit', version: '4.11'
providedCompile 'javax.servlet:javax.servlet-api:3.0.1'
}
tomcat {
httpPort = 8080
httpsPort = 8443
enableSSL = true
contextPath = 'sample-app'
}
建立src以下需要的資料夾,建立好後如下
開啟Gradle工具
左上角紅色框按鈕Refresh all Gradle
重整後就可以執行tomcatRun
執行結果
藍色網址點下去就可以看得你的網頁啦
有一點要注意的就是似乎無法使用Debug模式的中斷點功能
測試用的檔案
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>test</display-name>
</web-app>
package com.sam;
import javax.servlet.ServletConfig;
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.io.PrintWriter;
@WebServlet(urlPatterns = "/ReadInit", loadOnStartup = 1)
public class test extends HttpServlet {
public void init(ServletConfig config) throws ServletException {
System.out.println("正常啟動");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("Hello World!");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
測試用頁面
</body>
</html>