Jsp -- & gt; Lesson 2 Summary

Source: Internet
Author: User
Tags html comment

JSP implicit object
Public void _ jspService (HttpServletRequest request,
HttpServletResponse response)
Throws java. io. IOException, ServletException
{
JspFactory _ jspxFactory = null;
PageContext pageContext = null;
HttpSession session = null;
ServletContext application = null;
ServletConfig config = null;
JspWriter out = null;
Object page = this;
...
...
Throwable exception =
Org. apache. jasper. runtime. JspRuntimeLibrary. getThrowable (request );
If (exception! = Null ){
Response. setStatus (HttpServletResponse. SC _INTERNAL_SERVER_ERROR );
}
JSP syntax
JSP template Elements
The HTML content on the JSP page is called a JSP template element.
The JSP template element defines the basic skeleton of a webpage, that is, the structure and appearance of the page.
JSP script expression (expression) is used to output program data to the client.
Syntax: <% = variable or expression %>
Example: Current Time: <% = new java. util. Date () %>
When translating a script expression, the JSP Engine converts the program data into a string and uses out. print (...) Data is lost to the client.
Variables or expressions in JSP script expressions cannot be followed by semicolons (;).
JSP script snippets (1)
The JSP script snippet (scriptlet) is used to write multiple lines of Java code on the JSP page. Syntax:
<%
Multi-line java code
%>
Note: Only java code can appear in JSP script fragments, and no other template elements can appear. The JSP Engine translates JSP pages, the Java code in the JSP script segment will be stored in the Servlet's _ jspService method.
Java code in JSP script snippets must strictly follow the Java syntax. For example, each execution statement must end with a semicolon.
JSP script snippets (2)
A JSP page can contain multiple script fragments. Text, HTML tags, and other JSP elements can be embedded between two or more script fragments.
Example:
<%
Int x = 10;
Out. println (x );
%>
<P> This is the JSP page text. </p>
<%
Int y = 20;
Out. println (x );
%>
The code in multiple script fragments can access each other, as if all the code is placed in a pair of <%>. For example, out. println (x );
JSP script snippets (3)
The Java statements in a single script fragment can be incomplete. However, the result after the combination of multiple script fragments must be a complete Java statement, for example:
<%
For (int I = 1; I <5; I ++)
{
%>
<H1> www.it315.org </H1>
<%
}
%>
JSP Declaration
All code written on the JSP page is translated to the servlet service method by default, and the java code in the Jsp declaration is translated to the _ jspService method. Syntax:
<%!
Java code
%>
Therefore, JSP declarations can be used to define static code blocks, member variables, and methods of Servlet programs converted from JSP pages.
Multiple Static code blocks, variables, and functions can be defined in one JSP declaration, or they can be separately defined in multiple JSP declarations.
The scope of JSP implicit objects is limited to Servlet's _ jspService method. Therefore, these implicit objects cannot be used in JSP declarations.
JSP declaration à case
<%!
Static
{
System. out. println ("loading Servlet! ");
}
Private int globalVar = 0;
Public void jspInit ()
{
System. out. println ("initializing jsp! ");
}
%>
<%!
Public void jspDestroy ()
{
System. out. println ("destroying jsp! ");
}
%>
JSP comments
JSP annotation format:
<% -- Comment -- %>
When the JSP Engine translates a JSP page into a Servlet program, it ignores the commented content on the JSP page.
Note
There are three ways to comment
HTML comments (output comments): the comments can be seen when the client views the source code. For example,
<! -- This is an html comment. it will show up int the response. -->
JSP page comment (hidden comment): The comment is written in the JSP program but not sent to the customer. Therefore, the Comment cannot be seen when the client views the source code. Such annotations are ignored during JSP compilation.
<% -- This is a JSP comment. it will only be seen in jsp code -- %>
Java Note: it can only appear in the Java code area and cannot appear directly on the page. // Single line comment/* multi-line comment */
Example exercise-JiuJiudemo
Create a Web Project.
Compile the index1.jsp page. Two input text boxes are provided on the page to collect the data submitted by the user. The "Submit" and "cancel" buttons are provided to help you submit or cancel the corresponding operations.

 
 
Compile the result1.jsp page and display the multiplication table based on the number submitted by the user.

 
 
 
JSP commands
JSP commands (directive) are designed for the JSP Engine. They do not directly generate any visible output, but only tell the engine how to process the rest of the JSP page. Three commands are defined in the JSP 2.0 specification:
Page command
Include command
Taglib command
Introduction to JSP commands
Basic syntax format of JSP commands:
<% @ Command attribute name = "value" %>
Example: <% @ page contentType = "text/html; charset = gb2312" %>
If a command has multiple attributes, these attributes can be written in one command or written separately.
For example:
<% @ Page contentType = "text/html; charset = gb2312" %>
<% @ Page import = "java. util. Date" %>
You can also write:
<% @ Page contentType = "text/html; charset = gb2312" import = "java. util. Date" %>
Page command
The page command defines various attributes of a JSP page. No matter where the page command appears on a JSP page, it serves the entire JSP page, to keep the program readable and follow good programming habits, the page instruction should be placed at the beginning of the entire JSP page.
The complete syntax of the page command defined in the JSP 2.0 specification:
<% @ Page
[Language = "java"]
[Extends = "package. class"]
[Import = "{package. class | package. *},..."]
[Session = "true | false"]
[Buffer = "none | 8kb | sizekb"]
[AutoFlush = "true | false"]
[IsThreadSafe = "true | false"]
[Info = "text"]
[ErrorPage = "relative_url"]
[IsErrorPage = "true | false"]
[ContentType = "mimeType [; charset = characterSet]" | "text/html; charset = ISO-8859-1"]
[PageEncoding = "characterSet | ISO-8859-1"]
[IsELIgnored = "true | false"]
%>
Use the page command to solve JSP Chinese garbled characters
The JSP program has the same Chinese garbled characters as the Servlet program.
Chinese garbled characters in the output response body
Chinese garbled characters occur when reading the parameter information passed by the browser.
The JSP Engine may also cause Chinese garbled characters when translating JSP pages into Servlet source files.
JSP Engine JSP source file translated into Servlet Source file by default using UTF-8 encoding, and JSP developers can use a variety of character set encoding to compile JSP source file, therefore, when the JSP Engine translates a JSP source file into a Servlet Source file, it must undergo character encoding conversion.
If the JSP file does not indicate the character set encoding it uses, the JSP Engine treats it as the default ISO8859-1 character set encoding.
How to solve Chinese garbled characters when the JSP Engine translates JSP pages
Use the contentType attribute of the page command to describe the character set encoding of the JSP Source File
The pageEncoding attribute of the page command describes the character set encoding of the JSP source file.
Include command
The include command is used to introduce other JSP pages. If the include command is used to introduce other JSP pages, the JSP Engine translates the Two JSPs into a servlet. So the introduction of include commands is also called static introduction.
Syntax:
<% @ Include file = "relativeURL" %>
The file attribute specifies the path of the imported file. The path starts with "/", indicating the current web application.
Details:
The imported file must follow the JSP syntax.
The introduced file can use any extension. Even if the extension is html, the JSP Engine will process the content in the jsp page as instructed, JSP specifications are recommended. jspf (JSP fragments) serves as the extension of the static introduced file.
Because the include command involves two JSP pages, and the Two JSPs are translated into a servlet, therefore, the commands on these two JSP pages cannot conflict with each other (except pageEncoding and guide package ).
Taglib command
The Taglib command is used to import the tag library on the JSP page.
JSP running principle and nine implicit objects
When each JSP page is accessed for the first time, the WEB Container will send the request to the JSP Engine (a Java program) for processing. The JSP Engine first translates JSP into a _ jspServlet (essentially a servlet) and calls it according to the servlet call method.
Since JSP is translated into servlet during the first access, the first access is usually slow, but the second access, if the JSP Engine finds that JSP has not changed, it will not translate but directly call it, therefore, the execution efficiency of the program will not be affected.
When the JSP Engine calls the _ jspServlet corresponding to JSP, nine web development-related objects will be passed or created for _ jspServlet. To facilitate developers to obtain the reference of these web objects when compiling JSP pages, the JSP technology designer specially defines nine corresponding variables, developers can use these variables on the JSP page to quickly obtain references to these nine objects.
What are these nine objects and their functions are also the knowledge points frequently examined by the written examination.
JSP nine implicit objects
Request
Response
Session
Application
Config
Page
Exception
Out
PageContext

Related Article

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.