Java 为什么tomcat改变jsp时不需要重启
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9681679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
why tomcat does not require restart when jsp is changed
提问by Hardik Mishra
I have been using JSP,Servlet
for quite sometime. I know that whenever we change anything in Servlet
we need to restart Tomcat Server to get the changes. Where as in case of JSP change, tomcat does not require restart.
我已经使用JSP,Servlet
了一段时间。我知道每当我们更改任何内容时,Servlet
我们都需要重新启动 Tomcat 服务器以获取更改。在 JSP 更改的情况下,tomcat 不需要重新启动。
As per my knowledge JSP
page gets converted into Servlet
only when compiled. So, after all its a Servlet
.So, How does it works without Tomcat
restart.
根据我的知识JSP
页面Servlet
仅在编译时转换为。所以,毕竟它是一个Servlet
.So,它如何在不Tomcat
重启的情况下工作。
I have knowledge of cases when a JSP page gets compiled like on first time access after server restart etc.
我了解 JSP 页面在服务器重启后首次访问时被编译的情况等。
采纳答案by Bozho
Because by default tomcat is started in development mode, which means JSP-derived servlets recompiled when a change is detected. It's a good questions how does the JVM load the new class - probably the tomcat classloader is configured to do so.
因为默认情况下 tomcat 是在开发模式下启动的,这意味着在检测到更改时重新编译 JSP 派生的 servlet。这是一个很好的问题,JVM 如何加载新类 - 可能是 tomcat 类加载器被配置为这样做。
A few related notes:
一些相关的注意事项:
- you can turn off the development option for production
- you can have servlets been reloaded as well - you have to start tomcat with a JVM in debug mode.
- 您可以关闭生产的开发选项
- 您也可以重新加载 servlet - 您必须在调试模式下使用 JVM 启动 tomcat 。
回答by JB Nizet
Because when Tomcat is asked to execute a JSP, is compares the modification date of the JSP file with the modification time of the compiled class corresponding to this JSP, and if more recent, it recompiles on the fly before executing it.
因为当 Tomcat 被要求执行一个 JSP 时,它会将 JSP 文件的修改日期与该 JSP 对应的编译类的修改时间进行比较,如果最近,它会在执行之前即时重新编译。
This is BTW an option that should be turned off in production, because it takes time to perform this check.
顺便说一句,这是一个应该在生产中关闭的选项,因为执行此检查需要时间。
See http://tomcat.apache.org/tomcat-7.0-doc/jasper-howto.htmlfor details.
有关详细信息,请参阅http://tomcat.apache.org/tomcat-7.0-doc/jasper-howto.html。
回答by Ramesh PVK
Not just JSP's some containers also support reloading of servlet classif it is modified.
不只是 JSP 的一些容器也支持修改后的 servlet 类的重新加载。
It is upto the container to decide when to load servlets. A servlet can be loaded at runtime on demand
. And coming to JSP, JSP translated to servlet can also be loaded at runtime
.
由容器决定何时加载 servlet。A servlet can be loaded at runtime on demand
. 而来到JSP, JSP translated to servlet can also be loaded at runtime
。
Coming to your question,
来回答你的问题,
Why Tomcat does not require restart?
为什么Tomcat不需要重启?
It is because Tomcat is capable of adding/modifying classpath to Web Application classloader at runtime
. Tomcat will be having their custom Classloader implementation which allows them to add the classpaths at runtime
.
这是因为Tomcat is capable of adding/modifying classpath to Web Application classloader at runtime
. Tomcat 将拥有他们的custom Classloader implementation which allows them to add the classpaths at runtime
.
How does the custom classloader might work?
自定义类加载器如何工作?
One way to get this working is when a Servlet/JSP is modified, a new classloader is created for the Servlet/JSP with Application classloader as parent classloader . And the new classloader will load the modified class again
.
实现此功能的一种方法是在修改 Servlet/JSP 时,a new classloader is created for the Servlet/JSP with Application classloader as parent classloader . And the new classloader will load the modified class again
.