Ubuntu系统下JSP实现热部署的常见方法
Tomcat作为常用的Java Servlet容器,原生支持JSP热部署,需通过配置其核心文件开启相关功能。
sudo apt update && sudo apt install openjdk-11-jdk;再通过sudo apt install tomcat9安装Tomcat 9(或其他版本),安装完成后启动服务sudo systemctl start tomcat9。server.xml文件(路径为/etc/tomcat9/server.xml),找到<Host>标签(通常为name="localhost"的节点),添加autoDeploy="true"和deployOnStartup="true"属性,使Tomcat自动检测并部署webapps目录下的应用。例如:<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" deployOnStartup="true">
context.xml文件(路径为/etc/tomcat9/context.xml),在<Context>标签内添加reloadable="true"属性,Tomcat会监控WEB-INF/classes和WEB-INF/lib目录的变化,自动重新加载应用。例如:<Context reloadable="true">
sudo systemctl restart tomcat9重启服务,使配置生效。此后,修改JSP文件并保存,Tomcat会自动重新编译并加载,无需手动重启。JRebel是一款商业工具,支持Java代码、JSP、配置文件的实时热部署,无需重启Tomcat或应用服务器,大幅提升开发效率。
File -> Settings -> Plugins搜索“JRebel”,点击“Install”安装插件,安装完成后重启IDEA。File -> Project Structure -> Facets,确保JRebel已启用;再进入Run/Debug Configurations,选择Tomcat Server配置,在“Before launch”中添加“Make”和“JRebel”任务,使JRebel在启动前监控项目变化。若项目基于Spring Boot框架,可使用其内置的DevTools工具实现快速热部署,适用于开发环境。
pom.xml中添加以下依赖:<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
Gradle项目则在build.gradle中添加:developmentOnly("org.springframework.boot:spring-boot-devtools")
File -> Settings -> Build, Execution, Deployment -> Compiler,勾选“Build project automatically”;再进入Settings -> Advanced Settings,勾选“Allow auto-make to start even if developed application is currently running”。Ctrl + F9(Windows/Linux)或Cmd + F9(Mac)触发重新编译,Spring Boot DevTools会自动重启应用(仅重启修改的部分),实现热部署效果。reloadable="false"、JRebel禁用)。