why tomcat does not require restart when jsp is changed

北城余情 提交于 2020-01-01 08:41:47

问题


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.

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.

I have knowledge of cases when a JSP page gets compiled like on first time access after server restart etc.


回答1:


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.

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.



回答2:


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.

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.html for details.




回答3:


Not just JSP's some containers also support reloading of servlet class if it is modified.

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.

Coming to your question,

Why Tomcat does not require restart?

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.

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.



来源:https://stackoverflow.com/questions/9681679/why-tomcat-does-not-require-restart-when-jsp-is-changed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!