What is the difference between <%! %>
and <% %>
in JSP?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
<%! %>
are JSP Declaration tags while <% %>
are JSP Scriptlet tags.
Any code you put in scriptlets gets put in the JSPs _jspService()
method when it's compiled (the analogue of a Servlet's doGet
, doPost
,... methods). This is what you'd usually write your Java code in.
But what if you want to declare new methods in your JSP class or declare instance variables? That's when you'd use the declaration tags. Any thing you put in there gets put into the JSP, outside of the _jspService()
method.