Day2_J2EE(Java 2 Enterprise Edition)_JSP語法介紹_隱含物件(Implicit Object)


我們可以在JSP的網頁專案中穿插java code
利用

<%  ..... %>
來做包覆(當你要寫不只一行java code的時候就用)
可以一至多行

<%=.... %>
可能是某一種方法或是變數的宣告
很常使用在賦予值或返回計算結果之情況



做輸出功能可以透過
out.println()去實現



當然我們也可以去透過JSP的程式將一串字串全部轉為大寫

轉換為全大寫 JSP
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
<%@ page language="java" contentType="text/html; charset=BIG5"
    pageEncoding="BIG5"%>
<!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=BIG5">
<title>英文字母轉為大寫</title>
</head>
<body>
 Hello World 全部轉大寫:<%=new String("Hello World").toUpperCase() %>
</body>
</html>


甚至是數值運算或是Boolean判斷都可透過JSP去做

數值計算 / Boolean判別

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
<%@ page language="java" contentType="text/html; charset=BIG5"
    pageEncoding="BIG5"%>
<!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=BIG5">
<title>Insert title here</title>
</head>
<body>
3乘於5等於:<%=3*5 %>

<br/>
<br/>
1是否大於2 ? ---> <%=1>2 %>


</body>
</html>



(PS:JSP  可以直接刷新即可更新,Serlet 則必須關閉重啟 Tomcat)

當Client端去呼叫某一個JSP file的時候 JSP 會先轉變為servlet
JSP會去轉為Serlet去執行JAVA code
最後在去將結果打印出來讓client端閱讀
client端瀏覽器就可藉此處理閱讀出Java邏輯


那我們也可以來寫一個打印當前時間的範例
讓我們會更有感覺捨麼叫做在html中穿插java程式碼,這就是JSP的彈性功用之一。

NowTime JSP 範例
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
<%@ page language="java" contentType="text/html; charset=BIG5"
    pageEncoding="BIG5"%>
<!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=BIG5">
<title>當前時間輸出</title>
</head>
<body>
 現在時間為:<%=new java.util.Date() %>
</body>
</html>






於 JSP當中
像是剛剛我們呼叫的out.println() 的method
你就會發覺
和之前學的 System.out.println()
差別在省去寫System.

那主要原因就在於 out  隸屬於隱含物件其中之一
我們是可直接透過它這個物件去打印資料的



隱含物件(Implicit Object/Variable)

out
轉譯後對應 JspWriter 物件,其內部關聯一個 PrintWriter 物件。

request
轉譯後對應 HttpServletRequest 物件。

response
轉譯後對應 HttpServletResponse 物件。

config
轉譯後對應 ServletConfig 物件。

application
轉譯後對應 ServletContext 物件。

session
轉譯後對應 HttpSession 物件。

pageContext
轉譯後對應 PageContext 物件,它提供了 JSP 頁面資源的封裝,並可設定頁面範圍屬性。

exception
轉譯後對應 Throwable 物件,代表由其他JSP 頁面丟出的例外物件,只會出現於 JSP 錯誤頁面(isErrorPage 設定為 true 的 JSP 頁面)。

page
轉譯後對應 this。


這邊我們先來剖析一下Tomcat範例包
在我們所下載下來的 這包 Tomcat 檔案夾中

我們看一下官方範例做學習


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ResourceBundle;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * The simplest possible servlet.
 *
 * @author James Duncan Davidson
 */

public class HelloWorldExample extends HttpServlet {

    private static final long serialVersionUID = 1L;

    @Override
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException
    {
        ResourceBundle rb =
            ResourceBundle.getBundle("LocalStrings",request.getLocale());
        response.setContentType("text/html");
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();

        out.println("<!DOCTYPE html><html>");
        out.println("<head>");
        out.println("<meta charset=\"UTF-8\" />");

        String title = rb.getString("helloworld.title");

        out.println("<title>" + title + "</title>");
        out.println("</head>");
        out.println("<body bgcolor=\"white\">");

        // note that all links are created to be relative. this
        // ensures that we can move the web application that this
        // servlet belongs to to a different place in the url
        // tree and not have any harmful side effects.

        // XXX
        // making these absolute till we work out the
        // addition of a PathInfo issue

        out.println("<a href=\"../helloworld.html\">");
        out.println("<img src=\"../images/code.gif\" height=24 " +
                    "width=24 align=right border=0 alt=\"view code\"></a>");
        out.println("<a href=\"../index.html\">");
        out.println("<img src=\"../images/return.gif\" height=24 " +
                    "width=24 align=right border=0 alt=\"return\"></a>");
        out.println("<h1>" + title + "</h1>");
        out.println("</body>");
        out.println("</html>");
    }
}

HelloWorldExample.java 當中

此class繼承於 HttpServlet

所以是一個客製化的servlet


剛剛在JSP中寫的out就會轉譯成servlet這裡對應的JspWriter 物件
透過
JspWriter 物件內部方法去打印


在IDE自動提示中所看到的 JspWriter
https://docs.oracle.com/javaee/1.4/api/javax/servlet/jsp/JspWriter.html

就可以觀察到out的前身!!







參考資料:
隱含物件
https://openhome.cc/Gossip/ServletJSP/ImplicitObject.html


[JSP/Servlet入門]out 隱含物件 [精華]
https://www.javaworld.com.tw/jute/post/view?bid=6&id=49369&sty=1&tpg=2&age=1

留言

這個網誌中的熱門文章

經得起原始碼資安弱點掃描的程式設計習慣培養(五)_Missing HSTS Header

經得起原始碼資安弱點掃描的程式設計習慣培養(三)_7.Cross Site Scripting(XSS)_Stored XSS_Reflected XSS All Clients

(2021年度)駕訓學科筆試準備題庫歸納分析_法規是非題