Java the basic JSP
1. JSP Introduction
JSP is the abbreviation of JavaServer, is a kind of dynamic webpage technical standard which is advocated by Sun Microsystems Company and many companies participate together. Adding Java program code to the HTML file makes up the JSP Web page. When a Web server encounters a request to access a JSP Web page, it first executes the Java program code in it, and then returns the execution results to the client in HTML form. In fact, on the other hand, the nature of the JSP is the servlet, which is converted to a standard servlet at the time of the release compile run, which typically occurs when the first page request occurs.
The purpose of JSP design is to simplify presentation of presentation layer. The JSP does not add any functionality that is inherently not possible with the servlet. However, it is more convenient to write static HTML in the JSP without having to use the PRINTLN statement to output each line of HTML code. More importantly, with the separation of content and appearance, the tasks of different natures in page making can be conveniently separated.
In addition to ordinary HTML code, the other components of the embedded JSP page include the following three types: script elements, instructions, actions. Where the script element is used to embed Java code, this part of the Java code will be converted directly to a part of the servlet; JSP directives are used to control the structure of the servlet as a whole; JSP actions are used to introduce existing components to control the behavior of the Web container. In addition, to simplify the scripting elements, the JSP defines a set of internal object variables that can be used directly.
2. JSP instruction
A JSP directive is a message sent from a JSP to a Web container, which is used to set the global properties of a page, such as the output content type. The scope of the instruction is limited to the JSP page that contains the instruction itself.
The format of the JSP directive is:
<%@ directive Name property = "attribute value"%>
The directive name has page, include and tablib three kinds. Specific as follows:
(1) Page directive
The page directive is used to define global properties for the entire JSP page. The valid page attributes are language, import, Session, ContentType, IsThreadSafe, buffer, AutoFlush, extends, info, errropage, Iserrorpage and so on.
(2) include directive
JSP pages can insert additional files into the current page through the include directive. This file can be a JSP file, an HTML file, or a text file. The include directives are in the following format:
<% @include file = "Relativeurl"%>
The main role of the include directive is that in a Web application, when multiple JSP pages contain the same content, you can put the same part in a file, and then include the file with the include directive in other files. The advantage of doing this is that when you want to modify this part of the file code, you just need to modify a file.
(3) Taglib directive
When you need to use a custom label in a JSP file, you can specify it by using the TAGLIB directive. Format: <% @taglib uri= "uritotaglibrary" prefix= "TagPrefix"%>
Where the URI is a uniform resource tag, prefix is the identifier for the tag library, and you can uniquely name the custom label based on this prefix.
3. JSP Action
A JSP action is a set of dynamically executed instructions that control the behavior of the Web container. JSP actions can be used to dynamically insert files into a page, rewrite JavaBean components, and direct another page. The following four types of JSP actions are common:
(1) Include action
The include action inserts the specified file into the page that is being generated, in the following format:
<jsp:include page= "Relativeurl" flush= "True|false" >
Include actions and include directives vary greatly in the execution process. The include action does not process the contained file when it is executed, and if the contained file is modified, the runtime can see the results of the included file modifications. The static include directive contains files that must be recompiled if they change, otherwise you can only see the contents of the included files before they are modified. Therefore, use the Include Command award for better performance unless the included files are frequently changed.
(2) Forward action
Forward action is used to direct the current JSP page to another page, the client sees the address is the address of the current page, and the actual content is another page. The format is as follows:
<jsp:forward page= "Relativeurl" >
(3) Param action
The Param action is used to pass parameter information and must be used in conjunction with the include action or forward action. The format is as follows:
<jsp:param name= parameter name, value= value/>
(4) Usebean action
Usebean action enables JSP Web pages to use JavaBean, which makes it possible to fully apply the importance of Java. By using the Usebean action, one or more JavaBean components can be instantiated in a JSP Web page, and these instantiated JavaBean objects can be called in the JSP. The format is:
<jsp:usebean id= "name" class= "classname" scope= "Page|request|session|application" >
4. JSP built-in objects
JSP provides built-in objects that can be used in scripts that make it easier for users to gather information sent through a browser request, respond to a browser, and store user information. JSP provides a total of nine built-in objects: request, response, out, session, Application, config, PageContext, page, and exception. The following six types are commonly used:
(1) Request object
The request object is the most commonly used object in the JSP, representing the requests from the client, which encapsulates the information submitted by the user, such as the information that is filled in form forms, and can obtain information about a customer request by invoking the method of the Request object.
(2) Response object
The response object is used to send server data to the client in response to a client request. You can organize information sent to clients, such as cookies, HTTP headers, and so on, by response objects. However, because the group leader is relatively low-level, it is generally not recommended, you need to send text to the client directly using the Out object.
(3) Session object
The session object refers to a client-to-server conversation, starting with a Web application that connects the client to the server until the client disconnects from the server.
(4) Application Object
If a customer browses a different Web page, a different application object will be created. All JSP pages in the same web app will have access to the same application object, even if the customer browsing these JSP pages is not the same. Once the Application object is created, it will persist unless the server is shut down.
(5) Out object
The Out object can dynamically output the specific data content to the client's browser with JSP program code.
(6) Exception object
The exception object is used to handle all errors and exceptions that occur when a JSP file is executed.
。
Java EE basic JSP