1. 程式人生 > >java基礎80 JSP基礎知識點(網頁知識)

java基礎80 JSP基礎知識點(網頁知識)

1、jsp的引入

    Servlet的作用:用java語言開發動態資源技術!!!
    Jsp的作用:用java語言(+html語言)開發的動態資源技術!!!     Jsp就是servlet

問題:為什麼jsp就是servlet?
因為:Jsp翻譯成java檔案
public final class hello_jsp extends org.apache.jasper.runtime.HttpJspBase implements org.apache.jasper.runtime.JspSourceDependent
即:
org.apache.jasper.runtime.HttpJspBase類:
extends org.apache.jasper.runtime.HttpServlet implemets javax.servlet.jsp.HttpjspPage
結論:jsp就是一個servlet程式,Servlet的技術可以用在jsp中,Jsp技術並不是全部使用於servlet

2、jsp的特點

    1)jsp的執行必須交給tomcat伺服器。  Tomcat的work目錄:tomcat伺服器存放jsp執行時的臨時檔案
    2)jsp頁面既可以寫html程式碼,也可以java程式碼。   (html頁面不能寫java程式碼,而jsp頁面是可以寫java程式碼)

3、jsp的執行過程

    1、訪問hello.jsp頁面,tomcat掃描jsp檔案,apache-tomcat-7.0.73\work把jsp檔案翻譯成java原始檔:hello.jsp ----> hello_jsp.java(翻譯)
    2、tomcat伺服器把java原始檔編譯成class位元組碼檔案:hello_jsp.java ---> hello_jsp.class(編譯)
    3、tomcat伺服器構造hello_jsp類的物件
    4、tomcat伺服器呼叫hello_jsp類裡面的方法,返回內容顯示到瀏覽器

 1 <%@ page language="java" import
="java.util.*,java.text.*" pageEncoding="utf-8" %> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>My JSP 'hello.jsp' starting page</title> 13 14 <meta http-equiv="pragma" content="no-cache"> 15 <meta http-equiv="cache-control" content="no-cache"> 16 <meta http-equiv="expires" content="0"> 17 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 18 <meta http-equiv="description" content="This is my page"> 19 <!-- 20 <link rel="stylesheet" type="text/css" href="styles.css"> 21 --> 22 23 </head> 24 25 <body> 26 <% 27 //這裡寫的都是java程式碼 28 //獲取當前時間 29 SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); 30 String curTime=format.format(new Date()); 31 //輸出內容到瀏覽器(下面兩個效果是一樣的) 32 response.getWriter().write(curTime); 33 //out.write(curTime); 34 %> 35 </body> 36 </html>

注意:jsp檔案被修改或者jsp臨時檔案被刪除了,重新走編譯1和編譯2的過程

4、Jsp的生命週期

Jsp的生命週期 Servlet的生命週期
1、翻譯:jsp-java檔案
2、編譯:java檔案-class檔案(servlet程式
3、構造方法(第一次方法)
4、Init方法(第一次訪問):jspinit()
5、Service方法:jspService()
6、Destroy方法:jspDestory();
1、構造方法(第一次訪問)
2、Init方法(第一次訪問)
3、Service方法
4、Destroy方法

5、jsp的語法

5.1、jsp的表示式

    語法:<%=變數或表示式%>
    作用:向瀏覽器輸出變數值或者表示式計算的結果
    注意:
        1)表示式的原理就是翻譯成了out.print(“變數”);通過該方法向瀏覽器寫出內容
        2)表示式後面不需要帶分號結束

5.2、jsp的指令碼

    語法:<%java程式碼%>
    作用:執行java程式碼
    注意:原理把指令碼中的java程式碼原封不動拷貝到jspService方法中執行

5.3、jsp的宣告

    語法:<%!變數或方法%>
    作用:宣告jsp的變數或方法
    注意:變數翻譯成成員變數,方法翻譯成成員方法.

5.4、jsp的註釋

    語法:<%-- jsp註釋--%>
    注意:html註釋會被翻譯和執行,而jsp的註釋不能被翻譯和執行

6、jsp中的三大指令

6.1、include指令

    作用:在當前頁面用於包含其他頁面
    語法:%@include file=”common/header.jsp”%
    注意:
        1、原理是把被包含的頁面(header.jsp)的內容翻譯到包含頁面(index.jsp),合併翻譯成一個java原始檔,再編譯執行,這種包含叫靜態包含(原始碼包含)
        2、如果使用靜態包含,被包含的頁面不需要出現全域性的html標籤(原始碼包含)

6.2、page指令

1 <%@ page language="java" import="java.util.*,java.text.*" pageEncoding="utf-8" contentType="text/html; charset=utf-8" %>

   解析:

        language="java"  //告訴我們的服務使用什麼語言來翻譯jsp檔案
        import="java.util.*,java.text.*"  //告訴伺服器java檔案使用什麼包,匯入包,多個包之間用逗號分割
        pageEncoding="utf-8"  //告訴伺服器使用什麼編碼翻譯jsp檔案(成java檔案)
        contentType="text/html; charset=utf-8"  //伺服器傳送瀏覽器的資料型別和內容編碼

        Errorpage=”error.page” //錯誤頁面
        IsErrorpage=”fasle”;  //判斷是不是錯誤頁面
        Buffer=”8kb”  //頁面緩衝區的大小
        Session=”true”  //是否能夠使用session
        IsElIgnored=”false”   //是否忽略EL表示式

6.3、taglib指令

附錄1

 1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8" %>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11     
12     <title>My JSP 'script.jsp' starting page</title>
13     
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">    
17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18     <meta http-equiv="description" content="This is my page">
19     <!--
20     <link rel="stylesheet" type="text/css" href="styles.css">
21     -->
22   </head>
23   
24   <body>
25            <!--jsp表示式-->
26            <%
27                //變數
28                String name="jack";
29                int a=10;
30                int b=20;
31                //System.out.println(name);
32             %>
33             <%=name %>
34             <br/>
35             <%=(a-b) %>
36             <br/>
37          
38             <!--jsp指令碼-->
39             <%
40             //生成隨機數
41             Random ran=new Random();
42             float num=ran.nextFloat();
43             %>
44             <%=num %>
45         <hr/> 
46              
47         <!--練習:使用指令碼和html程式碼顯示99乘法表-->
48          <% 
49              String[] color={"red","green","blue"};
50                  for(int i=1;i<=9;i++){
51                      for(int j=1;j<=i;j++){
52                          out.print("<span style='color:"+color[i%3]+"'>"+j+"*"+i+"="+(i*j)+"&nbsp</span>");                 
53         %>
54                         <%=i%> x <%=j%>=<%=(i*j)%>&nbsp; <!-- 這行程式碼和上面的out.print();是一樣的,都是向頁面輸出 -->    
55                     <% 
56                      }
57                      %>
58                      <br/>
59                  <% 
60                  }
61                 %> 
62                 
63          <!--html的註釋方式-->
64          <%--jsp的註釋--%>
65          <%!
66              //變數
67              String name="rose";
68              
69              /*
70              jsp宣告中不能重複定義翻譯一些方法
71               public void _jspInit() {}
72              */
73          %>
74   </body>
75 </html>

附錄2

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8" buffer="1kb" %>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11     
12     <title>My JSP 'MyJsp.jsp' starting page</title>
13     
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">    
17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18     <meta http-equiv="description" content="This is my page">
19     <!--
20     <link rel="stylesheet" type="text/css" href="styles.css">
21     -->
22   </head>
23   <body>
24        <%
25           //方法1:頂部設定緩衝區的大小為1Kb(buffer="1kb"26            for(int i=1;i<=500;i++){
27            out.write("123");
28            }
29            //檢視緩衝區大小
30            System.out.println("當前緩衝區的大小:"+out.getBufferSize());
31            //檢視緩衝區剩餘大小
32            System.out.println("剩餘緩衝區大小:"+out.getRemaining());
33            //方法2:重新整理快取
34            //out.flush();
35         
36            response.getWriter().write("a"); //解析:方法1或2,都能讓123先輸出來,最後輸出a;如果兩者都去掉,那麼先輸出a,後輸出123.
37         %>
38   </body>
39 </html>

附錄3

web.xml 配置檔案

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app version="2.5" 
 3     xmlns="http://java.sun.com/xml/ns/javaee" 
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 5     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 6     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 7     <error-page>
 8         <error-code>500</error-code>
 9         <location>/common/500.jsp</location>
10     </error-page>
11       <error-page>
12           <error-code>404</error-code>
13           <location>/common/404.html</location>
14       </error-page>
15 </web-app>

使用者當前訪問的頁面

 1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11     
12     <title>My JSP 'page.jsp' starting page</title>
13     
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">    
17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18     <meta http-equiv="description" content="This is my page">
19     <!--
20     <link rel="stylesheet" type="text/css" href="styles.css">
21     -->
22   </head>
23   
24   <body>
25         <% 
26         String name=null;
27         name.charAt(1); //這裡是一個錯誤; charAt:這是一個字串,括號裡填1(整形)的話 就錯了
28         %>     
29   </body>
30 </html>

使用者訪問後,處理錯誤資訊的頁面(顯示給使用者看的頁面)

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"
 2 isErrorPage="true"  <!-- 這裡要宣告為錯誤頁面 -->
 3 %>
 4 
 5 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 6 <html>
 7   <head>
 8     <title>錯誤頁面500.jsp</title>
 9   </head>
10   
11   <