JSP(1)

1. What is JSP?
Java Server Pages[jsp]—java server page
[ pages that contain java programs running on the server{HTML}]
page—[HTML]—including java program codes—run on the server.
E.g:

<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.Date"%>
<%@ 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>Insert title here</title>
</head>
<body>
	<%
	  SimpleDateFormat  sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss E");
	  String datetime=sdf.format(new Date());
	%>
	<h1>当前系统时间:<% out.write(datetime); %></h1>
</body>
</html>

Insert picture description here

JSP is actually html containing java code. Use html tags to represent static effects, and the included java code handles dynamic data.

2. What are the elements in the JSP page?
Insert picture description here

2.1 Instruction
Format: <%@instruction name attribute 1=”attribute value 1"...%>
Function: some additional information that is declared in the jsp page to be executed by the browser.
Common instructions: page instruction/include instruction/taglib instruction
1. page instruction—common attributes
1.language="java"
—specify language 2.contentType="text/html; charset=utf-8": set the content type of the current jsp page
3.pageEncoding="utf-8": set the current Character encoding of jsp page
4.import="java.util.Date": import the dependency package of java class library [multiple can appear]
5.isELIgnored="false"—whether to enable EL expression language "${expression} ", false is available, true is not
possible 6.isErrorPage="false"—whether it is an error page, if it is, then you can use exception built-in JSP objects.
For example:

<%@ page language="java" contentType="text/html; charset=utf-8" 
	pageEncoding="utf-8" isELIgnored="false" isErrorPage="true"%>
<!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>Insert title here</title>
</head>
<body>
	<h1>page指令--常见属性</h1>
	<h2>1.language="java"---指定语言</h2>
	<h2>2.contentType="text/html; charset=utf-8":设置当前jsp页面的内容类型</h2>
	<h2>3.pageEncoding="utf-8":设置当前jsp页面的字符编码</h2>
	<h2>4.import="java.util.Date":导入java类库的依赖包【可以出现多个】</h2>
	<h3>&lt;%@ page  import="java.text.SimpleDateFormat"<br> 
    import="java.util.Date"%&gt;</h3>
    <hr>
    <h3>&lt;%@ page  import="java.text.SimpleDateFormat"%&gt;</h3> 
    <h3>&lt;%@ page import="java.util.Date"%&gt;</h3>
    <h2>5.isELIgnored="false"---是否启用EL表达式语言 “${表达式}”,false可用,true不可以</h2>
    <h3>123+234=${123+234}</h3>
    <h2>6.isErrorPage="false"---是否是一个错误页面,如果是那么就可以使用exception的JSP内置对象</h2>
    <%
    	exception.printStackTrace();
    %>
</body>
</html>

Insert picture description here

2. Include instruction [include other resources to enter this jsp page]-common attributes
such as:

<%@ page language="java" contentType="text/html; charset=utf-8" 
	pageEncoding="utf-8" isELIgnored="false" isErrorPage="true"%>
<!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>Insert title here</title>
</head>
<body>
	<h1>include指令--常见属性</h1>
	<h2>include指令--包含其他的资源进入本jsp页面</h2>
	<h2>file="test.txt":指定被包含的其他的资源的路径</h2>
	包含文本信息:<%@include file="test.txt" %><br>
	包含html信息:<%@include file="mytest.html" %>
	包含jsp信息:<%@include file="test1.jsp" %>
</body>
</html>

Insert picture description here

3.taglib instruction-when using JSTL [jsp standard tag library] tags in the jsp page, it is used to import the tag library
[this instruction will not be processed temporarily, we will introduce it in detail when we learn JSTL later]
2.2 Java code fragment
format :<% java program%>
Function: java program nested in html [provide dynamic data]
For example:

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8" 
    import="java.text.SimpleDateFormat" 
    import="java.util.Date"%>
<!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>Insert title here</title>
</head>
<body>
	<%
	  SimpleDateFormat  sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss E");
	  String datetime=sdf.format(new Date());
	%>
	<h1>当前系统时间:<% out.write(datetime); %></h1>
</body>
</html>

Insert picture description here

2.3jsp expression
Format: <%= expression/variable/method%>
Function: Calculate the result of a certain expression, or the result of a variable/method.
2.4jsp declaration
format: <%! class/variable/method%>
Function: define some java variables/methods/classes you need in the jsp page
For example:

<%@ page language="java" contentType="text/html; charset=utf-8" 
	pageEncoding="utf-8" isELIgnored="false" isErrorPage="true"%>
<!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>Insert title here</title>
</head>
<body>
	<h1>jsp表达式</h1>
	<h2>计算某种表达式的结果,或者变量/方法的结果。</h2>
	<h2>算术表达式 123+234=<%= 123+234 %></h2>
	<h2>关系表达式 123>234=<%= 123>234 %></h2>
	<h2>逻辑表达式 (123>234) || (123 < 234)===<%= (123 > 234) || (123 < 234) %></h2>
	<h1>jsp声明</h1>
	<h2>在jsp页面中定义一些自己需要的java变量/方法/类</h2>
	<%!
	   String  name="zhangsan";
	   public  String getInfo(){
		   return "hello,wangxing";
	   }
	%>
	<h2>声明中的变量name==<%= name %></h2>
	<h2>声明中的变量getInfo()==<%= getInfo() %></h2>
</body>
</html>

2.5jsp action
Format: <jsp: action name action attribute...></jsp: action name>
Function: encapsulate some commonly used java programs to facilitate the call
of included jsp actions

<%@ page language="java" contentType="text/html; charset=utf-8" 
	pageEncoding="utf-8" isELIgnored="false" isErrorPage="true"%>
<!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>Insert title here</title>
</head>
<body>
	<h1>jsp动作</h1>
	<h2>将一些常用的java程序封装,方便调用</h2>
	<h2>include的jsp动作--包含其他的资源进入本jsp页面</h2>
	包含文本信息:<jsp:include page="test.txt"></jsp:include><br>
	包含html信息:<jsp:include page="mytest.html"></jsp:include>
	包含jsp信息:<jsp:include page="test1.jsp"></jsp:include>
</body>
</html>

forward jsp action

<%@ page language="java" contentType="text/html; charset=utf-8" 
	pageEncoding="utf-8" isELIgnored="false" isErrorPage="true"%>
<!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>Insert title here</title>
</head>
<body>
	<h1>jsp动作</h1>
	<h2>forward的jsp动作--跳转到其他的资源</h2>
	<jsp:forward page="mytest.html"></jsp:forward>
</body>
</html>

What is the difference between include directive [<%@include file=”” %>] and include action [<jsp:include page=""></jsp:include>] ?
Insert picture description here

3. The execution process of the JSP page?
1. JSP is essentially a Servlet program.
Jsp will be converted into a Servlet program when it is executed.
Save location
F:\20200728\javawebworkspace.metadata.plugins\org.eclipse.wst.server.core\tmp0\work\Catalina\localhost\TestJSP1\org\apache\jsp

public final class test5_jsp extends org.apache.jasper.runtime.HttpJspBase
    implements org.apache.jasper.runtime.JspSourceDependent,
                 org.apache.jasper.runtime.JspSourceImports {
org.apache.jasper.runtime.HttpJspBase继承过javax.servlet.http.HttpServlet
 

Insert picture description here

When we request a jsp page, after the server receives the request of the jsp page, it will first convert the accessed jsp file into a servlet file, continue to compile the converted servlet file to get the bytecode file corresponding to the servlet file, and then execute This compiled bytecode file encapsulates the running result into the response object and returns it to the client browser.
Disadvantages: The first visit to the jsp page will be slower.
4. The difference between Servlet and JSP?
Insert picture description here

5. What are the built-in objects in JSP, what are their types, what are their functions, and what are the commonly used methods?
The built-in object in JSP is the pre-created object when the server is running, we don't need to create [new] by ourselves. JSP is a Servlet program.
There are 9 built-in objects in JSP
.
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/guoguo0717/article/details/109227772
jsp