JSP

JSP

JSP——Java Server Page:Java服務(wù)端頁面,在html頁面中編寫Java代碼的頁面。

絕大多數(shù)時候,我們希望響應(yīng)的不是簡簡單單一句話,而是一個頁面,我們用PrintWriter對象去寫一個頁面也是可以的,但缺點(diǎn)太明顯。

所以直接返回一個頁面,并且能夠?qū)慗ava代碼就能大大簡化我們的開發(fā),這就是——JSP。

當(dāng)前不提倡在jsp頁面中寫java代碼,代替使用EL表達(dá)式。

JSP是簡化Servlet編寫的一種技術(shù),它將Java代碼和HTML語句混合在同一個文件中編寫,只對網(wǎng)頁中的要動態(tài)產(chǎn)生的內(nèi)容采用Java代碼來編寫,而對固定不變的靜態(tài)內(nèi)容采用普通靜態(tài)HTML頁面的方式編寫。


建立對JSP的直觀認(rèn)識

在JSP頁面中編寫的Java代碼需要嵌套在<%和%>中,嵌套在<%和%>之間的Java代碼被稱之為腳本片段(Scriptlets),沒有嵌套在<%和%>之間的內(nèi)容被稱之為JSP的模版元素。

WEB容器(Servlet引擎)接收到以.jsp為擴(kuò)展名的URL的訪問請求時,它將把該訪問請求交給JSP引擎去處理。

每個JSP 頁面在第一次被訪問時,JSP引擎將它翻譯成一個Servlet源程序,接著再把這個Servlet源程序編譯成Servlet的class類文件,然后再由WEB容器(Servlet引擎)像調(diào)用普通Servlet程序一樣的方式來裝載和解釋執(zhí)行這個由JSP頁面翻譯成的Servlet程序——JSP本質(zhì)上就是一個Servlet。

idea中jsp被編譯出的servlet被放在哪里?
C:\Users\Administrator.IntelliJIdea2016.1\system\tomcat\Tomcat_8_0_32_web1\work\Catalina\localhost\web\org\apache\jsp

新建一個jsp頁面hello.jsp

<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <%
        Date date = new Date();
        System.out.println(date);
    %>
</body>
</html>

運(yùn)行可以看到控制臺上有時間輸出。


JSP中9個隱式對象

隱式對象(或隱含變量):在JSP當(dāng)中我們沒有手動聲明創(chuàng)建,但實際存在,可以直接使用的對象。

final javax.servlet.jsp.PageContext pageContext;
javax.servlet.http.HttpSession session = null;
final javax.servlet.ServletContext application;
final javax.servlet.ServletConfig config;
javax.servlet.jsp.JspWriter out = null;
final java.lang.Object page = this;
// 還有request和response以及exception對象,一共9個

request:客戶端的請求信息被封裝在request對象中,通過它才能了解用戶的需求,然后做出響應(yīng)

②response:包含了響應(yīng)客戶請求的有關(guān)信息,但在JSP中使用很少

③pageContext:頁面的上下文,是PageContext的一個對象,可以從該對象中獲取到其他8個隱含對象,也可以獲取到當(dāng)前頁面的其他信息

session:指的是客戶端與服務(wù)器的一次會話,從客戶端連到服務(wù)器的一個WebApplication開始,知道客戶端與服務(wù)器斷開連接為止

application:代表當(dāng)前web應(yīng)用,是ServletContext對象,能實現(xiàn)用戶間數(shù)據(jù)的共享,可存放全局變量,它開始于服務(wù)器的啟動,直到服務(wù)器的關(guān)閉,在此期間,此對象一直存在;這樣在用戶的前后連接或不同用戶之間的連接中,可以對此對象的同一屬性進(jìn)行操作;在任何地方對此對象屬性的操作,都將影響到其他用戶對此的訪問。服務(wù)器的啟動和關(guān)閉決定了application對象的生命周期

⑥config:當(dāng)前JSP對應(yīng)的Servlet的ServletConfig對象,可獲取該Servlet的初始化參數(shù)(開發(fā)時基本不用),需要通過映射的地址才可以。

out:JspWriter對象,調(diào)用out.println()可以直接把字符串打印到瀏覽器上

⑧page:page對象就是指向當(dāng)前JSP頁面本身,類型為Object,有點(diǎn)類似于類中的this,幾乎不使用

⑨exception:該對象是一個例外對象,只有頁面是一個錯誤頁面,即isErrorPage設(shè)置為true的時候(默認(rèn)為false)才能使用,否則無法編譯。

注意:JSP可以放置在WEB應(yīng)用程序中的除了WEB-INF及其子目錄外的其他任何目錄中


JSP模板元素

JSP頁面中的靜態(tài)HTML內(nèi)容稱之為JSP模版元素(比如html,body等等),在靜態(tài)的HTML內(nèi)容之中可以嵌套JSP的其他各種元素來產(chǎn)生動態(tài)內(nèi)容和執(zhí)行業(yè)務(wù)邏輯。
JSP模版元素定義了網(wǎng)頁的基本骨架,即定義了頁面的結(jié)構(gòu)和外觀。


JSP表達(dá)式

JSP表達(dá)式(expression)提供了將一個java變量或表達(dá)式的計算結(jié)果輸出到客戶端的簡化方式,它將要輸出的變量或表達(dá)式直接封裝在<%= 和 %>之中。
在JSP表達(dá)式中嵌套的變量或表達(dá)式后面不能有分號。

<body>
    <%
        Date date = new Date();
    %>
    
    <%= date %>
</body>

JSP腳本片斷

像片段一樣的JSP表達(dá)式,嵌套在<%和%>中,必須全部是符合java語法的語句。

<body>

    <%
        String ageStr = request.getParameter("age");
        int age = Integer.parseInt(ageStr);
        if(age > 18){
    %>
    
        成年....
        
    <%  }else{ %>
    
        未成年...
        
    <%  } %>
    
</body>
<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2018/3/9
  Time: 21:45
  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>
<form action="index.jsp" method="post">
    <input type="text" name="age">
    <input type="submit">
</form>
</body>
</html>


頁面間跳轉(zhuǎn)的2種方式---請求轉(zhuǎn)發(fā)和請求重定向(重點(diǎn))

一.請求轉(zhuǎn)發(fā)

請求轉(zhuǎn)發(fā)需要借助于一個接口,RequestDispatcher接口 ,利用這個接口的forward方法實現(xiàn)請求轉(zhuǎn)發(fā)。

二.請求重定向

用HttpServletResponse的sendRedirect方法實現(xiàn)請求重定向。


請求轉(zhuǎn)發(fā)

<body>
    <a href="forwardServlet">Forward</a>
</body>
@WebServlet("/forwardServlet")
public class ForwardServlet extends HttpServlet {
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("ForwardServlet's doGet");
        // 請求轉(zhuǎn)發(fā)
        // 1.調(diào)用HttpServletRequest的getRequestDispatcher()方法獲得RequestDispatcher對象
        // 調(diào)用getRequestDispatcher()需要傳入要轉(zhuǎn)發(fā)的地址(/代表當(dāng)前WEB應(yīng)用的根目錄)
        RequestDispatcher requestDispatcher = request.getRequestDispatcher("/testServlet"); 
        // 2.調(diào)用HttpServletRequest的forward(request,response)進(jìn)行請求轉(zhuǎn)發(fā)
        requestDispatcher.forward(request, response);
    }

}
@WebServlet("/testServlet")
public class TestServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("TestServlet's doGet方法");
    }

}

運(yùn)行html,點(diǎn)擊查看控制臺


image.png

請求重定向

<body>
    <a href="forwardServlet">Forward</a>
    <a href="redirectServlet">Redirect</a>
</body>
@WebServlet("/redirectServlet")
public class RedirectServlet extends HttpServlet {
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("RedirectServlet's doGet");
        response.sendRedirect("testServlet");
    }

}

再次運(yùn)行html,點(diǎn)擊Redirect,查看控制臺


image.png

貌似與請求轉(zhuǎn)發(fā)沒什么區(qū)別,但實際區(qū)別非常之大。

觀察用請求轉(zhuǎn)發(fā)和請求重定向時地址欄的變化
請求轉(zhuǎn)發(fā)地址欄沒變


image.png

請求重定向地址欄變化了


image.png

說明一個重要問題(本質(zhì)區(qū)別):
請求轉(zhuǎn)發(fā)只發(fā)出了一個請求
請求重定向發(fā)出了2個請求

請求轉(zhuǎn)發(fā)和請求重定向的目標(biāo)可以是一個servlet也可以是一個jsp
request.getRequestDispatcher("/XXXServlet").forward(request,response)

response.sendRedirect("/XXXServlet")只能在doGet或doPost方法中調(diào)用一次
也即,在doGet或doPost方法中,只能轉(zhuǎn)發(fā)或重定向到一個頁面,不能到多個頁面

屬性相關(guān)方法

①設(shè)置屬性:void setAttribute(String name,Object o)
②獲取指定屬性:Object getAttribute(String name)
能夠使用這些方法的對象有4個:
pageContext,request,session,application-->這4個對象也稱為域?qū)ο?/strong>

域?qū)ο螅ㄖ攸c(diǎn))

域:
2個維度:一份數(shù)據(jù)可以在多少個頁面間共享,可以在多長的時間范圍內(nèi)共享。

新建ServletA 書寫一下代碼運(yùn)行測試

@WebServlet("/aaa")
public class ServletA extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        javax.servlet.jsp.PageContext pageContext = javax.servlet.jsp.JspFactory.getDefaultFactory().getPageContext(this, request, response,
                null, true, 8192, true);
        pageContext.setAttribute("pageAttr","pageValue");
        String strPageContext = (String)pageContext.getAttribute("pageAttr");
        System.out.println(strPageContext);


        request.setAttribute("requestAttr","requestValue");


        HttpSession session = request.getSession();
        session.setAttribute("sessionAttr","sessionValue");

        request.getServletContext().setAttribute("applicationAttr","applicationValue");

        request.getRequestDispatcher("/ccc").forward(request,response);


    }
}

ServletC

@WebServlet("/ccc")
public class ServletC extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("ServletC");
        javax.servlet.jsp.PageContext pageContext = javax.servlet.jsp.JspFactory.getDefaultFactory().getPageContext(this, request, response,
                null, true, 8192, true);

        String strPageContext = (String)pageContext.getAttribute("pageAttr");
        System.out.println(strPageContext);

        String strRequestContext = (String)request.getAttribute("requestAttr");
        System.out.println(strRequestContext);

        String strSessionContext = (String)request.getSession().getAttribute("sessionAttr");
        System.out.println(strSessionContext);

        String strAppContext = (String)request.getServletContext().getAttribute("applicationAttr");
        System.out.println(strAppContext);
    }
}

ServletB

@WebServlet("/bbb")
public class ServletB extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        javax.servlet.jsp.PageContext pageContext = javax.servlet.jsp.JspFactory.getDefaultFactory().getPageContext(this, request, response,
                null, true, 8192, true);
        pageContext.setAttribute("pageAttr","pageValue");
        String strPageContext = (String)pageContext.getAttribute("pageAttr");
        System.out.println(strPageContext);


        request.setAttribute("requestAttr","requestValue");


        HttpSession session = request.getSession();
        session.setAttribute("sessionAttr","sessionValue");

        request.getServletContext().setAttribute("applicationAttr","applicationValue");

        response.sendRedirect("/ccc");
    }
}

pageContext:屬性的作用范圍僅限于當(dāng)前servlet或JSP頁面

request:屬性的作用范圍僅限于同一個請求(考慮頁面轉(zhuǎn)發(fā)的情況)

session:屬性的作用范圍限于一次會話(瀏覽器打開直到關(guān)閉,稱為一次會話,前提是在此期間會話沒有失效),數(shù)據(jù)是用戶獨(dú)立的。(后面細(xì)講)。

application:屬性的作用范圍限于當(dāng)前WEB應(yīng)用,是范圍最大的屬性作用范圍,只要在一處設(shè)置屬性,在其他各處的JSP或Servlet中都可以獲取,直到服務(wù)器關(guān)閉。數(shù)據(jù)所有用戶共享。

@WebServlet(name = "CountServlet",urlPatterns = "/count")
public class CountServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Object obj = request.getSession().getAttribute("count");
        if(obj == null)
        {
            request.getSession().setAttribute("count",1);
        }
        else
        {
            int count = Integer.parseInt(obj.toString());
            count++;
            request.getSession().setAttribute("count",count);
        }

        response.sendRedirect("index.jsp");

    }
}

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
<a href="/count">加1</a>

  ${count}
  </body>
</html>

servlet中通過getAttribute(String name),獲取指定屬性。而jsp中使用EL表達(dá)式。

EL表達(dá)式

EL能夠極大的簡化我們的開發(fā)

EL 全名為 Expression Language,它原本是 JSTL 1.0 為方便存取數(shù)據(jù)所自定義的語言。當(dāng)時 EL只能在JSTL標(biāo)簽中使用。到了JSP2.0 之后,EL已經(jīng)正式納入成為標(biāo)準(zhǔn)規(guī)范之一。

語法

EL 語法很簡單,它最大的特點(diǎn)就是使用上很方便。接下來介紹 EL 主要的語法結(jié)構(gòu)

${requestAttr}
${user.sex} 

所有 EL 都是以 ${ 為起始、以} 為結(jié)尾的。

User user = (User)session.getAttribute("user"); 
String sex = user.getSex( ); 

兩者相比較之下,可以發(fā)現(xiàn) EL 的語法更為方便、簡潔。

EL 隱含對象

如果我們在request和application中設(shè)置了同名屬性怎么辦?
與范圍有關(guān)的隱含對象
applicationScope
sessionScope
requestScope
pageScope

JSP中如何做條件判斷和遍歷集合

JSTL

JavaServer Pages Standard Tag Library (1.1 ) ,它的中文名稱為 JSP 標(biāo)準(zhǔn)標(biāo)簽函數(shù)庫。JSTL 是一個標(biāo)準(zhǔn)的已制定好的標(biāo)簽庫,可以應(yīng)用于各種領(lǐng)域,如:基本輸入輸出、流程控制、循環(huán)。

安裝

直接把JSTL有關(guān)的兩個jar包,拷貝進(jìn)lib目錄下

然后在jsp頁面上添加指令,就可以使用了

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

流程控制

<c:if>
<c:if>的用途就和我們一般在程序中用的 if 一樣


image.png

    <!-- 沒有else,但能儲存結(jié)果 -->
    <c:if test="${ sessionScope.age > 18 }">成年人</c:if>

<c:choose>
<c:choose>本身只當(dāng)做 <c:when> 和 <c:otherwise> 的父標(biāo)簽。
<c:when> 和 <c:otherwise>也不能脫離<c:choose>。
<c:otherwise>必須在<c:when>之后使用。

    <c:choose>
        <c:when test="${param.age > 60 }">
            老年
        </c:when>
        <c:when test="${param.age > 40 }">
            中年
        </c:when>
        <c:when test="${param.age > 18 }">
            青年
        </c:when>
        <c:otherwise>
            未成年
        </c:otherwise>
    </c:choose>

迭代操作

<c:forEach>
<c:forEach> 為循環(huán)控制,它可以將集合(Collection)中的成員循序瀏覽一遍。運(yùn)作方式為當(dāng)條件符合時,就會持續(xù)重復(fù)執(zhí)行<c:forEach>的本體內(nèi)容。

image.png

遍歷Collection(數(shù)組也一樣)

    <%
    <c:forEach items="${ requestScope.list }" var="customer">
        ${ customer.id } --${ customer.name } --${ customer.address } --${ customer.phone }<br><br>
    </c:forEach>

遍歷Map集合

    <c:forEach items="${ requestScope.map }" var="customer">
        ${ customer.key } --- ${ customer.value.id },${ customer.value.name },${ customer.value.address },${ customer.value.phone }<br><br>
    </c:forEach>

JSP

JSP指令

新建一個jsp文件,最上面一行就是jsp指令

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

JSP指令(directive)是為JSP引擎而設(shè)計的,它們并不直接產(chǎn)生任何可見輸出,而只是告訴引擎如何處理JSP頁面中的其余部分。

JSP指令的基本語法格式:
<%@ 指令 屬性名="值" %>

include指令

<%@ 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>
    <h1>A Page</h1>
    <!-- 在a.jsp中包含b.jsp,file后的資源寫的是相對路徑,如果有/代表是wen應(yīng)用根目錄 -->
    <%@include file="b.jsp" %>
</body>
</html>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1.學(xué)習(xí)內(nèi)容 JSP技術(shù)入門和常用指令 JSP的內(nèi)置對象&標(biāo)簽介紹 EL表達(dá)式&EL的內(nèi)置對象 2.JSP技術(shù)入門...
    WendyVIV閱讀 2,326評論 1 18
  • 一、JSP基礎(chǔ) 1.1什么是JSP JSP(Java ServerPage)是Java服務(wù)器端動態(tài)頁面技術(shù)。是su...
    晨星資源閱讀 1,267評論 0 6
  • JSP 基本結(jié)構(gòu)JSP 頁面組成:模板文本(HTML,XML,純文本),JSP 元素;JSP 元素:腳本元素,指令...
    jslancer閱讀 1,077評論 0 11
  • 1.什么是JSP (1)jsp全稱是Java Server Pages,它和Servlet技術(shù)一樣都是sun公司定...
    yjaal閱讀 3,834評論 5 99

友情鏈接更多精彩內(nèi)容