IDEA community + Gradle + Gretty 调试 servlet 应用 + war包部署到tomcat
参考:https://guides.gradle.org/building-java-web-applications
1.运行和调试
IDEA创建gradle项目,项目结构如下

各个文件:
build.gradle
// 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.java
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.html
<!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.jsp
<%@ 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访问
如果要 调试,那么在Gradle面板中选择任务gretty.appRunDebug,启动该任务后,选择Run->Edit Configurations 然后添加一个Remote Run/Debug,如下图,名为DebugServletDemo,根据gretty官方文档,默认debug port是5005,所以这里填好host和port

然后在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"即可}改为
IDEA community + Gradle + Gretty 调试 servlet 应用 + war包部署到tomcat的更多相关文章
- SpringBoot之打成war包部署到Tomcat
正常情况下SpringBoot项目是以jar包的形式,正常情况下SpringBoot项目是以jar包的形式,并且SpringBoot是内嵌Tomcat服务器,所以每次重新启动都是用的新的Tomcat服 ...
- 将Web项目War包部署到Tomcat服务器
1. 配置Java运行环境 1.1 下载并安装JDK 从官网上下载最新的JDK:http://java.sun.com/javase/downloads/index.jsp ,下载后安装,选择想把JD ...
- 将web项目打成war包部署在tomcat步骤
将web项目打成war包部署在tomcat步骤 1.将自己的项目打成war包. 2.将打包好的war复制到${tomcat.home}/webapps项目下. 3.在${tomcat.hom}/con ...
- IntelliJ IDEA打包WAR并部署运行(mac osx)将Web项目War包部署到Tomcat服务器基本步骤(完整版)
用IntelliJ IDEA做web开发体验很好,但导出war包比eclipse麻烦了不少,以下是解决方案: 打包:1.自动打包:File —> Project Structure —> ...
- 将Web项目War包部署到Tomcat服务器基本步骤
参考来源: http://www.cnblogs.com/pannysp/archive/2012/03/07/2383364.html 1. 常识: 1.1 War包 War包一般是在进行Web ...
- 将Web项目War包部署到Tomcat服务器基本步骤(完整版)
1. 常识: 1.1 War包 War包一般是在进行Web开发时,通常是一个网站Project下的所有源码的集合,里面包含前台HTML/CSS/JS的代码,也包含Java的代码. 当开发人员在自己 ...
- war包部署到tomcat
1.maven web app打包成app.war.打包命令:mvn clean package Dmaven.test.skip=true war 是什么?里面有什么东西?a.web.app所有必 ...
- Spring Boot Jar包转War包 部署到Tomcat下
原文:https://my.oschina.net/sdlvzg/blog/1562998 我们都知道springBoot中已经内置了tomcat,是不需要我们额外的配置tomcat服务器的,但是有时 ...
- Springboot-技术专区-war包部署在Tomcat上并修改默认端口
springboot项目内置Tomcat,直接打成jar包在dos下运行即可,但有时我们需要用war包以非内嵌Tomcat的方式来部署,以下是本人的实际经验 1.首先需要修改pom.xml文件 < ...
随机推荐
- Xamarin Essentials教程剪贴板Clipboard
Xamarin Essentials教程剪贴板Clipboard 现在手机设备操作以触屏为主,不便于文本输入.虽然可以通过复制/粘贴的方式,借助系统剪贴板简化操作,但仍然不够方便.如果通过代码操作 ...
- [C程序设计基础]快速排序
//从大到小排序 ///三个参数 a要排序的 数组, l扫左边的 r扫右边 void quickSort(int a[],int l, int r){ /// 左边要小于 右边才有意义 if (l & ...
- SPOJ COT3.Combat on a tree(博弈论 Trie合并)
题目链接 \(Description\) 给定一棵\(n\)个点的树,每个点是黑色或白色.两个人轮流操作,每次可以选一个白色的点,将它到根节点路径上的所有点染黑.不能操作的人输,求先手是否能赢.如果能 ...
- 生成树的计数 Matrix-Tree(矩阵树)定理
信息学竞赛中,有关生成树的最优化问题如最小生成树等是我们经常遇到的,而对生成树的计数及其相关问题则少有涉及.事实上,生成树的计数是十分有意义的,在许多方面都有着广泛的应用.本文从一道信息学竞赛中出现的 ...
- 上一篇是copy了整理网上的内容,这篇是一个推荐
因为公司用的 轮播组件是 aui 的 ui库 这个轮播有样式单一等等很多的问题.... 包括滑动BUG(自己也踩了一个小坑,高度和宽度问题...需要设定好,滑动提示按钮点 丑陋............ ...
- [c++] opencv加载png
1.cvloadimage载入png文件时,默认的第2个参数是1,即CV_LOAD_IMAGE_COLOR,生成的iplimage对象的channel数是3,而不是4,丢失了第4通道.需要改为cvlo ...
- redis(三)
string string是redis最基本的类型 最大能存储512MB数据 string类型是二进制安全的,即可以为任何数据,比如数字.图片.序列化对象等 命令 设置 设置键值 set key va ...
- 资源从3ds max导入UE4问题
1.先删掉灯光和相机.材质命名为英文,贴图也要英文取名,不能有中文,并且必须是JPG格式.并整理好组:删掉多余的物体,例如线2.坐标归零.并把材质转换为默认材质3.选中一个组,先unground,然后 ...
- Node辅助工具NPM&REPL
Node辅助工具NPM&REPL NPM和REPL是node的包管理器和交互式解析器,可以有效提高开发者效率 NPM npm(Node Package Manager)是node包管理器,完全 ...
- js顺序播放列表中的音乐
今天一个朋友问我js顺序播放音乐列表中的音乐的问题,我仔细一想,我也没有做过啊,无从下手啊,怎么办?然后我就上网查了一下audio标签,又百度了js如何顺序播放音乐,结果就找到了解决的办法. audi ...