WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

JSP syntax


This section will briefly introduce the basic syntax of JSP development.


Script program

A script can contain any number of Java statements, variables, methods, or expressions as long as they are valid in the scripting language.

The syntax of the script:

<% code segment %>

Alternatively, you can write an XML statement equivalent to it, like this:

<jsp:scriptlet>
   code segment
</jsp:scriptlet>

Any text, HTML tags, and JSP elements must be written outside of the script.

Here's an example, which is also the first JSP example for this tutorial:

<html>
<head><title>Hello World</title></head>
<body>
Hello World!<br/>
<%
out.println("Your IP address is " + request.getRemoteAddr());
%>
</body>
</html>

Note: Make sure 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 hello.jsp, then place it in the C:\apache-tomcat-7.0.2\webapps\ROOT directory, open a browser and type http://localhost:8080/hello in the address bar. .jsp. After running, you get the following results:


Other coding problem

If we want to display Other normally on the page, we need to add the following code in the header of the JSP file.:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

Next we will modify the above program to:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>welookups tutorial(welookups.com)</title>
</head>
<body>
Hello World!<br/>
<%
out.println("Your IP address " + request.getRemoteAddr());
%>
</body>
</html>

This way Other can be displayed normally.。


JSP statement

A declaration statement can declare one or more variables and methods for use by later Java code. In a JSP file, you must declare these variables and methods before you can use them.

Syntax format for JSP declarations:

<%! declaration; [ declaration; ]+ ... %>

Alternatively, you can write an XML statement equivalent to it, like this:

<jsp:declaration>
code segment
</jsp:declaration>

Program example:

<%! int i = 0; %> 
<%! int a, b, c; %> 
<%! Circle a = new Circle(2.0); %> 

JSP expression

A scripting language expression contained in a JSP expression is first converted to a String and then inserted into the place where the expression appears.

Since the value of an expression is converted to a String, you can use the expression in a line of text without regard to whether it is an HTML tag.

Expression elements can contain any expression that conforms to the Java language specification, but you cannot use a semicolon to end an expression.

Syntax format for JSP expressions:

<%= expression %>

Similarly, you can write an equivalent XML statement:

<jsp:expression>
expression
</jsp:expression>

Program example:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>welookups tutorial(welookups.com)</title>
</head>
<body>
<p>
   Today's date is: <%= (new java.util.Date()).toLocaleString()%>
</p>
</body> 
</html> 

Get the following results after running:

Today's date is:  2016-6-25 13:40:07

JSP annotations

JSP annotations have two main purposes: to annotate code and comment out a piece of code.

Syntax format for JSP annotations:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>tutorial(welookups.com)</title>
</head>
<body>
<%-- This part of the comment will not be displayed in the web page--%> 
<p>
   Today's date is: <%= (new java.util.Date()).toLocaleString()%>
</p>
</body> 
</html> 

After running, you get the following results:

Today's date is:  2016-6-25 13:41:26

Syntax rules for using annotations in different situations:

Syntax Description
<%-- Comment --%> JSP comments, comments will not be sent to the browser or even compiled
<!-- Comment --> HTML comments, you can see the comment content when viewing the source code of the web page through the browser
<\% Representative static <%Constant
%\> Representative static %> Constant
\' Single quotes used in attributes
\" Double quotes used in attributes

JSP instruction

The JSP directive is used to set properties related to the entire JSP page.

JSP instruction syntax format:

<%@ directive attribute="value" %>

There are three types of instruction tags here:

Instructions Description
<%@ page ... %> Define the dependency properties of the page, such as scripting language, error page, cache requirements, etc.
<%@ include ... %> Include other files
<%@ taglib ... %> Introduce the definition of the tag library, which can be a custom tag

JSP behavior

The JSP behavior tag uses the XML syntax structure to control the servlet engine. It can dynamically insert a file, reuse JavaBean components, direct users to another page, generate relevant HTML for Java plugins, and more.

Behavior tags have only one grammar format, which strictly adheres to the XML standard:

<jsp:action_name attribute="value" />

Behavior tags are basically some pre-defined functions. The following table lists some of the available JSP behavior tags::

Syntax Description
Jsp:include Used to include static or dynamic resources in the current page
Jsp:useBean Find and initialize a JavaBean component
Jsp:setProperty Set the value of the JavaBean component
Jsp:getProperty Will JavaBean The value of the component is inserted into the output
Jsp:forward Pass a request object containing a user request from one JSP file to another file
Jsp:plugin Used to include applets and JavaBean objects in the generated HTML page
Jsp:element Dynamically create an XML element
Jsp:attribute Defining properties of dynamically created XML elements
Jsp:body Define the body of dynamically created XML elements
Jsp:text Used to encapsulate template data

JSP implicit object

JSP supports nine automatically defined variables, and Jianghu people call hidden objects. A brief introduction to these nine hidden objects is given in the following table:

Object Description
Request Instance of the HttpServletRequest class
Response Instance of the HttpServletResponse class
Out An instance of the PrintWriter class that outputs the results to a web page
Session Instance of the HttpSession class
application ServletContextAn instance of a class, related to the application context
Config Instance of ServletConfig class
pageContext An instance of the PageContext class that provides access to all objects and namespaces on the JSP page
Page Similar to the this keyword in Java classes
Exception ExceptionThe object of the class, representing the corresponding exception object in the JSP page where the error occurred

Control flow statement

JSP provides full support for the Java language. You can use the Java API in JSP programs or even build Java code blocks, including judgment statements and loop statements.

Judgment statement

If...else block, please see the following example:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%! int day = 3; %> 
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title> tutorial(welookups.com)</title>
</head>
<body>
<h3>IF...ELSE Instance</h3>
<% if (day == 1 | day == 7) { %>
      <p>Today is not a weekend</p>
<% } else { %>
      <p>Today is not a weekend</p>
<% } %>
</body> 
</html> 

After running, you get the following results:

IF...ELSE Instances
Today is not a weekend

Now let's take a look at the switch...case block, which is very different from the if...else block. It uses out.println() and is loaded entirely in the script's tag, like this:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%! int day = 3; %> 
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>(welookups.com)</title>
</head>
<body>
<h3>SWITCH...CASE Instance</h3>
<% 
switch(day) {
case 0:
   out.println("Sunday");
   break;
case 1:
   out.println("Monday");
   break;
case 2:
   out.println("Tuesday");
   break;
case 3:
   out.println("Wednesday");
   break;
case 4:
   out.println("Thursday");
   break;
case 5:
   out.println("Friday");
   break;
default:
   out.println("on Saturday");
}
%>
</body> 
</html> 

Browser access, after running, the following results:

SWITCH...CASE Instances

Wednesday

loop statement

The three basic loop types of Java can be used in JSP programs: for, while, and do...while.

Let's take a look at the example of a for loop, the following output of the "novice tutorial" of different font sizes:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%! int fontSize; %> 
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title> tutorial(welookups.com)</title>
</head>
<body>
<h3>For Loop instance</h3>
<%for ( fontSize = 1; fontSize <= 3; fontSize++){ %>
   <font color="green" size="<%= fontSize %>">
 tutorial
   </font><br />
<%}%>
</body> 
</html> 

Use the while loop instead of the above example:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%! int fontSize=0; %> 
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>welookups tutorial(welookups.com)</title>
</head>
<body>
<h3>While Loop instance</h3>
<%while ( fontSize <= 3){ %>
   <font color="green" size="<%= fontSize %>">
    welookups tutorial
   </font><br />
<%fontSize++;%>
<%}%>
</body> 
</html> 

The following table lists the common JSP operators, with priorities from high to high:

Category Operator Combination
Suffix () [] . (dot operator) Left to right
One yuan ++ - - ! ~ Right to left
Multiplicative * / %  Left to right
Additivity + -  Left to right
Shift >> >>> <<   Left to right 
Relationship > >= < <=   Left to right
Equal/not equal == !=  Left to right
Bit and Left to right
Bit XOR Left to right
Bit or Left to right
Logic and &&  Left to right
Logic or ||  Left to right
Conditional judgment ?:  Right to left
Assignment = += -= *= /= %= >>= <<= &= ^= |=  Right to left
comma  Left to right 

JSP literals

The JSP language defines the following literals:

  • Boolean: true and false;
  • Integer (int): same as in Java;
  • Float: the same as in Java;
  • String: start and end with single or double quotes;
  • Null: null.