Hello, today for everyone children shoes to share is JSP, quickly take out a small book to write it down

I. Course objectives

Servlet annotation development

【 understanding 】 JSP principle

JSP tag instruction

JSP script writing

Second, servlet annotation development

The JAR package of servlet is provided by Tomcat for processing requests sent from the client to the server. With the continuous development of technology, versions are updated. Currently, versions before 3.0 and after 3.0 are generally used, which have the same functions but have been updated in configuration and writing. But the actual use of choice according to different needs, the essence of the implementation principle is the same

Servlet3.0 provides annotation development to simplify configuration

Grammar:

Write above the class declaration. The annotation current class specifies the name and URL of the servlet for the servlet

Name can be omitted by default the current class name will be used as the name

UrlPatterns are arrays of addresses that can be written to execute the same servlet

shorthand

Annotation writing can be simplified when only the URL is configured and there is only one URL

@WebServlet(“/my”)

Note: Although the annotation form simplifies the configuration, it makes the maintenance of the project more tedious, so it is written according to the actual requirements in the actual development process. Essential annotation development is the same as configuration development, except that the corresponding configuration is performed when the corresponding annotation is found at load time

Third, the JSP

In the process of using servlet to return pages, although dynamic pages can be returned, but if the page has a variety of styles and functions, then the code in the servlet will be a lot, and it will appear in the form of WRITING HTML strings in Java, which is very unfriendly to background development, so, Developed JSP (Java Server Page) Java written service page to solve this problem.

JSP execution process

When a client requests a JSP, the server will parse the JSP into a servlet and create a servlet for processing. If the JSP has been parsed and has not been modified, the server will directly return the previously parsed servlet for processing. Otherwise, it will rewrite the parsing

3.1 Creating a JSP page

Development tools typically provide the creation of JSP pages

Sp through special syntax, the servlet can be directly written HTML code files, just need to identify in the first line is a servlet page, then can be written in accordance with the syntax of HTML page writing, in the process of execution will automatically create output stream objects to output HTML code to the page

< % @page language= “Java” contentType= “text/ HTML; Charset =UTF-8 “pageEncoding=” UTF-8 “%> the form of the instruction makes it executable Java code and writes the corresponding Java command

3.2 page directive

The instructions, written at the top of the JSP line for JSP page Settings, have a number of properties

Grammar:

< % @page attribute 1=” attribute value “Attribute 2=” attribute value 1, attribute value 2″… Attribute n=” attribute value n”%>

JSP has a large number of command operations through Settings to complete different functions

3.3 the script

Because JSP is through special syntax can directly write HTML code and output to the client through the out object, but the essence is Java code, in order to distinguish with the direct output to the client HTML code, so the need to use script block will write in the JSP page Java code package, In this way, when exporting HTML pages to the client, you don’t have to tell Java code to be exported as HTML code

Grammar:

< % Java code %>

Although JSP essence is servlet can directly write Java code for logical operation, but the page appears a large number of Java code will affect the writing, so generally in the writing will be logical operation to the servlet processing

3.4 Method Declaration

Grammar:

Since JSP can be understood as a request in a servlet, methods cannot be declared directly, requiring special declaration syntax

3.5 Other Commands

3.5.1 Compiling Instructions

Instructions that are written in a JSP page without direct display or function and used when the JSP is requested to be compiled

Syntax format:

<%@deriective attr1=val1 attr2=val2…… % >

<%@ directive attribute name =” value “attribute name =” value “%>

The page directive

Mainly set JSP related attributes, such as: page coding, cache size, error processing page, page instruction in addition to import attributes, other attributes can only appear once;

<%@ page language=”java” contentType=”text/html,ISO-8859-1″ import=”java.util.,java.sql.,java.io.*”

The session = “true | flase” buffer = “none 8 KB | | sizekb” autoFlush = “true” | false info = “content of a string”

pageEncoding=”ISO-8859-1″%>

The include directive

In JSP development, in order to avoid code redundancy (repetition), JSP development can be used for many times in the function code package into an independent JSP file.

Include directive: Used to introduce other JSP pages. If you introduce other JSP pages using the include directive, the JSP engine translates the two JSPS into a Servlet, so include directive introduction is usually static as well.

< %@include file= “Absolute or relative URL of the included component “%>

The imported files must follow JSP syntax. An imported file can have any extension, and even if the extension is HTML, the JSP engine will process its contents as it would JSP pages. For the sake of name recognition, the JSP specification recommends using.jSPF (JSP Fragments) as an extension for statically imported files. Since the use of include will involve two JSP pages and translate JSO into a Servlet, the directives for the two JSP pages do not conflict (except for pageEncoding and package guide).

Loading the specified JSP page into the current page before the JSP is compiled is equivalent to copying the specified JSP page code to the specified location

The taglib directive

By using the taglib directive, developers can import other tag libraries to perform specific functions.

The < % @ taglib prefix = “c” uri = “java.sun.com/jsp/jstl/co… % >

Although JSP is only used for data display, but still write Java code, so provide a third-party tag library, through the taglib directive can be introduced in THE JSP page tag library, through the specific tag to achieve the original Java code (JSP page through these tags complete without writing any Java code)

3.5.2 Action instruction

Instructions that provide functionality during the execution of a JSP after it is compiled into servlets.

Syntax format:

<%jsp:deriective attr1=val1 attr2=val2…… % >

<% JSP: directive attribute name =” value “attribute name =” value” attribute name =” value “%>

Param instruction

Used to pass parameters, must be used with other labels that support parameter instructions.

< JSP :forward page=”index.jsp”>—- to the index.jsp page

< JSP :param name=”name” value=” heart rain “/>

<jsp:param name=”password” value=”123″/>

</jsp:forward>

Forwoad instruction

Perform a page jump to forward the processing of the request to the next page. < JSP :forward page= “date.jsp” />

a.jsp

b.jsp

Instructions are written in Java code through tags

The useBean command

Creates an object by calling the no-argument constructor of the specified class

SetProperty instruction

Sets the value of the specified property of the specified object

GetProperty instruction

Gets the value of the specified property of the specified object

The include directive

<jsp:include page=”index.jsp”/>

<%– import the specified page into the current page –%>

Include Differs between a compilation directive and an include action directive

3.6 JSP execution process

When the client requests to specify JSP, it first looks in the translated file to see if it can be modified. If it exists and has not been modified, the operation will be performed directly. If it does not exist or has been modified, then the JSP will be translated into servlet. The request is handed over to a servlet object for processing.

Translation stage: When there is no corresponding servlet instance in the server or the JSP is modified, the translation stage is entered to translate the JSP into the corresponding Setvlet

Compile phase: The corresponding serlvet.java is compiled into the corresponding.class execution file

Execution stage: call the corresponding servlet initialization method to create the corresponding servlet instance object and save it to the server, and associate the instance with THE URL request in the form of configuration to obtain annotations. When requested again, the corresponding instance is directly used for service processing

3.7 the JSP component

Static content:

Static htML-related page data (HTML, CSS, JS, and front-end code) written in a JSP

Note:

<%- Comment content -%>

Content written in a JSP that will not be compiled at compile time

Small script:

<% Java code %>

Java code written directly in JSP, code executed in the code into servlets

Statement:

The < %! Declare method %>

Used to declare methods in JSPS (part of a declaration script)

Expressions:

<%= Expression with return value %>

Printing the result of an expression directly to the page is equivalent to out.print(an expression script)

Instructions:

Compile instruction <%@ instruction attribute = value %>

page/include/taglib

Action directive < JSP: directive attribute = value /.>

forward/param/include/useBean/setProperty/getProperty

Mimicking HTML tag styles to reduce the amount of Java code written on JSP pages

Well, this is the end of today’s article, I hope to help you confused in front of the screen