A Brief Analysis of Basic Grammar in JSP Introduction Course

  • 2021-10-11 19:18:47
  • OfStack

This paper analyzes the basic syntax of JSP. Share it for your reference, as follows:

1. Instructions < % @ directives% >

The JSP instruction is the engine of JSP. They do not directly produce any visual output, but simply indicate what the engine needs to do with the remaining JSP pages. Instructions are made by < %@ ?% > Mark. The two main types of instructions are page and include. This article does not discuss the directive taglib, but it is used when creating custom tags in JSP 1.1.

The instruction page can be found at the top of almost all JSP pages. Although not required, you can use it to define things like where to find support for the Java class.


<%@ page import="java.util.Date" %>

Indicates where to direct network users when Java operational problems occur:


<%@ page errorPage="errorPage.jsp" %>

Whether information needs to be managed at the user session level is likely to span multiple Web pages (this is described more in the section on JavaBeans):


<%@ page session="true" %>

The directive "include" divides your content into more manageable elements, such as elements that include a regular page header or footer. Included pages can be a fixed HTML page or more JSP content:


<%@ include file="filename.jsp" %>

2. Statement < %! Declare% >

JSP declarations allow you to define page level 1 variables to hold information or define support methods that may be needed for the remaining JSP pages. If you find yourself writing too much code, it's usually best to write to a separate Java class. Declare by < %! ?% > Definition. An Java statement that must end the variable declaration with a semicolon and anything must be valid:


<%! int i=0; %>

3. Expressions < % = Expression% >

With the expression in JSP, the result of the evaluated expression is converted to a string and included directly in the output page. The JSP string is defined by the < %= ?% > Label flag, and does not include semicolons unless it is a referenced partial string.


<%= i %>

<%= "Hello" %>

4. Code segment/script segment < % code snippet% >

JSP code snippets or script snippets are embedded in " < % ?% > "In the tag. This Java code runs when the Web server responds to the request. Surrounding the script snippet may be pure HTML or XML code, where the snippet allows you to create conditional execution code, or just call another snippet of code. For example, the following code uses a combination of expressions and script snippets to display the string "Hello" in the H1, H2, H3, and H4 tags. Script fragments are not limited to one line of source code:


<% for (inti=1; i<=4; i++) { %>
<H<%=i%>>Hello</H<%=i%>>
<% } %>

5. Notes < %--Notes--% >

The last key element of JSP is about embedding annotations. Although you can always include HTML comments in your files, users will see these comments once they look at the page source code. If you don't want users to see comments, you should embed them into the < %-- ?--% > In the tag:


<%-- comment for server side only --%>

I hope this article is helpful to everyone's JSP programming.


Related articles: