JSP的引入:
在Servlet中,能夠直接獲取後臺服務器中的數據:獲取數據方便 通過拼接字符串的形式,給瀏覽器響應html:操作麻煩,效率太低。
如果能夠有一個既能直接書寫html代碼,又能書寫Servlet代碼的頁面。就能夠在客戶端和服務端直接進行數據交互了。所以引入了JSP。
jsp的三個指令
- <%@ 指令%>
- <%@ include file=”other02.jsp”%>
- ==把另外一個頁面other02.jsp的所有內容拿過來一起輸出。 所有的標籤元素都包含進來==
- <%@ taglib prefix=”” uri=”“%>
- ==uri: 標籤庫路徑
prefix : 標籤庫的別名==
- ==uri: 標籤庫路徑
jsp的動態標籤
jsp代碼
<%@ page import="java.util.Date" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>測試JSP</title>
</head>
<body>
<%--
獲取當前日期,並格式化成字符串。將其存儲在request域中
--%>
<%
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:dd");
String curTime = sdf.format(date);
request.setAttribute("curTime",curTime);
%>
<%--
在頁面上打印輸出當前時間
--%>
當前系統時間:<span style="color: red"><%=request.getAttribute("curTime")%></span>
</body>
</html>
- <% %> 中書寫的代碼被直接解析成java代碼;
- html部分都被out.write(“”)方法以字符串的形式拼接,然後響應給瀏覽器;
- 頁面上輸出的內容都在_jspService方法中,這個方法有兩個參數request,response。由此可看出JSP本質上就是一個Servlet。
jsp中書寫java代碼的幾種方式
方式1.<%java代碼%>
<%@ page contentType="text/html;charset=utf-8" language="java" %>
<html>
<head>
<title>Demo1</title>
</head>
<body>
<!--腳本片段-->
<%
for(int i = 0;i<10;i++){
response.getWriter().println("i="+i);
}
%>
</body>
</html>
方式2.腳本表達式<% =表達式 %>
爲了簡化用response對象書寫繁瑣性
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>DEMO</title>
</head>
<body>
<% ="這是DEMO示例文本" %>
<!--和下列表達式等價-->
<%
//信息先存在緩存中,然後輸出
out.println("hello out");
//直接輸出。效率更高
response.getWriter().println("這是示例文本")
%>
</body>
</html>
方式3.成員變量<% !java代碼%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>DEMO</title>
</head>
<body>
<% ="聲明成員變量" %>
<!--和下列表達式等價-->
<%!
public viod method1(){};
int i = 10;
%>
</body>
</html>
jsp的使用
jsp是問了簡化servlet的書寫,和在頁面上的表達方式,所以用jsp來動態取值,顯示在頁面上。
演示:
- 在servlet中向session中存入了一個Student stu {name:張三, age:18}對象;
- 在jsp中取出該對象
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Demo</title>
</head>
<body>
<%
Student student = request.getSession().getAttribute("stu");
String stu_name = student.getName();
int age = student.getAge();
response.getWriter().println("name"+stu_name+"----age"+age);
//等價於
<% ="name"+stu_name+"----age"+age %>
%>
</body>
</html>
EL
- 全稱:Expression Language
- EL的出現,是爲了簡化JSP的書寫,從【域對象】中查找指定的數據。
- EL的表達式:${表達式內容}
EL的四大域
域對象 | 對象名稱 | 說明 |
---|---|---|
page域 | pageScope | page域指的是當前JSP頁面,其中存儲的數據只在當前頁面有效 |
request域 | requestScope | request域:一次請求或請求鏈中request域 |
session域 | sessionScope | session域:一次會話過程中,session域 |
application域 | applicationScope | application域:服務啓動後整個項目對應的ServletContext域 |
EL表達式從四大域中取值
域對象 | 取值方式 |
---|---|
page域 | ${pageScope.xxx} |
request域 | ${requestScope.xxx} |
session域 | ${sessionScope.xxx} |
application域 | ${applicationScope.xxx} |
示例代碼
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%--
分別往request域,session域,servletContext域和pageContext中存值
--%>
<%
request.setAttribute("requestName","requestScope");
request.getSession().setAttribute("SessionName","SessionScope");
request.getServletContext().setAttribute("ServletContextName","servletContextScope");
pageContext.setAttribute("pageName","PageScope");
%>
<!--使用EL表達式從上述域中取值-->
從request域中取值:${requestScope.requestName}<br/>
從session域中取值:${SessionScope.SessionName}<br/>
從servletContext域中取值:${"ServletContextScope.serletContextName}<br/>
從page域中取值:${pageScope.pageName}
</body>
</html>
EL取值域的查找
當不確定域的時候,從page->request->sesssion->servletContext,從小到大開始尋找。
EL表達式cookie中取值
//servlet 代碼
protected void doGet(HttpServletRequest request,HttpServletResponse response)throws Exception{
//創建Cookie
Cookie cookie = new Cookie("username","lisi");
response.addCookie(cookie);
}
//jsp代碼
<body>
姓名:<input type="text" name="username" value="${cookie.username.value}"/>
</body>
EL運算符
- 同java一致
JSTL
全稱The JavaServer Pages Standard Tag Library
- 引入標籤
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
JSTL的核心標籤庫
- JSTL的常用標籤
1. <c:if test></c:if>
2. <c:forEach var="i" items="arr" begin="" end="" step="1"></c:forEach>
3. <c:choose></c:choose>與<c:when><c:otherWise>連用
//<c:if>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
session.setAttribute("user","username");
%>
<c:if test="${user==null}">
<c:out value="請登錄"></c:out>
</c:if>
<c:if test="${user!= null}">
<c:out value="登錄成功"></c:out>
</c:if>
</body>
</html>
//servlet
protected void doGet(HttpServletRequest request ,HttpServletResponse response)throws Exception{
List<String> list = new ArrayList<>();
list.add("zhangsan");
list.add("lisi");
list.add("wangwu");
request.getSession.setAttribute(list);
}
//<c:forEach>從域中取出來
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%--
使用foreach進行遍歷
--%>
<c:forEach items="${list}" var="item" begin="0" end="${list.size()}" step="1">
<font color="red">${item}</font><br>
</c:forEach>
</body>
</html>
對map集合的遍歷
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
User: admin
Date: 2018/8/13
Time: 17: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>
<%
int[] arr = {123,456,789};
request.setAttribute("arr",arr);
Map<String,String> map = new HashMap<>();
map.put("one","楊冪");
map.put("two","關曉彤");
map.put("three","劉詩詩");
request.setAttribute("map",map);
%>
<c:forEach items="${arr}" var="item">
${item}<br>
</c:forEach>
<c:forEach items="${map}" var="item">
map的key:${item.key}=${item.value}<br>
</c:forEach>
</body>
</html>
choose標籤
protected void doGet(HttpServletRequest request,HttpServletResponse response)throws Exception{
int id = 1;
switch (id){
case 0:
System.out.println("id爲0");
break;
case 1:
System.out.println("id爲1");
break;
default:
System.out.println("id未知");
break;
}
request.getSession().setAttribute("id",id);
}
//html<c:choose> 等價於
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<c:choose>
<c:when test="${id}==0">
id爲0
</c:when>
<c:when test="${id}==1">
id爲1
</c:when>
<c:otherWise>
id未知
</c:otherWise>
</c:>
</body>
</html>