Basic jsp knowledge (1): Basic jsp knowledge

Source: Internet
Author: User

Basic jsp knowledge (1): Basic jsp knowledge

1. JSP stands for Java Server Pages. Like servle technology, JSP is a technology defined by SUN to develop dynamic web resources.

2. JSP technology is also a dynamic web Resource Development Technology
JSP technology allows you to nest java code on pages to generate dynamic data. When the web server executes jsp, the web server transmits the web development-related objects to jsp, jsp can interact with browsers through these objects. Therefore, jsp is also a dynamic web resource development technology.

3. Comparison between JSP and Servlet

I learned Servlet first. Servlet seems to be writing pure java code, while jsp can both write java code and script language. To a large extent, jsp and servlet are very similar, except that the jsp site is higher and many servlet operations are encapsulated. Both JSP and Servlet can be used to develop dynamic web resources. However, due to their respective characteristics, servlet is gradually used as a controller component in web applications in long-term software practices, JSP technology is used as a data display template.

4. Schematic diagram of JSP:


Principles:

(1) The Web Container depends on the resource type accessed by the client (the Web container is determined based on the first line of code of the resource). If you access a JSP resource, it is handled by the JSP Engine. If Servlet resources are accessed, they are handled by the Servlet engine.
(2) if it is a JSP resource, the JSP Engine translates it into a Servlet resource and transmits the relevant object. If it is a static resource. write () format output. If it is a dynamic resource, it is output in the out. output in print () format. (This view C: \ apache-tomcat-7.0.22-windows-x86 \ apache-tomcat-7.0.22 \ work \ Catalina \ localhost under the corresponding project file source code can be seen)
(3) When the JSP Engine is correctly translated, it is output to the Servlet engine and processed as a Servlet.
(4) If you access the same jspname. jsp file again, the translation work is still ongoing, and the Servlet engine work can be reduced, so this is the reason why the N times are faster than 1st times.

5. JSP syntax
(1) JSP template Elements
The template element is the static content in HTML, that is, the tags used in html, such as <body>, <table>, and <form>.

(2) JSP expressions
A> Syntax: <% = "string variable or expression" %>
B> end with no semicolon
C> output to the browser by default


(3) JSP script snippets
A> Syntax: <%... java code; %>
B> the annotator in the script snippet is consistent with the java code.
C> variables defined in multiple JSP script fragments are essentially local variables in the _ jspService () method and can be accessed.

Note: After the JSP is modified, you do not need to redeploy it and refresh it directly. The Web Container will automatically compare the new and old JSP versions.


(4) JSP Declaration
A> Syntax: <%! ... Java code %>
B> variables or methods declared by JSP become instance variables of Servlet, instance methods, or common methods.
C> Local methods cannot be defined in JSP script fragments. methods can only be defined in JSP declarations.

5) JSP comments
A> Syntax: <% -- Comment content -- %>
B> for HTML comments, the JSP Engine translates them into servlet content,
For JSP comments, the JSP engine does not translate them into servlet content.

Therefore, based on the above, jsp annotation should be preferred for annotations.

C> annotations cannot be nested.

6) JSP commands
A> commands are the basis for programmers to control the JSP Engine.
B> there are three types:
Page
Include
Tablib
C> the page directive has the following attributes:
Language = "specifies the language supported by JSP. The default is java language"
Import = "the package to be imported on the current JSP page, where import can be written multiple times"
Session = "true indicates that the server needs to create a session/false, and the server does not need to create a session". The default value is true.
Buffer = "none | 8kb | sizekb" the cache size used by JSP output. The default value is 8 KB.
AutoFlush = "true indicates that when the cache is full, the web container is automatically refreshed to the client/false and needs to be manually refreshed to the client". The default value is true.
IsThreadSafe = "true indicates that the web server ensures thread security./false indicates that thread security is not ensured." The default value is true.
Info = "text" indicates the description of the jsp. You can use getServletInfo () to obtain the jsp information.
ErrorPage = "the target page forwarded to after an error occurs on the current jsp page"
IsErrorPage = "true" (when a jsp page has this attribute, the web Container automatically creates an exception object.
The above exception handling is a local Exception Handling
In the web. xml file:
<Error-page>
<Error-code> 500 </error-code>
<Location>/error_500.jsp </location>
</Error-page>
<Error-page>
<Exception-type> java. lang. NumberFormatException </exception-type>
<Location>/error_500.jsp </location>
</Error-page>

The Configuration File Code shows that when a browser code 500 exception occurs, the page will jump to the error_500.jsp page. If a NumberFormatException occurs, the page will also jump to error_500.jsp, you can modify the parameter value of <location> On the page you want to jump to. This shows how to handle exceptions in the configuration file. This is a global exception handling method.

If there is code and type in the global, both are displayed at the same time.
When both global and local exceptions occur, local exceptions take precedence.

A) JSP pages are encoded in UTF-8
B) JSP adopts UTF-8 encoding when saving
C) Instruct the browser to view in UTF-8

ContentType = "text/html; charsets = UTF-8"
IsELIgnored = "false table method JSP engine does not ignore EL Expression Language/true table method JSP Engine ignores EL Expression Language"


Basic jsp knowledge

<% @ %> This is a directive element that specifies page information. We recommend that you import a JSTL library. If you want to use an EL function fn: escapeXml (), declare <% @ taglib prefix = "fn" uri = "java.sun.com/jsp/jstl/core" %>
<%! %>, <%> Is used to add small pieces of code on the JSP page, such as Java code
<%> The entire code block can be added to the page.
<%! %> This is used for declaration. This is not recommended.
Another <% = %> is used to add an expression and obtain the result. For example, <% = 1 + 1%>

Basic jsp knowledge

This sentence is not in jsp.
This is the content output by jsp, which belongs to the html structure.

<Form is a form.
Action is action

? Act = up is the submitted address. Assume that Your webpage is 127.0.0.1.
The submission address here is 127.0.0.1 /? Act = up

The method is post, which indicates the post submission method. In addition, there is a get submission method.
The get submission method is to put the parameters in the url address, while the post is in the http header.

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.