jsp注释中的Java代码。它是如何运作的?
代码结构如下:
jsp code
<%
java code
%>
jsp code
那么,java 代码在 jsp 中是如何工作的呢?我可以在不重建的情况下实施 Chenges 吗?
Theres the code structure:
jsp code
<%
java code
%>
jsp code
So, how java-code works in jsp? Can i implement chenges without rebuild?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
答案取决于“重建”的含义。显然,要在 JVM 上运行某些东西,就必须对其进行编译。然而,JSP 容器会自动为您执行此操作:它发现文件已更改,并重新编译它。
实际上,它所做的不仅仅是重新编译:它首先将 JSP 转换为实现
Servlet
API 的纯 Java 代码,然后编译该 Java 代码。 JSP 中的静态文本(例如:被转换为
println()
调用。JSTL 和其他标记引用被转换为实例化和调用的 Java 代码Scriptlet 是逐字插入的,但它们中的大多数会让您检查生成的 servlet,例如,将生成的代码存储在
work< 下。 /代码> 目录。
The answer depends on what you mean by "rebuild." Clearly, for something to run on the JVM, it has to be compiled. However, the JSP container does that for you automatically: it sees that the file has changed, and recompiles it.
Actually, it does a little more than recompile: it first translates the JSP into pure Java code that implements the
Servlet
API, then it compiles that Java code. The static text in the JSP (eg:<html>
is turned intoprintln()
calls. The JSTL and other tag references are transformed into Java code that instantiates and invokes a tag handler. Scriptlets are inserted verbatim.You don't say what JSP container you're using, but most of them will let you examine the generated servlet. Tomcat, for example, stores the generated code under the
work
directory.http://www.exampledepot.com/egs/javax.servlet.jsp /code.html
只需在浏览器上重新加载页面即可应用更改,因为它是 JSP,这意味着每次加载页面时都会对其进行编译。
如果您使用 Tomcat,您可以在文件夹中看到 JSP 生成的 java 代码: TOMCAT_HOME/work/localhost
在 JSP 代码上使用 java 代码是不明智的,因为如果很难识别语法错误,并且如果有一个,则整个页面根本无法加载。与 PHP 不同,PHP 会加载直到出现语法错误。
http://www.exampledepot.com/egs/javax.servlet.jsp/code.html
Changes will be applied just by reloading the page on browser, since it's JSP, which means that the page will be compiled everytime it's loaded.
If you are using Tomcat you can see generated java codes of the JSP in folder: TOMCAT_HOME/work/localhost
It's not wise to use java code on a JSP code because if there's hard to identify syntax error and if there is one, the entire page won't be able to be loaded at all. Unlike PHP which will load until the point where there is syntax error.
<%
和%>
不是注释。它们标志着 scriptlet(即 java 代码)的开始。如果您使用 Tomcat,对 jsp 文件的任何更改都会被记录下来并重新构建,而无需执行任何其他操作。<%
and%>
are not comments. They signal the beginning of a scriptlet, i.e. java code. If you're using Tomcat, any changes to the jsp files are noted and rebuilt without you having to do anything else.