YouTip LogoYouTip

Jsp Syntax

This section will provide a brief introduction to the basic syntax used in JSP development. * * * ## Scriptlets A scriptlet can contain any number of Java statements, variables, methods, or expressions, as long as they are valid in the scripting language. The syntax format for a scriptlet: Alternatively, you can write an equivalent XML statement, like this: code fragment Any text, HTML tags, or JSP elements must be written outside the scriptlet. Here is an example, which is also the first JSP example in this tutorial: Hello World Hello World!
**Note:** Please ensure that Apache Tomcat is installed in the C:apache-tomcat-7.0.2 directory and the runtime environment is set up correctly. Save the above code in a file named hello.jsp, then place it in the C:apache-tomcat-7.0.2webappsROOT directory. Open a browser and enter http://localhost:8080/hello.jsp in the address bar. After running, you will get the following result: !(#) ### Chinese Encoding Issue If we want to display Chinese characters correctly on the page, we need to add the following code at the top of the JSP file: Next, we modify the above program to: Hello World!
Now the Chinese characters will display correctly. * * * ## JSP Declarations A declaration statement can declare one or more variables or methods for use in subsequent Java code. In a JSP file, you must declare these variables and methods before you can use them. The syntax format for a JSP declaration: Alternatively, you can write an equivalent XML statement, like this: code fragment Program example: * * * ## JSP Expressions A JSP expression contains a scripting language expression that is evaluated, converted to a String, and inserted where the expression appears in the JSP file. Since the value of the expression is converted to a String, you can use an expression within a line of text regardless of whether it is an HTML tag. The expression element can contain any expression that follows the Java Language Specification, but you cannot use a semicolon to end the expression. The syntax format for a JSP expression: Similarly, you can write an equivalent XML statement: expression Program example:

Today's date is:

After running, you get the following result: Today's date is: 2016-6-25 13:40:07 * * * ## JSP Comments JSP comments have two main purposes: to comment on code and to comment out a section of code. The syntax format for JSP comments:

Today's date is:

After running, you get the following result: Today's date is: 2016-6-25 13:41:26 Syntax rules for using comments in different situations: | **Syntax** | **Description** | | --- | --- | | | JSP comment; the comment content is not sent to the browser and is not even compiled. | | | HTML comment; the comment content can be seen when viewing the page source code in a browser. | | <% | Represents the static | Represents the static %> constant. | | ' | Single quote used in attributes. | | " | Double quote used in attributes. | * * * ## JSP Directives JSP directives are used to set attributes related to the entire JSP page. The syntax format for JSP directives: There are three types of directive tags: | **Directive** | **Description** | | --- | --- | | | Defines page-dependent attributes, such as scripting language, error page, cache requirements, etc. | | | Includes other files. | | | Introduces tag library definitions, which can be custom tags. | * * * ## JSP Actions JSP action tags use XML syntax structure to control the servlet engine. They can dynamically insert a file, reuse JavaBean components, forward the user to another page, generate related HTML for Java plugins, etc. Action tags have only one syntax format, which strictly follows the XML standard: Action tags are essentially some predefined functions. The following table lists some available JSP action tags: | **Syntax** | **Description** | | --- | --- | | jsp:include | Used to include static or dynamic resources in the current page. | | jsp:useBean | Searches for and initializes a JavaBean component. | | jsp:setProperty | Sets the value of a JavaBean component. | | jsp:getProperty | Inserts the value of a JavaBean component into the output. | | jsp:forward | Forwards a request object containing the user's request from one JSP file to another. | | jsp:plugin | Used to include Applet and JavaBean objects in the generated HTML page. | | jsp:element | Dynamically creates an XML element. | | jsp:attribute | Defines the attribute of a dynamically created XML element. | | jsp:body | Defines the body of a dynamically created XML element. | | jsp:text | Used to wrap template data. | * * * ## JSP Implicit Objects JSP supports nine automatically defined variables, known as implicit objects. These objects are automatically available in the JSP page without requiring additional declaration or initialization. A brief introduction to these nine implicit objects is provided in the following table: | **Object** | **Description** | | --- | --- | | request | An instance of the **HttpServletRequest** class, representing the HTTP request object. It contains information sent from the client to the server, such as form data, URL parameters, etc. | | response | An instance of the **HttpServletResponse** class, representing the HTTP response object. It is used to send data and responses to the client. | | out | An instance of the **JspWriter** class, used to output text content to the client, typically for generating HTML. | | session | An instance of the **HttpSession** class, representing the user session object. It can be used to store and retrieve user-specific data across multiple pages. | | application | An instance of the **ServletContext** class, representing the context of the web application. It can be used to store and retrieve global application data. | | config | An instance of the **ServletConfig** class, containing configuration information about the current JSP page, such as initialization parameters. | | pageContext | An instance of the **PageContext** class, providing access to all objects and namespaces of the JSP page. | | page | Similar to the `this` keyword in a Java class, it represents the instance of the current JSP page and can be used to call methods of the page. | | exception | An object of the **exception** class, representing the corresponding exception object in the JSP page where an error occurred. It is used to handle exceptions in JSP pages and can be used to catch and handle exceptions that occur on the page. | * * * ## Control Flow Statements JSP provides full support for the Java language. You can use the Java API in JSP programs and even establish Java code blocks, including conditional statements and loop statements, etc. ## Conditional Statements **if…else** block, see the following example:

IF...ELSE Instance

Today is a weekend

Today is not a weekend

After running, you get the following result: IF...ELSE InstanceToday is not a weekend Now let's look at the switch…case block, which is quite different from the if…else block. It uses `out.println()` and is entirely contained within the scriptlet tag, like this:

SWITCH...CASE Instance

Accessing via a browser, after running you get the following result: SWITCH...CASE InstanceWednesday * * * ## Loop Statements In JSP programs, you can use Java's three basic loop types: for, while, and do…while. Let's look at an example of a for loop, which outputs "" in different font sizes:

For Loop Instance

<%for ( fontSize = 1; fontSize <font color="green" size="">
After running, you get the following result: !(#) Now rewrite the above example using a while loop:

While Loop Instance

<%while ( fontSize <font color="green" size="">
Accessing via a browser, the output is (since fontSize is initialized to 0, an extra line is output): !(#) JSP Operators JSP supports all Java logical and arithmetic operators. The following table lists common JSP operators, ordered from highest to lowest precedence: | **Category** | **Operator** | **Associativity** | | --- | --- | --- | | Postfix | () [] . (dot operator) | Left to right | | Unary | ++ - - ! ~ | Right to left | | Multiplicative | * / % | Left to right | | Additive | + - | Left to right | | Shift | >>>>><>= <>= <<= &= ^= |= | Right to left | | Comma | , | Left to right | * * * ## JSP Literals The JSP language defines the following literals: * Boolean (boolean): true and false; * Integer (int): same as in Java; * Floating-point (float): same as in Java; * String (string): starts and ends with single or double quotes; * Null: null.
← Sqlite UpdateSqlite And Or Clauses β†’