Java programming basic notes • [Chapter 2 JSP foundation]

All chapters >>>>


Contents of this chapter

2.1 Introduction to JSP

2.1.1 Overview of JSP

2.1.2 Develop the first JSP page

2.1.3 JSP processing flow

2.1.4 Practice exercises

2.2 JSP instruction identification

2.2.1 JSP instructions

2.2.2 Instruction mark

2.2.3 include directive

2.2.4 taglib instruction

2.2.5 Practice exercises

2.3 JSP script identification

2.3.1 Declaration mark

2.3.2 JSP expression

2.3.3 Scriptlet script program

2.3.4 Practice exercises

2.4 Comment mark

2.4.1 JSP comments

2.4.2 Comprehensive case

2.4.3 Practice exercises

to sum up:


2.1 Introduction to JSP

2.1.1 Overview of JSP

Java Server Page is abbreviated as JSP, which is a dynamic web page technology standard advocated and established by Sun, used to develop dynamic web pages

JSP is to embed the traditional Java code into the Html page code, which is compiled and executed by the web server, and the final static Html is generated and returned to the client

JSP advantages:

  • Cross-platform
  • Safety
  • Scalability
  • Strong flexibility
  • Reusability

2.1.2 Develop the first JSP page

JSP is a dynamic web page. The page contains not only HTML code but also Java code. You can use Eclipse to create Jsp pages

  • Create a new Jsp File under the Eclipse Javaweb project WebContent
  • Input file name

Example:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/
html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>First JSP page</title>
</head>
<body>
<h1> 这是第一个 JSP 页面 </h1>
</body>
</html>

Jsp page header instruction identification:

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

Deploy the project in the Tomcat server, start the server, test access to the JSP page

Note: Pay attention to the console output when starting the server to ensure that no exception occurs

2.1.3 JSP processing flow

The browser cannot run the JSP file directly. Only by deploying the Web project containing the JSP file to the Web server can you see the JSP display effect

When the client browser sends a request to the server to access a JSP page, the server loads the corresponding JSP page according to the request, and converts, compiles and executes the page

2.1.4 Practice exercises

 

2.2 JSP instruction identification

2.2.1 JSP instructions

A JSP page is a regular Web page with JSP elements, which is composed of static content and dynamic content

The dynamic content includes the following:

  • Instruction identification
  • Script ID
  • Comment ID

2.2.2 Instruction mark

The instruction mark is mainly used to set the relevant setting information that is valid in the entire JSP page. It is interpreted and executed by the server, and no content is output to the web page.

<%@ 指令名 属性 1=“属性值 1” 属性 2=“属性值 2”...%>

(1) Instruction name: It is used to specify the instruction name, including three instructions page, include and taglib in JSP.

(2) Attributes: used to specify the attribute name, different commands contain different attributes. In one command, multiple attributes can be set, separated by commas or spaces.

(3) Attribute value: user-specified attribute value.

Example:

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

language attribute. This attribute is used to set the language used by the JSP page. The default value of this attribute is Java

import attribute. This attribute is used to set the class package imported by JSP. Import the code snippet of the class package in the JSP page, such as <%@page import=“java.util.*”%>

pageEncoding attribute. Used to set the encoding format of the JSP page, that is, specify the page encoding, and support Chinese generally set to GBK or UTF-8

contentType attribute. This attribute is used to set the MIME type and character encoding of the JSP page. The browser will display the content of the JSP output according to the MIME type and character set encoding specified in contentType. Common MIME formats are as follows

MIME

effect

application/msword

Word document

application/pdf 

PDF document

audio/x-wav

Audio file in Wav format

text/html 

HTML format

text/plain

Normal document

image/jpeg 

JPEG format

2.2.3 include directive

The include directive can include another page in a JSP page. It is often used to extract common parts of multiple pages (such as top navigation, bottom copyright, etc.), and directly quote when used to improve development and maintenance efficiency

Format: <%@include file="include file path"%>

Example: Include the navigation bar at the head of the website and the copyright information bar at the bottom of the website in the main page

top.jsp design top navigation and other information

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

copyright.jsp design bottom copyright information

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

Index.jsp home page contains top.jsp and copyright.jsp

<body>

	<%@include file="nav.jsp"%>
        首页自身代码省略……
    <%@ include file="copyright.jsp"%>

</body>

Note: When using the include directive to include files, in order to prevent the hierarchical results of the entire page from conflicting, the <html> element and <body> element in the included page should be deleted, because these are already specified in the file containing the page mark

2.2.4 taglib instruction

In the JSP file, you can declare the tag library used in the page through the taglib directive, and at the same time refer to the tag library, and specify the tag prefix. After the tag library is referenced in the page, the tags in the tag library can be referenced by the prefix

<%@taglib prefix=“c” uri=“http://java.sun.com/jsp/jstl/core” %>

2.2.5 Practice exercises

 

2.3 JSP script identification

2.3.1 Declaration mark

The declaration identifier is used to define global variables or methods in the JSP page, and the identifier is usually used to define the variables or methods that the entire JSP page needs to reference

grammar:

<%! 声明变量或方法代码 %>

note:

There can be no space between <% and !, but there can be a space between! And the following code.

<%! and %> may not be on the same line. The following format is also correct:

<%!  

    声明变量或方法的代码 
%>

Example: Declare a flag to declare a global variable and global method to count the number of page visits.

<%!
	int number = 0;// 声明全局变量
	int count() {
		number++;//number 自增
		return number;
	}%>
<p class="title1">
<span class="fl"> 这是第 <%=count()%> 次访问该页面
</span>
</p>

2.3.2 JSP expression

Example: Declare a flag to declare a global variable and global method to count the number of page visits.

<ul class="dian">
<%
String paint01 = " 夏塘清趣 ";//脚本用于声明变量
String paint02 = " 事茗图 ";
String paint03 = " 渔父图 ";
 %>
<li><a href="#"><%=paint01%></a></li>//表达式用于输出结果到网页上
<li><a href="#"><%=paint02%></a></li>
<li><a href="#"><%=paint03%></a></li>
</ul>

2.3.3 Scriptlet script program

Scriptlet (code fragment) script program is Java code or script code embedded in JSP pages

  • Code snippets will be executed during the processing of the page request, variables or flow control statements can be defined through Java code

grammar:

<% 表达式或者变量名 %>
  • Note that there is no sign after <%
  • The code in the script must conform to the java syntax specification

Example: Use JSP script to define variable information

<ul class="dian">
<%
String paint01 = " 夏塘清趣 ";//脚本用于声明变量
String paint02 = " 事茗图 ";
String paint03 = " 渔父图 ";
 %>
<li><a href="#"><%=paint01%></a></li>//表达式用于输出结果到网页上
<li><a href="#"><%=paint02%></a></li>
<li><a href="#"><%=paint03%></a></li>
</ul>

Description:

  • By declaring the variables and methods created by the identifier, the life cycle is from the beginning of creation to the end of the server shutdown; and the life cycle of the variables and codes created by the script fragments will be destroyed after the page is closed
  • Scripts are generally used to declare variables, calculations, logic judgments and loop control, while expressions are only used to output content

2.3.4 Practice exercises

 

2.4 Comment mark

2.4.1 JSP comments

Comments are used to explain the functionality of certain codes, thereby increasing the readability of the program. JSP program can contain 3 different types of comments

1. JSP standard notes

<%-- JSP标准注释 --%>
JSP标准注释在客户端浏览器无法查看到

2. Java comments

<%
       //Java单行注释
       /*
           Java多行注释
       */
%>
Java注释在客户端浏览器中也无法看到

3. HTML comments

<!-- HTML网页注释 -->
HTML网页注释在客户端浏览器可以看到

2.4.2 Comprehensive case

Example: Make news display page, use comments to complete code description:

<ul class="jiantou">
<%
        //调用声明的方法,得到数据,准备循环
        List<String> paintList = getList();
        for (int i = 0; i < paintList.size(); i++) {
%>
        <li><a href="#"><%=paintList.get(i)%></a></li>
<%
}
%>
</ul>

News display page production, running result graph

2.4.3 Practice exercises

 

to sum up:

  • Java Server Page, abbreviated as JSP, is a dynamic web page technology that embeds Java code in Html, which has the advantages of cross-platform, security, and strong reusability.
  • When the client accesses the Jsp page in the Web server through the browser, the server will convert, compile and execute the Jsp, and finally return to the static Html code page generated by the client
  • When embedding Java code in Jsp, you can use declarations, scripts, and expressions in several different ways
  • The comments in the Jsp page include Jsp standard comments, Java comments and Html comments.

 

Guess you like

Origin blog.csdn.net/weixin_44893902/article/details/114003238