"Head First Servlets & JSP" using JSP

Source: Internet
Author: User

Learning Points of knowledge

JSP, which will eventually become a servlet

The JSP eventually becomes a complete servlet that runs in the Web application, except that the Servlet class is written by the container.

The Scriptlet in JSP

The so-called Scriptlet is the Java code placed in the <%...%> tag. Such as:

 
   
  
  1. <%
  2. out.println(com.inspur.Counter.getCount());
  3. %>
Directives in the JSP

The difference between JSP directives and scriptlet is that there is one more @ sign. Note There are no spaces in the middle.
Importing a package using the page directive

    • To import a package:
 
   
  
  1. <%@ page import="com.inspur.*"%>
  2. <%
  3. out.println(Counter.getCount());
  4. %>
  5. </body>
    • Import multiple packages, using an import property, except that multiple packages are separated by commas
 
   
  
  1. <%@ page import="com.inspur.*,java.util.*"%>
JSP expressions

Similar to the Scriptlet code, the expression has one more =. Note that there are no spaces in the middle, and that there are no semicolons at the end of the expression!
Scriptlet Code:
<% out.println(Counter.getCount());%>
Expression code:
<%= Counter.getCount()%>

Why does an expression not end with a semicolon?
The expression becomes a parameter of Out.print ()!
That is <%= Counter.getCount()%> , the container will be converted to:
<% out.print(Counter.getCounter());%>, if you add a semicolon, it will be brought into the outer brackets resulting in a compilation error.
In addition, out is an implicit object.

Claims in the JSP

If you declare the count variable using Java syntax:
<% int count=0;%>
In fact, it is scriptlet!
However, there is a real JSP declaration, and the scriptlet difference lies in adding one!:
<%! int count=0;%>
The difference between the above two statements:
All scriptlet and expression codes are placed in service methods, that is, variables declared in Scriptlet are always local variables!
The JSP declares the variable is the class variable, the JSP declares the method is the class Method!

How does a container handle JSP?


The following is a simple example:

An implicit object
API an Implicit object
JspWriter Out
HttpServletRequest Request
HttpServletResponse Response
HttpSession Session
ServletContext Application
ServletConfig Config
Jspexception exception
PageContext PageContext
Object Page

Note that PageContext encapsulates other implicit objects, even if has a pagecontext reference, and can get references to other implicit objects and get the properties of all scopes.

JSP annotations

<!-- HTML 注释 用户在浏览器就可以看到我 -->
<%-- JSP注释,页面开发人员才可能看到我 --%>

The API of the servlet generated by the JSP


Our main focus is on three key methods: Jspinit (), Jspdestroy (), and _jspservice ().
The first two methods can be overridden, and the last method cannot be overwritten, which is why the underscore begins.
You can see how the method is overridden later in this article.

The life cycle of a JSP





Note that the conversion and compilation of the JSP only happens once, similar to the other servlets, once the loading is initialized, only one thing happens when the request is created or assigned a thread to run the service method.
However, the first person to access the JSP page may have to wait a while, but the JSP specification mentions a recommended JSP pre-compilation protocol that you can look at.

Initialize JSP

Servlet initialization can be done in a JSP, but this is slightly different from the practice in a regular servlet.

    • Configuring servlet Initialization Parameters
    • Cover Jspinit ()
      Use the Jspinit () method to get a servlet initialization parameter (the parameter already configured in DD) and use this parameter value to set an Application scope property:
Properties in a JSP

In most cases, you use one of the 4 implicit objects to get and set the properties in the 4 JSP scopes.
4 scopes? Processing the request, session, and application (context) scopes in a standard servlet, the JSP fourth scope is the page scope, which is obtained from the PageContext object.

As I said earlier, PageContext encapsulates references to other scopes, so here's an example:

    • Examples of using PageContext to get and set properties:

JSP directives and Directive properties

Page is a JSP directive, import is one of its 13 properties, then what are the other properties of the page directive?

El Expressions in JSPs

It is not good to put servlets, declarations, and expressions in a JSP:
1) Web page designers should not be required to understand Java
2) Java code in JSP is difficult to modify and maintain
The user of EL (Expression Language) provides an easier way to invoke Java code, but the code itself is placed in place.
The code might be in a normal Java class, perhaps a JavaBean, a class with a static method, or some label processor.
In short, according to today's best practice, you can no longer write method code in JSP, if you want to write Java methods Elsewhere, and then use El to invoke.

    • Block script elements in a JSP
      Use <scripting-invalid> in DD

    • Ignore El
      If the JSP has template text (normal HTML or text), which happens to include something like El (${somethind}), tell the container to ignore these seemingly el things. (The default El is enabled.) )
      Place in DD

Action elements in a JSP
    • Standard action:
      <jsp:include page="Footer.jsp" />
    • Other actions
      <c:set var="rate" value="32" />

"Head First Servlets & JSP" using JSP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.