JSP基本原理

來源:互聯網
上載者:User

 (一)JSP本質是Servlet,當使用者向指定的Servlet發送請求時,Servlet利用輸出資料流動態產生HTML頁面,包括每一個靜態HTML的標籤和所有在HTML頁面中出現的內容。

(二)JSP頁面由兩部分組成

     靜態部分:標準的HTML標籤,靜態頁面內容,這些內容與靜態HTML頁面相同。

    動態部分:受JAVA 程式控制的內容,這些內容由JAVA程式來動態產生。

(三)編寫一個簡單的JSP頁面步驟

     設定myeclipse或者eclipse中jsp頁面預設的編碼格式:windows->preference->myeclipse->Files and Eitors->JSP    設定Encoding為UTF-8  然後Apply

    <body>
   這是我的第一個JSP頁面<br>
   <%out.println(new Date()); %>
  </body>

解釋:JSP其實就是Servlet的一種簡化,使用JSP時,其實還是使用Servlet,因為Web應用中的每個JSP頁面都會由Servlet容器產生對應的Servlet。

package org.apache.jsp;import javax.servlet.*;import javax.servlet.http.*;import javax.servlet.jsp.*;import java.util.*;public final class index_jsp extends org.apache.jasper.runtime.HttpJspBase    implements org.apache.jasper.runtime.JspSourceDependent {  private static final javax.servlet.jsp.JspFactory _jspxFactory =          javax.servlet.jsp.JspFactory.getDefaultFactory();  private static java.util.Map<java.lang.String,java.lang.Long> _jspx_dependants;  private javax.el.ExpressionFactory _el_expressionfactory;  private org.apache.tomcat.InstanceManager _jsp_instancemanager;  public java.util.Map<java.lang.String,java.lang.Long> getDependants() {    return _jspx_dependants;  }  public void _jspInit() {    _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();    _jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());  }  public void _jspDestroy() {  }  public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)        throws java.io.IOException, javax.servlet.ServletException {    final javax.servlet.jsp.PageContext pageContext;    javax.servlet.http.HttpSession session = null;    final javax.servlet.ServletContext application;    final javax.servlet.ServletConfig config;    javax.servlet.jsp.JspWriter out = null;    final java.lang.Object page = this;    javax.servlet.jsp.JspWriter _jspx_out = null;    javax.servlet.jsp.PageContext _jspx_page_context = null;    try {      response.setContentType("text/html;charset=ISO-8859-1");      pageContext = _jspxFactory.getPageContext(this, request, response,      null, true, 8192, true);      _jspx_page_context = pageContext;      application = pageContext.getServletContext();      config = pageContext.getServletConfig();      session = pageContext.getSession();      out = pageContext.getOut();      _jspx_out = out;      out.write('\r');      out.write('\n');String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";      out.write("\r\n");      out.write("\r\n");      out.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\r\n");      out.write("<html>\r\n");      out.write("  <head>\r\n");      out.write("    <base href=\"");      out.print(basePath);      out.write("\">\r\n");      out.write("    \r\n");      out.write("    <title>My JSP 'index.jsp' starting page</title>\r\n");      out.write("\t<meta http-equiv=\"pragma\" content=\"no-cache\">\r\n");      out.write("\t<meta http-equiv=\"cache-control\" content=\"no-cache\">\r\n");      out.write("\t<meta http-equiv=\"expires\" content=\"0\">    \r\n");      out.write("\t<meta http-equiv=\"keywords\" content=\"keyword1,keyword2,keyword3\">\r\n");      out.write("\t<meta http-equiv=\"description\" content=\"This is my page\">\r\n");      out.write("\t<!--\r\n");      out.write("\t<link rel=\"stylesheet\" type=\"text/css\" href=\"styles.css\">\r\n");      out.write("\t-->\r\n");      out.write("  </head>\r\n");      out.write("  \r\n");      out.write("  <body>\r\n");      out.write("    This is my JSP page. <br>\r\n");      out.write("  </body>\r\n");      out.write("</html>\r\n");    } catch (java.lang.Throwable t) {      if (!(t instanceof javax.servlet.jsp.SkipPageException)){        out = _jspx_out;        if (out != null && out.getBufferSize() != 0)          try { out.clearBuffer(); } catch (java.io.IOException e) {}        if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);      }    } finally {      _jspxFactory.releasePageContext(_jspx_page_context);    }  }}

該servlet原始碼主要包含三個類:

init():初始化JSp/Servlet的方法

destory():銷毀JSP/Servlet之前的方法

service():對使用者請求產生想用的方法。

(四)JSP工作原理的總結

1.JSP檔案必須在JSP伺服器內運行

2.JSP檔案必鬚生成Servlet才能執行

3.每個JSP頁面的第一個訪問者速度很慢,因為必須等待JSP編譯成Servlet

4.JSP頁面的訪問者無需安裝任何用戶端,甚至不需要可以運行java的運行環境,因為JSP頁面輸送到用戶端的是標準的HTML頁面。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.