日期:2014-05-16  浏览次数:20369 次

Servlets & JSP Series 7 - Being a JSP

Servlets & JSP Series 7 - Being a JSP

?

  • A JSP becomes a servlet: a sevlet that you do not create, the container looks at your JSP, translates it into Java source code, and compiles it into a full-fledged Java servlet class.
  • The Container takes what you have written in your JSP, translates it into a servlet class source file, then compiles that into a Java servlet class, after that, it’s just servlets all the way down, and the servlet runs in exactly the same way it would if you had written and compiled the code yourself, in other words the Container loads the servlet class, instantiates and initializes it, makes a separate thread for each request, and calls the servlet’s service() method.
  • We can put regular old Java code in a JSP using a scriplet-which just means Java code within a <% … %> tag. Directives come in three flavors: page; include; taglib.
  • The Java code is between angle brackets with percent signs: <% and %>, but the directive adds an additional character to the start of the element-the @ sign; <% out.println %> equals <%= %>; three different JSP element types: Scriptlet <% %>; Directive <%@ %>; Expression <%= %>.
  • The container takes everything you type between the <%= %> and puts it in as the argument to a statement that prints to the implicit response PrintWriter out, when the container sees this: <%= Counter.getCount() %> it turns it into this: out.print(Counter.getCount());. So Please NEVER end an expression with a semicolon.
  • In an expression, if the method does not return anything, you will get an error, you cannot, MUST NOT use a method with a void return type as an expression, the Container is smart enough to figure out that there won’t be anything to print if the method has a void return type.
  • The page directive is about giving the Container information it needs when translating your JSP into a servlet, the attributes we care about are import, session, content-Type, and isELIgnored.
  • ALL scriptlet and expression code lands in a service method, that means variables declared in a scriptlet are always LOCAL variables.
  • There is another JSP element called a declaration: <%! int count=0 %>; JSP declarations are for declaring members of the generated servlet class, that means both variables and methods, in orther words, anything between the <%! And %> tag is added to the class outside the service method, that means you can declare both static variables and methods. A JSP declaration is always defined inside the class but outside the service method.
  • What the container does with your JSP: 1.Looks at the directives, for information it might need during translation; 2.Creates an HttpServlet subclass; 3.If there is a page directive with an import attribute, it writes the import statements at the top of the class file; 4.If there are declarations, it writes them into the class file, usually just below the class declaration and before the service method; 5.Builds the service method, the service method’s actual name is _js