JSP
Java Server Pages简称jsp,是简化Servlet编写的一种技术,它将Java代码和Html语句混合在同一个文件中编写,页面动态资源使用java代码,静态页面使用html
初体验:
<%@ page import="java.util.Date" %>
<%@ page import="java.text.SimpleDateFormat" %><%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2021/6/15
Time: 15:36
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>动态展示时间</title>
</head>
<body>
<%
Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日");
String currentTime = simpleDateFormat.format(date);
%>
<h3><% out.write(currentTime);%></h3>
</body>
</html>
实现原理
JSP本质上就是一个Servlet
jsp页面,运行的时候,会先生成一个java文件,进行编译灾再去执行
脚本与注释
脚本
- <% 代码 %>脚本片段,声称在service方法中,每次请求的时候都会执行
- <%! 代码 %>申明片段,在java代码中声名成员变量,放在jsp生成java文件中的成员位置
- <%= 代码%>输出脚本片段,相当于out。print(“代码”)方法,输出到jsp页面
注释
html注释
jsp注释
<%-- --%>
java注释(jsp脚本内使用)
单行//
多行/* /
文档注释/* */
作用范围
注释 1 | JSP源码 2 | java源码 1 | html源码 2 |
---|---|---|---|
html | 可见 | 可见 | 可见 |
java | 可见 | 可见 | 不可见 |
jsp | 可见 | 不可见 | 不可见 |
jsp指令
指令?
用于配置jsp页面,导入资源文件
格式
<%@ 指令名称 属性名1=“属性值1” 属性名2=“属性值2” …%>
jsp三大指令
page指令
用于配置当前jsp页面的一些信息
属性名 | 说明 |
---|---|
contentType | 相当于response.setContentType();设置响应体的MIME类型和编码方式 |
language | 目前仅支持java语言 |
import | 导入jar包 |
errorPage | 当前页报错后,跳转指定错误提示页面 |
isErrorPage | 声明当前jsp页面是一个异常处理页面,打开异常开关 false:(默认) true:可以操作execption异常对象 |
include指令
页面包含的,导入页面的资源文件
<%@include file=“xxx.jsp”%>
例如以下代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>show.jsp</title>
</head>
<body>
<%@include file="top.jsp"%>
<div style="border: red solid 5px;height: 400px">我是include静态包含,主体..</div>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>top.jsp</title>
</head>
<body>
<div style="border: skyblue dashed 5px;height: 180px">头部</div>
</body>
</html>
taglib指令
导入资源,在jstl中会使用该指令导入标签库
profix自定义前缀名
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>jstl</title>
</head>
<body>
导入jstl标签库
</body>
</html>
JSP内置对象
在jsp页面中不需要获取和创建,可以直接使用的对象
九大内置对象
变量名 | 说明 | 说明 |
---|---|---|
pageContext | PageContext | 当前页面中共享数据(域对象) |
request | HttpServletRequest | 一次请求中共享数据(域对象) |
session | HttpSession | 一次会话中共享数据(域对象) |
application | ServletContext | 整个web应用共享数据(域对象) |
response | HttpServletResponse | 响应对象 |
page(this) | Object | 当前页面(servlet)对象 |
out | JSPWriter | 数处对象 |
config | ServletConfig | servlet配置对象 |
execption | Throwable | 异常对象(默认关闭。。。) |
常用内置对象
变量名 | 说明 |
---|---|
pageContext | 当前页面的域对象和获取其他八个内置对象 |
request | 接受用户请求(参数)和获取一次请求中域对象 |
response | 设置响应:字节流和字符流 |
out | 专门在jsp中处理字节流 print():可以输出一切类型 write():只能输出字符类型 |
EL表达式
Expression Language表达式语言
格式: ${表达式}
作用:主要是从域对象中查找指定的数据
语法: ${域名城.键名}:从指定域中获取指定键的值
域名 | 对应关系 |
---|---|
pageScope | pageContext |
requestScope | request |
sessionScope | session |
applicationScope | application(ServletContext) |
${键名}:表示依次从最小的域中查找是否有该键对应的值,直到找到为止。
例如:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
pageContext.setAttribute("name", "吉泽明步老师");
request.setAttribute("name", "饭岛爱老师");
request.setAttribute("age", 20);
session.setAttribute("sex", "女");
application.setAttribute("address", "日本东京热");
%>
<%
%>
${pageScope.name}
${requestScope.age}
${sessionScope.sex}
${applicationScope.address}
EL获取值
获取字符串
代码演示
package com.lifly.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/elGetValueServlet")
public class ElGetValueServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 处理post请求乱码问题
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;utf-8");
request.setAttribute("username","tom");
request.getRequestDispatcher("/el_getValue.jsp").forward(request,response);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2021/6/16
Time: 14:57
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%--
<%
pageContext.setAttribute("name", "吉泽明步老师");
request.setAttribute("name", "饭岛爱老师");
request.setAttribute("age", 20);
session.setAttribute("sex", "女");
application.setAttribute("address", "日本东京热");
%>
${pageScope.name}
${requestScope.age}
${sessionScope.sex}
${applicationScope.address}
<br>
--%>
${name}<br>
${age}<br>
${sex}<br>
${address}<br>
<br>
${username}
</body>
</html>
获取对象
package com.lifly.JavaBean;
/**
* @Describe
* @Author Double LiFly
* @date 2021/6/16 15:13
*/
public class User {
private String username;
private String password;
public User() {
}
public User(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
package com.lifly.servlet;
import com.lifly.JavaBean.User;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/elGetObjectServlet")
public class ElGetObjectServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 处理post请求乱码问题
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;utf-8");
User user = new User("tom", "1234");
request.setAttribute("user",user);
request.getRequestDispatcher("/el_getObject.jsp").forward(request,response);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2021/6/16
Time: 15:12
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%--
获取user对象
--%>
直接打印对象:${user}<br>
获取对象的每一个属性:<br>
用户名:${user.username}<br>
密码:${user.password}<br>
</body>
</html>
获取数组
package com.lifly.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/elGetArrayServlet")
public class ElGetArrayServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 处理post请求乱码问题
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;utf-8");
String[] str = {"jack","rose","tom"};
request.setAttribute("str",str);
request.getRequestDispatcher("/el_getArray.jsp").forward(request,response);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2021/6/16
Time: 15:22
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%--
获取数组
--%>
直接打印数组:${str}<br>
获取数组的每一个值:<br>
${str[0]}<br>
${str[1]}<br>
${str[2]}<br>
</body>
</html>
获取List集合
package com.lifly.servlet;
import com.lifly.JavaBean.User;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
@WebServlet("/elGetListServlet")
public class ElGetListServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 处理post请求乱码问题
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;utf-8");
//创建集合(泛型为String)
ArrayList<String> list = new ArrayList<>();
list.add("jack");
list.add("rose");
list.add("tom");
//创建集合(泛型为User对象)
ArrayList<User> users = new ArrayList<>();
users.add(new User("jen","1234"));
users.add(new User("jim","2345"));
request.setAttribute("list",list);
request.setAttribute("users",users);
request.getRequestDispatcher("/el_getList.jsp").forward(request,response);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2021/6/16
Time: 15:28
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%--
获取集合
--%>
获取整个集合:<br>
${list}<br>
${users}<br>
获取集合元素list:<br>
${list[0]}<br>
${list[1]}<br>
${list[2]}<br>
获取集合集合User:<br>
${users[0]}<br>
${users[0].username} ${users[0].password}<br>
${users[1]}<br>
${users[1].username} ${users[1].password}<br>
</body>
</html>
获取Map集合
package com.lifly.servlet;
import com.lifly.JavaBean.User;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
@WebServlet("/elGetMapServlet")
public class ElGetMapServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 处理post请求乱码问题
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;utf-8");
//创建集合
HashMap<String, String> map = new HashMap<>();
map.put("jack","北京");
map.put("rose","上海");
map.put("tom","深圳");
HashMap<String, User> map1 = new HashMap<>();
map1.put("user1",new User("jim","1234"));
map1.put("user2",new User("jan","2345"));
request.setAttribute("map",map);
request.setAttribute("map1",map1);
request.getRequestDispatcher("el_getMap.jsp").forward(request,response);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2021/6/16
Time: 15:38
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%--
获取map集合
--%>
获取整个map:<br>
${map}<br>
${map1}<br>
获取map中的元素:<br>
${map.jack}<br>
${map.rose}<br>
${map.tom}<br>
获取map1中的元素:<br>
${map1.user1.username}|${map1.user1.password}<br>
${map1.user2.username}|${map1.user2.password}<br>
</body>
</html>