JSP syntax and JSP basic knowledge points

Source: Internet
Author: User

"JSP basic Knowledge Point"
The JSP full name is Java Server Pages, which, like servlet technology, is a technology defined by sun to develop dynamic Web resources.
Jsp/servlet specification. JSP is actually a servlet.
The biggest feature of JSP technology is that writing JSP is like writing HTML, but it can only provide static data to the user compared to HTML, while JSP technology allows to nest Java code in the page and provide dynamic data to the user.

[JSP template element]
The HTML content in a JSP page is called a JSP template element

The JSP template element defines the basic skeleton of a Web page, which defines the structure and appearance of the page.

Several large objects of JSP
PageContext pagecontext = null;
HttpSession session = NULL;
ServletContext application = null;
ServletConfig config = null;
JspWriter out = null;
Object page = this;
JspWriter _jspx_out = null;
PageContext _jspx_page_context = null;

_jspx_page_context = PageContext;
application = Pagecontext.getservletcontext ();
Config = Pagecontext.getservletconfig ();
Session = Pagecontext.getsession ();
out = Pagecontext.getout ();
_jspx_out = out;

[JSP script, as follows]

  <body>  <%  request.setattribute ("name", "Zhang San");  Request.getrequestdispatcher ("/one02.jsp"). Forward (request, response);   %>  </body>
Java code before "<% ******%>" is called a JSP script

[JSP expression, as follows]

   <body>     Script expression (output a variable): <%=request.getattribute ("name")%>  </body>
In "<%= write expression%>" such as the expression of JSP expressions, note: The expression is not a statement, all the last can not have ";" No.


For example, the following code

     <table border=1 width= "$" >     <% for     (int i=0;i<4;i++) {      %>      <tr>      <td> <%=i%></td>      <td>112</td>      </tr>      <%      }       %>     </ Table>
The results shown are:


In the JSP page, you can use the above method clever use of HTML page tags and jsp java script and expressions to form a loop, output table


[JSP Declaration]

     <%!     int a=0;     public void Add () {     System.out.println ("good everyone");     }          public class a{public     void Add1 () {     System.out.println ("Hello everyone, I am an inner class");}     }      %>      <%       this.add ();       A a=new a ();      A.ADD1 ();       %>

When the background is automatically compiled, it becomes:


Variables in the JSP declaration can be found, become properties of the class, and the class becomes an inner class.

In the <%! %> "Inside the Java code can define methods and properties


[JSP comments]

  <body>    <%--I'm a JSP comment--%>  </body>
When the JSP engine translates a JSP page into a servlet program, it ignores the content that is commented on the JSP page.

[JSP Directive]
JSP directives (Directive) are designed for the JSP engine, and they do not directly produce any visible output, but simply tell the engine how to handle the rest of the JSP page.
Three directives were defined in the JSP2.0 specification:


Static instructions
Page directive
Lnclude directive
TAGLIB directive

For example:
<%@ page import= "java.awt.*"%>
You can reference a Java package to a page by using the page directive.
The properties in "<%@%>" are as follows:
"Pageencoding=" UTF-8 "" Sets the default encoding for JSP pages and the default encoding for client pages is UTF-8
"Contenttype=" Text/html;charset=iso-8859-1 "" sets the client-side encoding, which by default is iso-8859-1 and pageencoding no difference on the page display
"Import=" "" is used to introduce jar packages in the current JSP page
"Language=" java "" specifies that the script language supported by the current page is Java (currently JSP only supports the Java scripting language)
"Autoflush=" true "" is automatically refreshed and automatically sent by the server to the client (generally do not modify)
"Buffer=" 8KB "" specifies that the buffer size is 8kb by default
"Extends" inheritance, you can modify the inherited classes, you can write your own class inheritance. A class is inherited by default.
"Info" key value pairs, can be obtained on the server. Generally seldom used
"Iselignored=" false "" supports El expressions, and if true, indicates that El expressions are not supported.
"Isthreadsafe=" true "" is thread safe, the default is true, which means thread-safe
"Session=" true "" to see if the session can be used on the page. The default is true, which means that it can be used directly on JSP pages.

"Iserrorpage" defines whether the current page is an error handling page. Default is false not error handling page
"ErrorPage" specifies that when the current page JSP script sends an error, the error is handled on the page. Generally used in conjunction with Iserrorpage. Equivalent error handling mechanism


The page directive is used to define the various properties of the JSP page, regardless of where the page directive appears on the JSP page, it is the entire JSP page, in order to maintain the readability of the program and follow good programming habits, the page instruction is best placed in the entire JSP page starting position.
The ErrorPage property is set to a path (relative or absolute) and, if preceded by "/", indicates the root directory relative to the current Web application (note the site root, otherwise, the new team path)
You can use the <exception-type> child element in the Web. xml file to specify the fully qualified name of the exception class,<loaction> element specifies an error-handling page path that begins with "/".

The global configuration in Web. XML should look like this:

  <error-page>  <error-code>404</error-code>  <location>/one04.jsp</location >  </error-page>


The following is a demonstration of an error handling

The trigger error page code is:

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"  errorpage= "one05.jsp"%><%string Path = Request.getcontextpath (); String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >

Define the error handling page code as:

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8" iserrorpage= "true"%><%string Path = Request.getcontextpath (); String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >

The results shown are:



Include contains
1. Static include: Refers to the code level of the inclusion, refers to the target page of the source copy of the document included in the
2. Dynamic inclusion: Refers to the execution of the target page, the results are included in
For example:

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><%@ page import= "java.awt.*"%><% String path = Request.getcontextpath (); String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
The one06.jsp page is:

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >


The one07.jsp page is:

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><%string path = Request.getcontextpath () ; String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >

The include directive is used to introduce other JSP pages, and if a different JSP page is introduced with the include directive, the JSP engine translates the two jsps into a servlet. So the include directive is also often referred to as static ingestion.
Grammar:
<%@ include file= "absolute URL of the contained component"%>
Where the file property is used to specify the path of the introduced files. The path begins with "/" and represents the current web app.
Details:
The file being introduced must follow the JSP syntax
The introduced file can use any extension, even if its extension is the html,jsp engine that handles the contents of the JSP page as it is handled, and the JSP specification recommends using the. JSPF (JSP fragments) as the extension of the static ingest file.
Since the use of the include directive will involve 2 JSP pages and will translate 2 jsps into a servlet, the instructions for the 2 JSP pages do not conflict (except for pageencoding and guide packages).




Taglib directives for importing tag libraries in JSP pages





JSP syntax and JSP basic knowledge points

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.