JSP page script fragment, statement, jsp expression

First, we must understand the operating principle jsp. JSP is the essence of a Servlet, Tomcat server will first be translated into JSP .java files before running, and then compile the .java text
as .class files, and when we visit jsp, after the processing of the request is the translation class .

Servlet
Service,也就是脚本片段
声明,全局变量与方法在里面
  1. <%%> Is called 脚本片段, which will write the content translation method in the Service Servlet, it is clear we can define local variables in the Service or method call other methods.
  2. <%!%> Called 声明, which writes the contents of the future will have a direct translation in the Servlet class, because we can define the class 方法and 属性as well 全局变量, so we can sound in the <%!%>
    Ming methods, properties, global variables.
  3. <% =%> Referred to jsp表达式for already declared variable or expression above is output to the page.

to sum up

  • Because JSP variables in the built-in object code written in the Service of the (script fragment), and the method used to define global variables (declared) related code are written in Servlet inside, that is, with the same level of Service and so, in <%!%> can not call the built-in objects, such as application, response, request and so on.
  • If you want the application object in <%!%> Can be used to indirectly obtain applicaiton ServletContext attributes. Incidentally, mention here the relationship and differences with the applicaiton of ServletContext.
ServletContext applicaiton = getSerVletContext();

Relationship and differences with the applicaiton of ServletContext.

  1. servletcontext mainly used in a servlet, of course, also be used in the jsp;
  2. application only in jsp page;
  3. application and servletcontext global container are stored information;
  4. application is in packaging applications servletcontext jsp, so they are actually the same object is represented, with application attribute stored servletcontext can be read, and vice versa;
  5. Jsp can be used directly in the application, the object may be obtained by servletcontext GetServletContext () method, and the application of this object to use exactly the same;
  6. 不懂之处: You can not use applicaiton in <%!%>, But you can use the ServletContext.
Published 22 original articles · won praise 0 · Views 148

Guess you like

Origin blog.csdn.net/weixin_42649617/article/details/104864552