1. Why do I need page instruction
In the Java file, how to introduce a class from another package.
In the JSP file, how to introduce a class from another package.
(1), using the Import keyword, its advantages are: a reference, used everywhere.
(2), use the fully qualified class name, that is, the class name must be preceded by a complete package name.
In order to use the Import keyword, you need to use the page directive in the JSP file. The advantage of introducing Java class files using the Import keyword is that once introduced, the Java class file is available throughout the entire JSP file range.
2. What is page instruction
The page instruction defines the global attributes in the JSP file by setting multiple properties within. If some properties are not set, the JSP container uses the default directive property values.
The syntax format of the page instruction is:
<%@ Page Property 1 = "Property Value" Property 2 = "Property value 1, property value 2" ... Property N= "Property value"%>
<%@ page language= "java" import= "java.util.*,java.text.*" contenttype= "text/html; CHARSET=GBK "%>
When multiple property values are set on the same property, they are separated by commas.
2.1, Language Property
The Language property in the page directive is used to specify the scripting language used by the current JSP page, and the current JSP version can only use Java as the scripting language. This property can be set without setting, because JSP defaults to Java as a script. The Language property is set by: <%@ page language= "java"%>
2.2. Import Property
The Import property allows you to reference an external class file in a script fragment of a JSP file. If an import property introduces multiple class files, it needs to be separated by commas between multiple class files. Format:
<%@ page import= "java.util.*, java.text.*"%>
or split into:
<%@ page import= "java.util.*"%>
<%@ page import= "java.text.*"%>
2.3, ContentType Property
Enables the Web container to display JSP files in what format on the client browser and what encoding to use. Format:
<%@ page contenttype= "text/html; CHARSET=GBK "%>
When the ContentType property value is set to text/html, the page is displayed in HTML page format.
3. JSP script elements
3.1, what is a small script
Small script is to embed a section of Java code in the JSP page, write syntax: <% java code%>
3.2. What is an expression
An expression is a representation of a data that the system calculates and displays as a value. When the Web container encounters an expression, it evaluates the embedded expression value or variable value, and then returns the result as a string and inserts it into the appropriate page.
Writing syntax: <%=java expression%>
4, in the JSP page declaration method, JSP declaration.
When writing a program, you can use a JSP declaration if you need to define methods for Java scripts. The basic syntax for JSP declarations is: <%! Java declaration%>. There is generally no output in the JSP declaration, which is typically used in conjunction with small scripts and JSP expressions. For example:
<%!
String FormatDate (Date d) {
Java.text.SimpleDateFormat formatter = new SimpleDateFormat ("YYYY year mm month DD Day");
Return Formatter.format (d);
}
%>
Today is <%=formatdate (new Date ())%>