什么是JSP?
全称 Java Server Page 中文名 JAVA服务器页面
由Sun Microsystems公司倡导 许多公司参与一起建立的一种动态网页技术标准
JSP 原理
访问jsp页面时 jsp页面会被翻译成.java文件
然后 .java会被编译成 .class文件(字节码文件)
在网页中显示当前时间
<%@ 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>
<%
// 正常写 Java 代码
Date date = new Date();
out.print(date.toLocaleString());
// 被废弃的方法,使用仅为测试
%>
</body>
</html>
实现登录页面
<%@ 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>
<form action="/Demo/dologin.jsp" method="post">
账号:<input type="text" name="username">
<br>
密码:<input type="password" name="password">
<br>
<input type="submit" value="登录">
<br>
<%
String msg = (String)request.getAttribute("msg");
if(msg != null){
out.print(msg);
}
%>
</form>
</body>
</html>
<%@ 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>
<%
// 获取参数
String username = request.getParameter("username");
String password = request.getParameter("password");
// 处理逻辑
if(username.equals("test") && password.equals("123")){
// 登录成功
// 跳转页面 -> 欢迎页面,并且显示名字
// 传值时,如果是非表单提交的数据,可以保存在域对象中
// 如果是表单提交过来的数据,直接使用请求转发就行了
request.getRequestDispatcher("/success.jsp").forward(request, response);
} else {
// 登录失败 返回登录页面
// 相对于服务器内,不用添加工程名,例如:请求转发
// 相对于服务器外(相对于网址的8080后),需要添加工程名,例如:请求重定向
// 失败后,在页面中显示错误信息
// 使用请求转发,比较合适
request.setAttribute("msg", "登录失败,请确认用户名和密码!");
request.getRequestDispatcher("/login.jsp").forward(request, response);
}
// 跳转页面
%>
</body>
</html>
<%@ 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>
<%
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
%>
<h1>欢迎你
<%
// String username = (String)session.getAttribute("username");
String username = request.getParameter("username");
out.write(username);
%>
</h1>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!-- 默认是true,相当与一个开关 -->
<!-- false时,.Java文件编译时没有session这个对象 -->
<%@ page session="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>
<%-- jsp注释:安全效率高 --%>
<!-- html注释:不安全,效率低 -->
<!-- 测试声明 -->
<%!
// 加上叹号会被翻译到类的下面,即为全局变量
// 还可以声明方法,静态块
// 可以这么用,但是一般没人这么去写!
int num2 = 10;
%>
<%
// 测试session开关
session.getAttribute("123123");
int num1 = 10;
num1++;
num2++;
// out.print(num1);
%>
<!-- 相当于 out.print(num1); -->
<!-- 表达式 -->
<%=num1+2 %>
<%=num2 %>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page errorPage="/error.jsp" %>
<!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>
<%
int num = 10 / 0;
%>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!-- 默认是关闭状态的,打开捕获异常信息的对象的创建 Throwable -->
<%@ page 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>
<%
String msg = exception.getMessage();
out.print(msg);
%>
服务器正在紧张的建设当中...请稍后重试
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!-- include指令 file指包含哪个页面
静态包含:在翻译成.Java文件前就已经合成了一个页面
动态包含:
代码逐行执行,当执行到动态包含时
才会去编译被包含的页面,这样会生成两套文件 -->
<%--@ include file="/4.jsp" --%>
<!-- 动态包含 -->
<%-- <jsp:include page="/4.jsp"></jsp:include> --%>
<!-- taglib标签,导入jstl核心库
prefix="c" 表示给jstl标签库中的标签,起一个别名 -->
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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>
<c:if test=""></c:if>
bbbbbb
</body>
</html>
<!-- https://mvnrepository.com/ -->
<%@ 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>
aaaaaaaa
</body>
</html>
<%@ page import="Demo.User"%>
<%@ 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>
<%
// 创建一个User对象
User user = new User();
user.setName("wanglong");
user.setPwd("123");
out.print(user.getName());
%>
<br>
<jsp:useBean id="user1" class="Demo.User"></jsp:useBean>
<%-- <jsp:useBean id="user1" class="Demo.User"/> --%>
<jsp:setProperty property="name" name="user1" value="dingpeng" />
<jsp:setProperty property="pwd" name="user1" value="456" />
<%=user1 %>
<br>
<jsp:getProperty property="name" name="user1"/>
<jsp:getProperty property="pwd" name="user1"/>
</body>
</html>
public class User {
private String name;
private String pwd;
public User() {
super();
}
public User(String name, String pwd) {
super();
this.name = name;
this.pwd = pwd;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "User [name=" + name + ", pwd=" + pwd + "]";
}
}
pageContext 域
<%@ 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>转发</title>
</head>
<body>
<!-- 测试 pageContext 域 -->
<%
/* 该域对象,只在本页面有效,出了页面就获取不到 */
pageContext.setAttribute("page", "pageContext");
%>
<%=pageContext.getAttribute("page") %>
<!-- jsp 动作标签--转发
携带参数相当于在网址后面进行拼接
/2.jsp?username=test&password=123 -->
<%-- 携带请求的参数(不要在这中间加注释!编译不出来!) --%>
<jsp:forward page="/2.jsp">
<jsp:param value="test" name="username"/>
<jsp:param value="123" name="password"/>
</jsp:forward>
</body>
</html>
<%@ 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></title>
</head>
<body>
<!-- 获取 pageContext 中的内容 -->
<%=pageContext.getAttribute("page") %>
<!-- 获取转发中的参数 -->
<%-- <%=request.getAttribute("username") %> --%>
<!-- 是保存在参数中,而不是域中 -->
<%=request.getParameter("username") %>
<%=request.getParameter("password") %>
</body>
</html>
el表达式
<%@ 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>
<!-- 测试域中的查找顺序 -->
<%
/* 一般系统方法中,需要一个 int 参数,一般会是常量 */
/* 只要有 pageContext 就可以向各个域中添加值,参数三表示对应域的常量 */
// pageContext.setAttribute("page", "pageContext", PageContext.PAGE_SCOPE);
// pageContext.setAttribute("page", "request", PageContext.REQUEST_SCOPE);
// pageContext.setAttribute("page", "session", PageContext.SESSION_SCOPE);
pageContext.setAttribute("page", "application", PageContext.APPLICATION_SCOPE);
%>
<!-- 从小到大找 -->
<%-- =pageContext.findAttribute("page") --%>
<!-- 使用 el 表达式,获取域中数据(注意:报错就重新粘贴一下) -->
<!-- 实际上就使用 pageContext.findAttribute 这个方法 -->
${page }
</body>
</html>
<%@page import="com.lanou3g.Address"%>
<%@page import="com.lanou3g.User"%>
<%@ 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>
<!-- 域中保存对象使用 el 表达式获取 -->
<%
User user = new User();
user.setUsername("test");
user.setPassword("123");
request.setAttribute("u", user);
// 添加地址对象
Address address = new Address();
address.setCity("zh");
user.setAddress(address);
%>
<!-- 相当于调用 get 方法,前提:必须有 get 方法 -->
<!-- 如果我 user 对象中,有个属性,也是个对象 -->
${u.username }
<!-- JavaBean 导航:通过调用 get 方法获取的
直接使用点 -->
${u.address.city }
<!-- 使用[]获取值 -->
<!-- 注意:使用[]要加引号 -->
<!-- 如果域中保存的是个集合 -->
${u["username"] }
${u['username'] }
</body>
</html>
<%@page import="java.util.HashMap"%>
<%@page import="java.util.ArrayList"%>
<%@ 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>
<!-- 创建 list 集合和 map 集合,添加几个值,测试 el 表达式的获取 -->
<%
ArrayList<Integer> arrayList = new ArrayList<>();
HashMap<String,ArrayList<Integer>> hashMap = new HashMap<>();
arrayList.add(1);
arrayList.add(2);
arrayList.add(3);
request.setAttribute("list", arrayList);
hashMap.put("a", arrayList);
hashMap.put("b", arrayList);
hashMap.put("c", arrayList);
request.setAttribute("map", hashMap);
%>
<!-- 如果是集合的话,只能使用[] -->
${list }
${list[0] }
${map }
${map["a"] }
</body>
</html>
<%@page import="java.util.ArrayList"%>
<%@ 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>
<!-- 使用 el 表达式,判断 empty 空值 -->
<%
String str1 = null;
request.setAttribute("str1", str1);
String str2 = "";
request.setAttribute("str2", str2);
ArrayList<String> list1 = new ArrayList<>();
list1.add("aaa");
ArrayList<String> list2 = new ArrayList<>();
ArrayList<String> list3 = null;
ArrayList<String> list4 = new ArrayList<>();
list1.add("");
request.setAttribute("list1", list1);
request.setAttribute("list2", list2);
request.setAttribute("list3", list3);
request.setAttribute("list4", list4);
/* 测试三木运算符 */
String sex = "男";
request.setAttribute("sex", sex);
%>
<!-- 判断是否为空,只要没有值,就返回 true -->
${empty str1 }
${empty str2 }
<!-- 判断集合中指向空和不添加值,返回什么? -->
${empty list1 }
${empty list2 }
${empty list3 }
${empty list4 }
<!-- 支持三目运算符 -->
${empty list3 ? "前面" : "后面" }
<!-- 根据域中的值,来默认选择男女 -->
<br><input type="radio" value="男" name="sex" ${sex=="男" ? "checked='checked'" : "" }>男
<br><input type="radio" value="女" name="sex" ${sex=="女" ? "checked='checked'" : "" }>女
</body>
</html>
隐式对象
<%@ 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>
<!-- 测试隐式对象 可以直接在 el 表达式中使用 -->
<%
pageContext.setAttribute("page", "pageContext", PageContext.PAGE_SCOPE);
pageContext.setAttribute("page", "request", PageContext.REQUEST_SCOPE);
pageContext.setAttribute("page", "session", PageContext.SESSION_SCOPE);
pageContext.setAttribute("page", "application", PageContext.APPLICATION_SCOPE);
%>
${pageScope.page }
${requestScope.page }
${sessionScope.page }
${applicationScope.page }
</body>
</html>
<%@ 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>
<!-- 容错性强,如果域中没有该值,什么都不显示,不会显示 null -->
${www }
<!-- el 表达式中的 pageContext 就真的代表对象,可以调用方法 -->
<form action="${pageContext.request.contextPath }/9.jsp" method="post">
账号:<input type="text" name="username">
<br>密码:<input type="password" name="password">
<br><input type="checkbox" name="hobby" value="唱歌">唱歌
<input type="checkbox" name="hobby" value="跳舞">跳舞
<br><input type="submit" value="提交">
</form>
</body>
</html>
<%@ 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>
<%
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
%>
<!-- 用 el 表达式获取请求中的参数 -->
<!-- 相当于调用了 getParameter() 方法 -->
${param.username }
${paramValues.hobby[0] }<br>
<!-- 获取请求头 -->
${header["User-Agent"] }<br>
<!-- 获取 cookie 中的值 -->
${cookie.JSESSIONID }<br>
${cookie.JSESSIONID.value }<br>
</body>
</html>
JSTL 表达式
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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>
<!-- JSTL 表达式需要导包 taglib -->
<!-- JSTL 表达式实际上就处理 jsp 页面上的逻辑处理 -->
<!-- 通用标签 set out remove -->
<!-- 声明一个变量 默认是放在 page 域中 -->
<c:set var="num" value="10" scope="session"></c:set>
<!-- 相当于直接输出到网页上,与 out 对象一样 -->
<c:out value="${num }"></c:out> <!-- 与${num }一样 -->
<c:out value="${num }" default="aaa"></c:out>
<!-- 删除 -->
<c:remove var="num" scope="session"/>
${num }
</body>
</html>
逻辑表达式
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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>
<!-- 声明变量 -->
<c:set var="n" value="10"></c:set>
<!-- 逻辑表达式 if choose -->
<c:if test="${n > 4 }">
aaa
</c:if>
<c:if test="${n < 4 }">
bbb
</c:if>
<c:choose>
<c:when test="${n == 5 }">5</c:when>
<c:when test="${n < 5 }">10</c:when>
<c:otherwise>
不知道
</c:otherwise>
</c:choose>
</body>
</html>
循环
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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>
<%
ArrayList<String> list = new ArrayList<>();
list.add("aaa");
list.add("bbb");
list.add("ccc");
list.add("ddd");
list.add("eee");
request.setAttribute("list", list);
%>
<!-- 循环 -->
<c:forEach var="i" begin="0" end="10" step="3">
${i }<br>
</c:forEach>
<!-- items 代表需要遍历的容器, var 表示容器中的对象 -->
<c:forEach items="${list }" var="j" step="2">
${j }<br>
</c:forEach>
<c:forEach items="${list }" var="l" varStatus="vs">
<tr>
<td>${l }</td>
<td>${vs.index }</td>
<td>${l }</td>
<td>${l }</td>
<td>${l }</td>
</tr>
</c:forEach>
</body>
</html>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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>
<style type="text/css">
body{
background: purple;
text-align: center;
color: white;
}
table{
margin: 0 auto;
}
</style>
</head>
<body>
<%
ArrayList<String> list = new ArrayList<>();
list.add("aaa");
list.add("bbb");
list.add("ccc");
list.add("ddd");
list.add("eee");
request.setAttribute("list", list);
%>
<table border="1">
<tr>
<td width="100">值</td>
<td width="100">索引</td>
<td width="100">计数</td>
<td width="100">第一个</td>
<td width="100">最后一个</td>
</tr>
<c:forEach items="${list }" var="l" varStatus="vs">
<tr>
<td>${l }</td>
<td>${vs.index }</td>
<td>${vs.count }</td>
<td>${vs.first }</td>
<td>${vs.last }</td>
</tr>
</c:forEach>
</table>
</body>
</html>
pageContext
// 查看 pageContext 中的方法
public void fun(PageContext pageContext) {
// 强大的 pageContext 可以获取任何域对象
// 从小到大
pageContext.getRequest();
pageContext.getSession();
pageContext.getServletContext();
pageContext.getServletConfig();
// 强大的方法(el 表达式就依赖这个方法)
// 该方法会搜索所有域对象,用 key 去取值
// 按域的大小,从小到大进行搜索,找到就输出对应的 value
// request < session < context 这个顺序搜索
pageContext.findAttribute("key");
}