Initial solution of JSP web page Programming
JSP is one of the most important parts in today's Web development. It is a part of the direct and customer interface. JSP is especially important for commercial software.
1. Introduction to JSP
JSP is the abbreviation of assumerver Pages. It is a dynamic web page technical standard advocated by Sun Microsystems and participated by many companies. Adding Java code to an HTML file constitutes a JSP webpage. When the Web server encounters a request to access the JSP webpage, it first executes the Java program code, and then returns the execution result to the customer with the HTML surname. The Java program is running on the server, and the client is only used for display.
1) Introduction to mainstream Web technologies
JSP technology is not the only dynamic web page technology. Before the emergence of JSP technology, there were several useful dynamic web pages. These technologies mainly include.
> CGI: Common GatewayInterface, which is usually translated as a Common Gateway Interface. It is an interface for the HTTP server to communicate with other programs on the machine. Its appearance changes the webpage from static to dynamic. However, to generate a dynamic web page, when the HTTP server receives a request to the CGI program, each request starts a new process. When a large number of user requests call CGI applications, the execution of Multiple CGI applications will result in a large load on the server, seriously affecting the server system performance.
> ASP: The full name of Active ServerPage. It is a technology developed by Microsoft to process dynamic pages. It can also embed some scripting languages such as JavaScript in HTML. It transfers Web requests to the server and implements interpretation of all ASP scripting languages on the server. ASP is a relatively useful technology, but its disadvantage is that it can only support IIS servers on Microsoft's Windows NT platform.
> PHP: The full name of Personal HomePage. PHP is a cross-platform embedded scripting language for servers. It borrowed a lot of C, Java, and Perl syntax and coupled with PHP's own features, so that Web developers can quickly write dynamic pages. It supports the vast majority of databases currently. And PHP is completely free to download. In addition, users can obtain the source code without restrictions, and even add the features they need. For small projects, it is a suitable programming language. However, PHP is weak for large and more complex projects.
> JSP: Java Server Pages. Compared with the above technologies, JSP has the following advantages:
> Fast running: only compile and load the first request;
> Content generation and display are separated;
> Platform independence: it can be developed in any environment on all platforms;
> It has all the powerful functions of Java;
> Separate content generation and display;
> Use the logo to simplify page development;
> Emphasize reusable group parts.
2) Introduction to the JSP running environment
From the beginning of JSWDK to Tomcat and WebLogic, the running environment of JSP is gradually changing, and many excellent JSP containers are emerging. Common JSP containers include Tomcat, WebLogic, and WebSphere
3) install and start the JSP running environment
Install JDK before installing the JSP running environment. Next, install Tomcat at http://tomcat.apache.org /. After the download is complete, follow the default settings to install. Create a new system variable named TOMCAT_HOME in the system environment variable and set the value to the Tomcat installation directory (my name is d: \ Apache Software Foundation \ Tomcat7.0 _ SingerFive ).
2. JSP program demonstration
Next, let's start with the simplest introduction. First, let's try JSP and understand what JSP is. At the beginning, we may think it is very simple, but as we continue to learn, you will feel the strength of JSP more and more.
1) experience JSP
.
[Html]
<Html>
<Head>
<Meta http-equlv = "Content-Type" content = "text/html; charset = GBK">
<Title> A simple JSP Example </title>
</Head>
<Body>
This is a JSP Example.
</Body>
</Html>
<Html>
<Head>
<Meta http-equlv = "Content-Type" content = "text/html; charset = GBK">
<Title> A simple JSP Example </title>
</Head>
<Body>
This is a JSP Example.
</Body>
</Html>
If you save the preceding code as .html, you can directly open the file in a browser. Otherwise. if jsp is a suffix, you cannot directly double-click the file to view the actual running effect. Instead, you need to deploy it to the JSP Container (Tomcat) and use the JSP Container for parsing before viewing.
2) initial combination of JSP and HTML
The following is a simple JSP and HTML combination program. This program uses the pages command in the JSP script tag command. This knowledge will be detailed in the following sections.
[Html]
<% @ Page contentType = "text/html" pageEncoding = "UTF-8" %>
<Html>
<Head>
<Metahttp-equivmetahttp-equiv = "Content-Type" content = "text/html; charsets = UTF-8"/>
<Title> XXXXXXXXX </title>
</Head>
<Body>
<H1> This is a JSP and HTML </Body>
</Html>
<% @ Page contentType = "text/html" pageEncoding = "UTF-8" %>
<Html>
<Head>
<Metahttp-equiv = "Content-Type" content = "text/html; charsets = UTF-8"/>
<Title> XXXXXXXXX </title>
</Head>
<Body>
<H1> This is a JSP and HTML </Body>
</Html>
Copy the jsp file to jspexample under the Tomcat address (my files are D: \ Apache Software Foundation \ Tomcat7.0 _ SingerFive \ webapps \ examples \ jsp \ jspTest. jsp), enter the address in the browser to open the view.
The page command is used in the first line. When the browser requests a JSP page for the first time, the container first compiles the JSP file into a Servlet class file, and then the container loads the class to the Virtual Machine (Java Virtual Machine) of the container) and run the Servlet class, and finally send the result to the client.
3. Basic JSP syntax
JSP web pages are embedded in HTML web pages by specific JSP elements. In addition to HTML changes, JSP provides 5 elements required for building Web content.
> Directive)
> Declaration)
> Program code (scriptlet)
> Expression)
> Comments)
JSP commands are used to set attributes related to the entire JSP page, such as the script language of the page, the Java package name to be imported, and the page encoding character set. The JSP syntax is as follows:
<% @ Command name attribute = "value" %>
JSP Commands include page, include, and taglib3 commands. JSP commands will be described in detail later.
JSP declaration is used to declare the attributes and methods of the page. The declared attributes and methods apply to any part of JSP, which is equivalent to the member methods and member variables in the class. Its JSP syntax is as follows:
<%! Declaration Part %>
The following Code declares a variable and a method respectively:
<%!
Int I = 1;
%>
<%!
Void method {}
%>
JSP code is the most common script element in JSP. Its essence is a valid Java code. The JSP program code is included between <%... %>. Its JSP syntax is as follows:
<% Java code %>
The following code outputs some content on the client:
[Html]
<% @ PagecontentType = "text/html" pageEncoding = "UTF-8" %>
<%! Int a = 100; %>
<Html>
<Head>
<Metahttp-equivmetahttp-equiv = "Content-Type" content = "text/html; charsets = UTF-8"/>
<Title> XXXXXXXXX </title>
</Head>
<Body>
<H3> Number of outputs from 100 to 200 <%
For (int I = 0; I <100; I ++ ){
Out. println (I + a + "<br> ");
}
%>
</Body>
</Html>
<% @ PagecontentType = "text/html" pageEncoding = "UTF-8" %>
<%! Int a = 100; %>
<Html>
<Head>
<Metahttp-equiv = "Content-Type" content = "text/html; charsets = UTF-8"/>
<Title> XXXXXXXXX </title>
</Head>
<Body>
<H3> Number of outputs from 100 to 200 <%
For (int I = 0; I <100; I ++ ){
Out. println (I + a + "<br> ");
}
%>
</Body>
</Html>
The tag pair used by the JSP expression is <% =... %>. The result is converted to a string and displayed on the page. For example, this code implements a simple jianfa operation:
[Html]
<% @ PagecontentType = "text/html" pageEncoding = "UTF-8" %>
<Html>
<Head>
<Title> XXXXXXXXX </title>
</Head>
<Body>
<H3> operation: 5-2 = <% = (5-2) %> </Body>
</Html>
<% @ PagecontentType = "text/html" pageEncoding = "UTF-8" %>
<Html>
<Head>
<Title> XXXXXXXXX </title>
</Head>
<Body>
<H3> operation: 5-2 = <% = (5-2) %> </Body>
</Html>
JSP comments are used to explain and describe the program code. Common comments include display comments and implicit comments. The display comment mainly refers to the comment of an HTML file. When a request contains a JSP page that displays the comment, you can view the comment through the source code. The syntax is as follows:
<! -Comment content -->
Example: <body>
<H3> This is a page that displays comments
<! -- Comment on the description here -->
</Body>
The display comment is displayed when the source file (HTML) is viewed in the browser, and the implicit comment is included in <% --... the content between -- %> cannot be seen by implicit comments on JSP pages, and cannot be seen in HTML source files. Its syntax is as follows:
<% -- Comment content -- %>
· The following code uses implicit annotations. You can further understand the differences between the two annotations by comparing the two examples:
<Body>
<H3> This is a page that displays comments
<% -- Comment here -- %>
</Body>
4. JSP commands
JSP commands are used to set attributes related to the entire JSP page. JSP Commands include commands in page, include, and taglib3. The following describes the page instruction and include instruction elements in detail.
1) page commands
The Page command defines many attributes of a Page, such as the script language, encoding method, and imported java package. The syntax of the Page command is as follows:
[Html]
<% @ Page
Language = "java"
Session = "true/false"
PageEncoding = "characterSet"
IsThreadSafe = "true/false"
IsErrorPage = "true/false"
Info = "text"
Import = "package. class/package .*,...."
Extends = "package. class"
ErrorPage = "relativeURL"
ContentType = "TYPE, charset = CHARSET"
Buffer = "none/8kb/sizekb"
AutoFlush = "true/false"
%>
<% @ Page
Language = "java"
Session = "true/false"
PageEncoding = "characterSet"
IsThreadSafe = "true/false"
IsErrorPage = "true/false"
Info = "text"
Import = "package. class/package .*,...."
Extends = "package. class"
ErrorPage = "relativeURL"
ContentType = "TYPE, charset = CHARSET"
Buffer = "none/8kb/sizekb"
AutoFlush = "true/false"
%>
The following table describes these attributes:
Attribute name
Description
Example
Language
Specifies the scripting language used for JSP pages. Currently, only Java
<% @ Page langage = "java" %>
Session
Whether to use session in JSP
<% @ Page session = "true" %>
PageEncoding
Encoding Character Set of the JSP page
<% @ Page pageEncoding = "gb2312" %>
IsErrorPage
Whether the JSP file displays an error page
<% @ Page isErrorPage = "true" %>
IsThreadSafe
Specifies whether JSP files can use multithreading.
<% @ Page isThreadSafe = "true" %>
Info
Set JSP page information
<% @ Page info = "string" %>
Import
Specifies the imported Java package
<% @ Page import = "java. util" %>
ErrorPage
Page called when an exception occurs
<% @ Page errorPage = "erroe = r. jsp" %>
ContentType
Specifies the file format of the JSP page and the Content Encoding Character Set sent by the server to the client.
<% @ Page contentType = "text/html; charset = GBK" %>
Buffer
Specify the buffer size of the JSP webpage
<% @ Page buffer = "32kb" %>
AutoFlush
If the value is true, the data is output to the client.
If the value is false, an exception occurs.
<% @ Page autoFlush = "true" %>
After understanding these attributes of the page command, the following code uses the page command to deepen understanding:
[Html]
<% @ Page
ContentType = "text/html; charset = GBK"
PageEncoding = "UTF-8"
Import = "java. util .*"
Session = "true"
Buffer = "32kb"
%>
<Html>
<Head>
<Title> JSP page instructions </title>
</Head>
<Body>
<H3> This is a webpage with multiple page commands </Body>
</Html>
<% @ Page
ContentType = "text/html; charset = GBK"
PageEncoding = "UTF-8"
Import = "java. util .*"
Session = "true"
Buffer = "32kb"
%>
<Html>
<Head>
<Title> JSP page instructions </title>
</Head>
<Body>
<H3> This is a webpage with multiple page commands </Body>
</Html>
2) include commands
Used to insert files to JSP web pages. These files can be text files, HTML files, or JSP files. The syntax of this command is as follows. file refers to the file path to be loaded.
<% @ Include file = "relative file path" %>
The following program explains how to use the include command:
[Html]
<% @ Pagelanguage = "java" import = "java. util. *" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %>
<% @ Includefile = "test.html" %>
<Html>
<Head>
<Title> test include </title>
</Head>
<Body>
<H3> This is a page for testing the include command </Body>
</Html>
<% @ Pagelanguage = "java" import = "java. util. *" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %>
<% @ Includefile = "test.html" %>
<Html>
<Head>
<Title> test include </title>
</Head>
<Body>
<H3> This is a page for testing the include command </Body>
</Html>
Generally, you need to write frequently used code in some files. If there is no include command, you must write the same code in each file for a long time, this will result in a waste of time and low efficiency. With the include command, you can directly load it to the target page, which greatly saves time.