javaWeb學習日記_18:JSP基礎
1. jsp的作用:
* Servlet:
> 缺點:不適合設定html響應體,需要大量的response.getWriter().print("<html>")
> 優點:動態資源,可以程式設計。
* html:
> 缺點:html是靜態頁面,不能包含動態資訊
> 優點:不用為輸出html標籤而發愁
* jsp(java server pages):
> 優點:在原有html的基礎上新增java指令碼,構成jsp頁面。
2. jsp和Servlet的分工:
* JSP:
> 作為請求發起頁面,例如顯示錶單、超連結。
> 作為請求結束頁面,例如顯示資料。
* Servlet:
> 作為請求中處理資料的環節。
3. jsp的組成
* jsp = html + java指令碼 + jsp標籤(指令)
* jsp中無需建立即可使用的物件一共有9個,被稱之為9大內建物件。例如:request物件、out物件
* 3種java指令碼:
> <%...%>:java程式碼片段(常用),用於定義0~N條Java語句!方法內能寫什麼,它就可以放什麼!
> <%=...%>:java表示式,用於輸出(常用),用於輸出一條表示式(或變數)的結果。response.getWriter().print( ... );這裡能放什麼,它就可以放什麼!
> <%!...%>:宣告,用來建立類的成員變數和成員方法(基本不用,但容易被考到),類體中可以放什麼,它就可以放什麼!
案例:演示jsp與servlet分工!
form.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'form.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="/day11_1/AServlet" method="post"> 整數1:<input type="text" name="num1"/><br/> 整數2:<input type="text" name="num2"/><br/> <input type="submit" value="提交"/> </form> </body> </html>
AServlet.java
package cn.itcast.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class AServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 獲取引數
String s1 = request.getParameter("num1");
String s2 = request.getParameter("num2");
// 轉換成int型別
int num1 = Integer.parseInt(s1);
int num2 = Integer.parseInt(s2);
// 運算
int sum = num1 + num2;
// 把結果儲存到request域中
request.setAttribute("result", sum);
// 轉換到result.jsp
request.getRequestDispatcher("/jia/result.jsp").forward(request, response);
}
}
result.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'result.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
Integer result = (Integer)request.getAttribute("result");
%>
<%=result %>
</body>
</html>
4. jsp原理(理解)
* jsp其實是一種特殊的Servlet
> 當jsp頁面第一次被訪問時,伺服器會把jsp編譯成java檔案(這個java其實是一個servlet類)
> 然後再把java編譯成.class
> 然後建立該類物件
> 最後呼叫它的service()方法
> 第二次請求同一jsp時,直接呼叫service()方法。
* 在tomcat的work目錄下可以找到jsp對應的.java原始碼。
* 檢視jsp對應java檔案:
> java指令碼
> html
5. jsp註釋
* <%-- ... --%>:當伺服器把jsp編譯成java檔案時已經忽略了註釋部分!
<!--fdsafdsa-->:html註釋