參考:https://guides.gradle.org/building-java-web-applicationshtml
1.運行和調試java
IDEA建立gradle項目,項目結構以下web
各個文件:express
build.gradleapache
// https://guides.gradle.org/building-java-web-applications plugins { id 'java' id 'war' id 'org.akhikhl.gretty' version '1.4.2' } group 'ServletDemo' version '1.0-SNAPSHOT' sourceCompatibility = 1.8 repositories { mavenCentral() } dependencies { // https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api providedCompile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0' testCompile group: 'junit', name: 'junit', version: '4.12' }
HelloServlet.javaapi
package servlets; 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; @WebServlet(name = "HelloServlet", urlPatterns = {"hello"}, loadOnStartup = 1) public class HelloServlet extends HttpServlet { int i = 0; // Servlet "persistence" protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { response.getWriter().print("Hello, World! " + i++); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name = request.getParameter("name"); if (name == null) name = "World"; request.setAttribute("user", name); request.getRequestDispatcher("response.jsp").forward(request, response); } } ///:~
index.htmltomcat
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Servlet Demo</title> </head> <body> <p>Say <a href="hello">Hello</a></p> <form method="post" action="hello"> <h2>Name:</h2> <input type="text" id="say-hello-text-input" name="name" /> <input type="submit" id="say-hello-button" value="Say Hello" /> </form> </body> </html>
response.jspjava-web
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Hello Page</title> </head> <body> <h2>Hello, ${user}!</h2> </body> </html>
而後點擊左側的Gradle面板,選擇任務gretty.appRun就能夠啓動app,經過localhost:8080訪問app
若是要 調試,那麼在Gradle面板中選擇任務gretty.appRunDebug,啓動該任務後,選擇Run->Edit Configurations 而後添加一個Remote Run/Debug,以下圖,名爲DebugServletDemo,根據gretty官方文檔,默認debug port是5005,因此這裏填好host和portless
而後在Gradle面板中啓動任務gretty.appRunDebug,以下圖Run選項卡中所示,在5005端口等待Debugger的鏈接
接下來再啓動Run->Debug->DebugServletDemo,以下圖Debug選項卡中所示,Debugger已鏈接上5005端口
此時再次查看Run選項卡,會發現其中內容以下,點擊該http連接開始調試
2.部署到tomcat
2.1. 打包war只須要Gradle面板中選擇build.build任務便可,而後把war包放到tomcat安裝目錄的webapps/目錄下,這裏個人war包名爲ServletDemo-1.0-SNAPSHOT.war
2.2. 修改tomcat安裝目錄下的conf/tomcat-users.xml,以下內容,從而能夠使用tomcat的管理權限
<?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <tomcat-users xmlns="http://tomcat.apache.org/xml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd" version="1.0"> <!-- NOTE: By default, no user is included in the "manager-gui" role required to operate the "/manager/html" web application. If you wish to use this app, you must define such a user - the username and password are arbitrary. It is strongly recommended that you do NOT use one of the users in the commented out section below since they are intended for use with the examples web application. --> <!-- NOTE: The sample user and role entries below are intended for use with the examples web application. They are wrapped in a comment and thus are ignored when reading this file. If you wish to configure these users for use with the examples web application, do not forget to remove the <!.. ..> that surrounds them. You will also need to set the passwords to something appropriate. --> <!-- <role rolename="tomcat"/> <role rolename="role1"/> <user username="tomcat" password="<must-be-changed>" roles="tomcat"/> <user username="both" password="<must-be-changed>" roles="tomcat,role1"/> <user username="role1" password="<must-be-changed>" roles="role1"/> --> <role rolename="manager-gui"/> <user username="admin" password="1qaz2WSX" roles="manager-gui"/> </tomcat-users>
2.3. 啓動tomcat,訪問localhost:8080,以下:
2.4. 點擊"Manager App"按鈕,進入以下頁面,而後start ServletDemo-1.0-SNAPSHOT便可訪問
2.4.1. 這裏我發現war包的app不能啓動,在tomcat的logs/manager.2018.xx.xx.log中發現報錯Invalid <url-pattern> [hello] in servlet mapping,去HelloServlet.java中把urlPatterns="hello"改成urlPatterns="/hello"便可}改成