当前位置:网站首页>JSP basic knowledge summary
JSP basic knowledge summary
2022-04-23 01:38:00 【Love you, Bai~】
JSP Learning notes
1. brief introduction
JSP:Java Server Page, yes Java Web Server side dynamic resources .
2. Related configuration
file —> Set up —>( Search for ) code —> Such as UTF-8
3. notes
display comments : On the client side ( Web source code ) View comments for
Implicit annotation : Not on the client ( Web source code ) View comments for
stay jsp Your own notes in <%----%>
The shortcut key is also
Ctrl+/
4.Scriptlet
- Defining local variables — Generated code in servlet Medium service In the method body of
- Declare global variables — Generated code in servlet In the method body of
- Output expression , You can output variables or literal values — Generated code in servlet In the class body of
<%-- The first one is :Java Script segment , Can write Java Code for , Defining local variables , Write statements, etc --%>
<%// Defining local variables
String str= "Hello jsp";
// Output content to console
System.out.println(str);
// Output content to browser
out.print(str);
// Output global variables
out.write(" Global variables "+num);
%>
<%-- The second kind : Statement , Declare global variables , Method , Class etc. --%>
<%!
int num =10;
%>
<%-- The third kind of : Output expression , You can output variables or literal values --%>
<%=str%>
5. A small problem —jsp in out.println Report errors
- Should be Tomcat Related to File->Project Structure->modules->Dependencies
Pictured
choice library Then click OK
choice tomcat Then click add selected Click OK again
6.include Static contains
Before saying include , First to speak
1.JSP Instruction label
2.include Static inclusion
Format :<%@include file=" The page address to include "%>
eg:
Master file
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title> Static inclusion </title>
</head>
<body>
<%@include file="04-head.jsp"%>
<h2> Main part </h2>
<%@include file="04-foot.jsp"%>
</body>
</html>
04-head.jsp file
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title> Head </title>
</head>
<body>
<h2> Head content </h2>
</body>
</html>
04-foot.jsp file
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title> The tail </title>
</head>
<body>
<h2> Tail content </h2>
</body>
</html>
The final result :
characteristic :
- Replace the content directly
- Static inclusion will only generate a source file , The end is all in ispservlet In method body ( In the source code file )
- Variables with the same name cannot appear ( Because in the end, the documents are spliced together )
- A little bit more efficient , The coupling degree is high , inflexible
3.include Dynamic inclusion
Format : <jsp:include page=" To include the page path "></jsp:include>
Master file :
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title> Dynamic inclusion </title>
</head>
<body>
<jsp:include page="04-head.jsp"></jsp:include>
<h2> Subject matter </h2>
<jsp:include page="04-foot.jsp"></jsp:include>
</body>
</html>
04-head.jsp file
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title> Head </title>
</head>
<body>
<h2> Head content </h2>
</body>
</html>
04-foot.jsp file
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title> The tail </title>
</head>
<body>
<h2> Tail content </h2>
</body>
</html>
The final result :
characteristic :
- Dynamic inclusion is equivalent to a call in a method
- Dynamic inclusion will generate multiple source files
- You can define variables with the same name
- Efficient , Low coupling
Be careful : When dynamic inclusion does not need to pass parameters ,include There should be nothing between the two labels , Including line breaks and spaces
eg:
stay head Add the space bar after the file
Running effect :
reason :
Because when you enter spaces or special symbols , At this time, the input content will be regarded as parameters
Use dynamic inclusion to pass parameters
<%-- Dynamic inclusion
Format :
<jsp:include page=" To include the page path "></jsp:include>
<%-- Dynamically include pass parameters
<jsp:include page=" Page path to include ">
<jsp:param name=" Parameter name " value=" Parameter values "/>
</jsp:include>
notes :name Property does not support expression ,value Support expression
To obtain parameters :
requset.getParameter(name); Get the parameter value through the specified parameter name
--%>
The main function :
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title> Dynamic inclusion </title>
</head>
<body>
<jsp:include page="04-head.jsp"></jsp:include>
<h2> Subject matter </h2>
<%
int a = 1;
%>
<jsp:include page="04-foot.jsp"></jsp:include>
<%
String str = "HEELO";
%>
<jsp:include page="04-foot.jsp">
<jsp:param name="uname" value="admin"/>
<jsp:param name="msg" value="<%=str%>"/>
</jsp:include>
</body>
</html>
04-foot.jsp file
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title> The tail </title>
</head>
<body>
<h2> Tail content </h2>
<%
int a = 10;
// Get the parameters passed dynamically
String uname = request.getParameter("uname");
String msg =request.getParameter("msg");
out.print(uname+","+msg);
%>
</body>
</html>
04-head.jsp file
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title> Head </title>
</head>
<body>
<h2> Head content </h2>
</body>
</html>
7.JSP Four domain objects of
The scope of attribute validation
JSP Four domain objects of
page Scope
Jump valid on the current page , Invalid after jump
request Scope
Valid in a request , Server side jump is valid , Invalid client jump
session Scope
Effective in a conversation , Server and client jumps are valid
qpplication Scope
The whole application works
JSP Server jump
1. Server jump
<jsp:forward page=" Jump to the page address "></jsp:forward>
2. Server jump
Hyperlinks <a href=" Jump to the page address "> Jump </a>
( Hyperlink jump ) Master file :
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>JSP Four domain objects of </title>
</head>
<body>
<%
// Set up page Domain object of scope
pageContext.setAttribute("name1"," Zhang San ");
// Set up request object
request.setAttribute("name2"," Li Si ");
// Set up session Scope object
session.setAttribute("name3"," Wang Wu ");
// Set up application Domain object of scope
application.setAttribute("name4"," Zhao Liu ");
%>
<a href="06-JSP Four domain objects of -02.jsp"> Jump </a>
</body>
</html>
06-JSP Four domain objects of -02.jsp file
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>JSP Four domain objects of </title>
</head>
<body>
// Get the value in the object
out.print("page Range :"+pageContext.getAttribute("name1")+"<br>");
out.print("page Range :"+request.getAttribute("name2")+"<br>");
out.print("page Range :"+session.getAttribute("name3")+"<br>");
out.print("page Range :"+application.getAttribute("name4")+"<br>");
%>
</body>
</html>
Running results :
( Server jump ) Master file :
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>JSP Four domain objects of </title>
</head>
<body>
<%
// Set up page Domain object of scope
pageContext.setAttribute("name1"," Zhang San ");
// Set up request object
request.setAttribute("name2"," Li Si ");
// Set up session Scope object
session.setAttribute("name3"," Wang Wu ");
// Set up application Domain object of scope
application.setAttribute("name4"," Zhao Liu ");
%>
<jsp:forward page="06-JSP Four domain objects of -02.jsp"></jsp:forward>
</body>
</html>
06-JSP Four domain objects of -02.jsp file
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>JSP Four domain objects of </title>
</head>
<body>
// Get the value in the object
out.print("page Range :"+pageContext.getAttribute("name1")+"<br>");
out.print("page Range :"+request.getAttribute("name2")+"<br>");
out.print("page Range :"+session.getAttribute("name3")+"<br>");
out.print("page Range :"+application.getAttribute("name4")+"<br>");
%>
</body>
</html>
Running results :
8. Simple user login function
login.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title> The user login </title>
</head>
<body>
<form action="loginServlet" method="post">
full name : <input type="text" name="uname"> <br>
password : <input type="password" name="upwd"> <br>
<button> Sign in </button> <span style="color: red;font-size: 12px"><%=request.getAttribute("msg")%></span>
</form>
</body>
</html>
index.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<h2> welcome <%=session.getAttribute("uname")%> Sign in </h2>
</body>
</html>
LoginServlet.java
@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// Set the encoding format of the client ( Prevent confusion code )
req.setCharacterEncoding("UTF-8");
// Accept the parameters passed by the client
String uname = req.getParameter("uname");
String upwd = req.getParameter("upwd");
// Judge whether the parameter is empty
if (uname == null ||"".equals(uname.trim())) {
// Prompt user information
req.setAttribute("msg", " User name cannot be empty ");
// Request forwarding to jump to login.jsp
req.getRequestDispatcher("login.jsp").forward(req, resp);
return;
}
if (upwd == null||"".equals(upwd.trim())) {
// Prompt user information
req.setAttribute("msg", " User password cannot be empty ");
// Request forwarding to jump to login.jsp
req.getRequestDispatcher("login.jsp").forward(req, resp);
return;
}
// Determine whether the account password is correct
if (!"admin".equals(uname)||!"admin".equals(upwd)) {
// Prompt user information
req.setAttribute("msg", " Login failed ");
// Request forwarding to jump to login.jsp
req.getRequestDispatcher("login.jsp").forward(req, resp);
return;
}
// Login successful
// Set login information to session
req.getSession().setAttribute("uname",uname);
// Jump to index.jsp
resp.sendRedirect("index.jsp");
}
}
9.EL expression
eg:
<span style="color: red;font-size: 12px"><%=request.getAttribute("msg")%></span>
adopt EL expression , You can put what is displayed on the page in a simple user function null Change to an empty string
<span style="color: red;font-size: 12px">${msg}</span>
1. The syntax of the expression 
When getting the specified domain object
${ The name of the object . Data name }
2. Use of expressions - get data
1. obtain list
obtain list
obtain list Of size ${list.size()}
obtain list The specified subscript :${list[ Subscript ]}
notes :list Represents the domain limited variable name
2. obtain Map
obtain Map
obtain Map Specified in the key Of value
${map.key}
perhaps
${map["key"]}
notes :Map Represents the domain limited variable name
eg:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title> Use of expressions </title>
</head>
<body>
<%
//list
List<String> list = new ArrayList<>();
list.add("aaa");
list.add("bbb");
list.add("ccc");
request.setAttribute("list",list);
//Map
Map map = new HashMap();
map.put("aaa","111");
map.put("bbb","222");
map.put("ccc","333");
request.setAttribute("map",map);
%>
<h4> obtain list</h4>
obtain list Of size:${
list.size()}<br>
obtain list The specified subscript :${
list[1]}<br>
<h4> obtain Map</h4>
obtain list Of size:${
map.aaa}<br>
obtain list The specified subscript :${
map["bbb"]}<br>
</body>
</html>
3.JavaBean
4.empty
1. character string
The domain object is a string
Nonexistent domain object :ture
An empty string :ture
null:ture
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title> Use of expressions </title>
</head>
<body>
<%
request.setAttribute("str1","abc");
request.setAttribute("str2","");
request.setAttribute("str3",null);
%>
${empty str}<br>
${empty str1}<br>
${empty str2}<br>
${empty str3}<br>
</body>
</html>
2.List
No length —true
null-true
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %><%--
Created by IntelliJ IDEA. User: lenovo
Date: 2022/1/28
Time: 1:36
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title> Use of expressions </title>
</head>
<body>
<%
List list1 =new ArrayList<>();
List list2 = null;
List list3 = new ArrayList<>();
list3.add(1);
request.setAttribute("list1",list1);
request.setAttribute("list2",list2);
request.setAttribute("list3",list3);
%>
${
empty list1}<br>
${
empty list2}<br>
${
empty list3}<br>
</body>
</html>
3.Map
null:true
empty map object :true
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %><%--
Created by IntelliJ IDEA.
User: lenovo
Date: 2022/1/28
Time: 1:36
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title> Use of expressions </title>
</head>
<body>
<%
Map map1 = null;
Map map2 = new HashMap<>();
Map map3 = new HashMap<>();
map3.put(1,2);
request.setAttribute("map1",map1);
request.setAttribute("map2",map2);
request.setAttribute("map3",map3);
%>
${empty map1}<br>
${empty map2}<br>
${empty map3}<br>
</body>
</html>
4.JavaBean
null:true
An empty object :false
3.EL operation
JSTL Study
1. Use of labels
JSTL Use
1. download JSTL The required jar package
(standard.jar and jstl.jar)
2. In the project web In the catalog WEB-INF New China lib Catalog , take jar Copy it in
3. choice “ file ”————> Project structure ————> modular ————> rely on ————>“+”————> The corresponding lib Import the directory
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title>11</title>
<%-- JSTL Use
1. download JSTL The required jar package
(standard.jar and jstl.jar)
2. In the project web In the catalog WEB-INF New China lib Catalog , take jar Copy it in
3. choice “ file ”————> Project structure ————> modular ————> rely on ————>“+”————> The corresponding lib Import the directory
--%>
</head>
<body>
<c:if test="${1==1}">
Hello JSTL
</c:if>
</body>
</html>
2.if label
Format :
<c:if test="${num > 100}" var="String" scope="String">
...
</c:if>
notes :
- Label operations are generally domain objects
- if The label doesn't have else, If you want to else The effect of , You need to set two completely opposite conditions
3. Condition label
choose,when and otherwise label
Grammar format
attribute
- choose Tags have no properties
- when There is only one tag test attribute , Must attribute
- otherwise The label must be set in the last when After tag
Be careful :
- choose Labels and otherwise Tags have no properties , and when The label must have a test attribute
- choose The tag must contain at least one when label , There can be no otherwise label
- otherwise The label must be set at the last when After tag
4. Laminated label
Grammar format
attribute
eg:
( Traverse the subject content multiple times )
as follows
( loop )
as follows
版权声明
本文为[Love you, Bai~]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230137132882.html
边栏推荐
- 轮转法分片
- Rotation slice
- Unity结合iTextSharp生成PDF 准备dll
- 修改数组(并查集)
- Android sqliteopenhelper data table structure upgrade
- Solve the problem when installing MySQL
- Custom numeric input control
- Let's talk about passive safety again. I'll teach you to understand the rating report of China Insurance Research Institute collision test
- In the context of Internet plus, how can enterprises innovate customer service?
- VSCODE + PHP Debug + 名字空间指引
猜你喜欢
In the second half of the smart watch, opportunities and challenges coexist
Activity preview | on April 23, a number of wonderful openmldb sharing came, which lived up to the good time of the weekend!
NR polar code VII - SCL (successful cancellation list coding)
42、使用mmrotate中k3det进行旋转目标检测,并进行mnn部署和ncnn部署
Redis implements distributed locks
Practice and exploration of knowledge map visualization technology in meituan
Unrelated interprocess communication -- creation and use of named pipes
Chris LATTNER, father of llvm: the golden age of compilers
什么时候应该编写单元测试?什么是TDD?
Slow response of analysis API
随机推荐
GBase 8t索引
Deployment of mask detection flash for yolov5
JSP基础知识总结
Jerry's CPU performance test [chapter]
Full Permutation (DFS and next_permutation solution)
Jerry's AI server [chapter]
“自虐神器”一夜爆火:用手柄控制自己的脸,代码自取,后果自负
iTextSharp 基础结构
Slow response of analysis API
Android sqliteopenhelper data table structure upgrade
W801/W800/W806唯一ID/CPUID/FLASHID
In the context of Internet plus, how can enterprises innovate customer service?
世界读书日:18本豆瓣评分9.0以上的IT书值得收藏
How to introduce SPI into a project
[course summary] Hello harmonyos series of courses, take you to zero foundation introduction
mb_substr()、mb_strpos()函数(故事篇)
.NET单元测试第一篇:常见.NET单元测试框架有哪些?
Gbase 8s数据库日志简介及管理
engine. Post() handles post requests
Introduction and management of gbase 8s database log