JSP (java服務器頁面)
servlet
創建第一個項目
1. 選擇Dynamic Web Project , 模板版本選擇2.5
2. 項目的目錄結構, META-INF不用理解 , WEB-INF下lib存放jar包與web.xml文件(必要的配置),服務器響應頁面在WebRoot下,如圖中index.jsp.
3. tomcat添加項目,啟動tomcat .
JSP基本語法
例如上例中的index.jsp文件:
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 'index.jsp' starting page</title> 13 <meta http-equiv="pragma" content="no-cache"> 14 <meta http-equiv="cache-control" content="no-cache"> 15 <meta http-equiv="expires" content="0"> 16 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 17 <meta http-equiv="description" content="This is my page"> 18 <!-- 19 <link rel="stylesheet" type="text/css" href="styles.css"> 20 --> 21 </head> 22 23 <body> 24 This is my JSP page. <br> 25 </body> 26 </html>
1.在開始的page中:
language用來設置腳本語言,jsp中只有java一種
Language : 用來定義要使用的腳本語言
contentType:定義 JSP 字符的編碼和頁面響應的 MIME 類型
pageEncoding:Jsp 頁面的字符編碼
2.scriptlet 標簽
通過 scriptlet 標簽我們可以在 Jsp 里嵌入 Java 代碼;
第一種:<%! %>
我們可以在里面定義全局變量、方法、類;
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 'index.jsp' starting page</title> 13 <meta http-equiv="pragma" content="no-cache"> 14 <meta http-equiv="cache-control" content="no-cache"> 15 <meta http-equiv="expires" content="0"> 16 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 17 <meta http-equiv="description" content="This is my page"> 18 <!-- 19 <link rel="stylesheet" type="text/css" href="styles.css"> 20 --> 21 22 <%! 23 String str = "全局變量" ; 24 %> 25 <%! 26 public void func(){ 27 System.out.println("全局方法"); 28 } 29 %> 30 <%! 31 class My{ 32 private int a = 1; 33 public void f(){ 34 System.out.println("全局類"); 35 } 36 } 37 %> 38 </head> 39 40 <body> 41 This is my JSP page. <br> 42 </body> 43 </html>
將上述jsp放入tomcat編譯之后:
在tomcat的work目錄下尋找到jsp中java代碼的編譯文件
第二種:<% %>
我們可以在里面定義局部變量、編寫語句;
與第一種相差不多,具體可以嘗試.
第三種:<%= %>
我們可以在里面輸出一個變量或一個具體內容;
相當於在頁面中輸出一段信息,如 <%=b%> 會輸出變量b
3. Jsp 注釋
<!-- --> Html 注釋 客戶端(在瀏覽器查看網頁源碼時)可見
<%-- --%> Jsp 注釋 客戶端(在瀏覽器查看網頁源碼時)不可見
// java 單行注釋
/*
*/ java 多行注釋
4. Jsp包含指令
<%@ include file=”要包含的文件”%>
在同級目錄下創建MyHtml.html文件,內容只有:
1 <body> 2 My Html page<br> 3 </body>
在index.jsp中添加靜態包含指令代碼:
1 <body> 2 <%@ include file="MyHtml.html" %> 3 </body>
啟動 tomcat :
<jsp:include page=”要包含的文件”>
基本操作與上述相差不多,只不過 靜態包含先把包含文件加入,再編譯運行, 動態包含是先編譯,在將包含文件插入 . 在開發中應多使用動態包含.
5. Jsp 跳轉指令
<jsp:forward page=" ">
<jsp:param value=”” name=”” />
</jsp:forward>
服務器內部跳轉,可帶參數;
例如 , 從form.jsp中帶參數跳轉至target.jsp 中
form.jsp :
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 'form.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 <jsp:forward page="target.jsp"> 27 <jsp:param value="HelloWorld" name="forward"/> 28 </jsp:forward> 29 </body> 30 </html>
target.jsp :
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 'target.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 get form jsp: <%=request.getParameter("forward") %> 27 </body> 28 </html>
啟動tomcat ,訪問 http://127.0.0.1:8000/HelloWorld/form.jsp (HelloWorld是項目名稱)