javascript
JSP 基础(一)
JavaServletPage(JSP)
一 JSP簡介
Servlet的缺陷 –Servlet的編碼、部署和調(diào)試任務(wù)繁瑣 –生成動態(tài)網(wǎng)頁繁瑣,不利于項目分工 為了彌補Servlet的這些缺陷,SUN公司在Servlet的基礎(chǔ)上推出了JSP技術(shù)作為解決方案 采用JSP技術(shù)編寫動態(tài)頁面 ? –由HTML語句和嵌套在其中的Java代碼組成的一個普通文本文件 –JSP頁面的擴展名必須為.jsp二 JSP運行機制與生命周期
JSP的執(zhí)行包括7個階段
?2.1 JSP頁面翻譯階段:Web容器第一次接收到某個JSP頁面的請求后,首先把自動將該頁面翻譯成Servlet代碼。
??
1 <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> 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 <% 25 out.print("Hello Jsp"); 26 %> 27 </body> 28 </html>執(zhí)行了上面的這個.jsp后,會在D:\apache-tomcat-6.0.20\work\Catalina\localhost\項目名\org\apache\jsp下發(fā)現(xiàn)一個index_jsp.class文件和一個index_jsp.java文件
打開java文件
1 package org.apache.jsp; 2 3 import javax.servlet.*; 4 import javax.servlet.http.*; 5 import javax.servlet.jsp.*; 6 import java.util.*; 7 8 public final class index_jsp extends org.apache.jasper.runtime.HttpJspBase 9 implements org.apache.jasper.runtime.JspSourceDependent { 10 11 private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory(); 12 13 private static java.util.List _jspx_dependants; 14 15 private javax.el.ExpressionFactory _el_expressionfactory; 16 private org.apache.AnnotationProcessor _jsp_annotationprocessor; 17 18 public Object getDependants() { 19 return _jspx_dependants; 20 } 21 22 public void _jspInit() { 23 _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory(); 24 _jsp_annotationprocessor = (org.apache.AnnotationProcessor) getServletConfig().getServletContext().getAttribute(org.apache.AnnotationProcessor.class.getName()); 25 } 26 27 public void _jspDestroy() { 28 } 29 30 public void _jspService(HttpServletRequest request, HttpServletResponse response) 31 throws java.io.IOException, ServletException { 32 33 PageContext pageContext = null; 34 HttpSession session = null; 35 ServletContext application = null; 36 ServletConfig config = null; 37 JspWriter out = null; 38 Object page = this; 39 JspWriter _jspx_out = null; 40 PageContext _jspx_page_context = null; 41 42 43 try { 44 response.setContentType("text/html;charset=ISO-8859-1"); 45 pageContext = _jspxFactory.getPageContext(this, request, response, 46 null, true, 8192, true); 47 _jspx_page_context = pageContext; 48 application = pageContext.getServletContext(); 49 config = pageContext.getServletConfig(); 50 session = pageContext.getSession(); 51 out = pageContext.getOut(); 52 _jspx_out = out; 53 54 out.write('\r'); 55 out.write('\n'); 56 57 String path = request.getContextPath(); 58 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 59 60 out.write("\r\n"); 61 out.write("\r\n"); 62 out.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\r\n"); 63 out.write("<html>\r\n"); 64 out.write(" <head>\r\n"); 65 out.write(" <base href=\""); 66 out.print(basePath); 67 out.write("\">\r\n"); 68 out.write(" \r\n"); 69 out.write(" <title>My JSP 'index.jsp' starting page</title>\r\n"); 70 out.write("\t<meta http-equiv=\"pragma\" content=\"no-cache\">\r\n"); 71 out.write("\t<meta http-equiv=\"cache-control\" content=\"no-cache\">\r\n"); 72 out.write("\t<meta http-equiv=\"expires\" content=\"0\"> \r\n"); 73 out.write("\t<meta http-equiv=\"keywords\" content=\"keyword1,keyword2,keyword3\">\r\n"); 74 out.write("\t<meta http-equiv=\"description\" content=\"This is my page\">\r\n"); 75 out.write("\t<!--\r\n"); 76 out.write("\t<link rel=\"stylesheet\" type=\"text/css\" href=\"styles.css\">\r\n"); 77 out.write("\t-->\r\n"); 78 out.write(" </head>\r\n"); 79 out.write(" \r\n"); 80 out.write(" <body>\r\n"); 81 out.write(" "); 82 83 out.print("Hello Jsp"); 84 85 out.write("\r\n"); 86 out.write(" </body>\r\n"); 87 out.write("</html>\r\n"); 88 } catch (Throwable t) { 89 if (!(t instanceof SkipPageException)){ 90 out = _jspx_out; 91 if (out != null && out.getBufferSize() != 0) 92 try { out.clearBuffer(); } catch (java.io.IOException e) {} 93 if (_jspx_page_context != null) _jspx_page_context.handlePageException(t); 94 } 95 } finally { 96 _jspxFactory.releasePageContext(_jspx_page_context); 97 } 98 } 99 }?
?2.2 JSP頁面編譯階段:index_jsp.java文件編譯成index_jsp.class文件。
?2.3 JSP頁面類裝載階段:Web容器裝載新生成的Servlet類。
?2.4 JSP頁面實例化階段:Web容器創(chuàng)建實例。
?2.5 JSP頁面初始化階段:web容器調(diào)用Servlet示例的_jspInit()方法。
?2.6 JSP頁面服務(wù)階段:容器創(chuàng)建一個新線程來處理客戶的請求,Servlet對象的_jspService()方法運行。
?2.7 JSP頁面的銷毀:servlet對象的_jspDestory()方法。?
如果一個Web應(yīng)用程序中包含有JSP頁面,部署這個應(yīng)用時,在JSP生命周期中,整個翻譯和編譯步驟只發(fā)生一次。JSP一旦被翻譯和編譯,就像其他Servlet一樣了。其實只有在第一次執(zhí)行的時候有性能上的差別。
JSP通常用于簡化創(chuàng)建產(chǎn)生文本的Servlet,二Servlet更適合用于發(fā)送原生字節(jié)到客戶端或所需要用java源代碼完全控制源代碼的場合。
三 JSP語法與語義
? ?JSP網(wǎng)頁主要有元素(Element)和模板數(shù)據(jù)(Template Data)兩部分組成。
? 元素可以分成三個不同的類別:腳本元素,指令,動作。(下面進行解釋)?
四 腳本元素
?在JSP中有三種不同類型的腳本元素:scriptlet,腳本表達式,聲明。
? 4.1 Scriptlet? ? ?
JSP腳本片斷(scriptlet)用于在JSP頁面中編寫多行Java代碼。語法格式:
<% ?多行java代碼 ?%>
在<% %>中可以定義變量、編寫語句,不能定義方法。
eg:
1 <% 2 /*聲明變量*/ 3 int sum=0; 4 5 /*編寫語句*/ 6 for (int i=1;i<=100;i++){ 7 sum+=i; 8 } 9 out.println("<h1>Sum="+sum+"</h1>"); 10 11 out.print("Hello Jsp"); 12 %>?
? 4.2 腳本表達式
?JSP腳本表達式(expression)用于將程序數(shù)據(jù)輸出到客戶端。語法:
? ? ? ? ? ? ? ?<%= 變量或表達式 %>
eg:
?
1 <body> 2 3 <% 4 int[] balls = new int[6]; 5 Random r = new Random(); 6 for (int i = 0; i < 6;) { 7 boolean flag = true; 8 int temp = r.nextInt(33) + 1; 9 for (int j = 0; j <= i; j++) { 10 if (balls[j] == temp) { 11 flag = false; 12 break; 13 } 14 } 15 if (flag) { 16 balls[i] = temp; 17 i++; 18 %> 19 <div class="red"><%=temp%></div> 20 21 <% 22 } 23 } 24 %> 25 <div class="blue"><%=r.nextInt(16) + 1%></div> 26 </body>?
?
?
?JSP引擎在翻譯腳本表達式時,會將程序數(shù)據(jù)轉(zhuǎn)成字符串,然后在相應(yīng)位置用out.print(…) 將數(shù)據(jù)輸給客戶端。
JSP腳本表達式中的變量或表達式后面不能有分號(;)
4.3 聲明
? ?JSP頁面中編寫的所有代碼,默認會翻譯到servlet的service方法中, 而Jsp聲明中的java代碼被翻譯到_jspService方法的外面。語法:
<%!?java代碼 ?%>
所以,JSP聲明可用于定義JSP頁面轉(zhuǎn)換成的Servlet程序的靜態(tài)代碼塊、成員變量和方法?。?
多個靜態(tài)代碼塊、變量和函數(shù)可以定義在一個JSP聲明中,也可以分別單獨定義在多個JSP聲明中。
JSP隱式對象的作用范圍僅限于Servlet的_jspService方法,所以在JSP聲明中不能使用這些隱式對象。
可以在JSP程序中聲明一個或多個變量。但是每一個聲明語句都必須以分號結(jié)束
eg:
1 <%! String s="hello"; %> 2 <%! int a,b,c; %> 3 <%! java.util.Date date=new java.util.Date(); %>?
1 <%! 2 public void method(){ 3 4 } 5 %>?
五 注釋?
六 JSP指令
七 JSP標準動作
八 JSP隱式對象
?
總結(jié)
- 上一篇: EF使用CodeFirst方式生成数据库
- 下一篇: ExtJS4.2学习(21)动态菜单与表