JSP筆記01——基礎文法

來源:互聯網
上載者:User

標籤:null   ceo   work   getattr   height   time   lease   end   static   

JSP ————> servlet

我的第1個Java Web應用程式——index.jsp

我的第2個Java Web應用程式——welcome-file

我的第3個Java Web應用程式——conf/server.xml

我的第4個Java Web應用程式——jsp page

我的第5個Java Web應用程式——jsp 指令碼

我的第6個Java Web應用程式——jsp 聲明

我的第7個Java Web應用程式——JSP 運算式 

我的第8個Java Web應用程式——JSP頁面生命週期

我的第8個Java Web應用程式——我的乘法口訣表!

 

1

<!doctype html><html lang="en"> <head>  <meta charset="UTF-8">  <meta name="Generator" content="EditPlus®">  <meta name="Author" content="">  <meta name="Keywords" content="">  <meta name="Description" content="">  <title>Document</title> </head> <body>  <h1>???????</h1> </body></html>
<?xml version="1.0" encoding="ISO-8859-1" ?><web-app xmlns="http://java.sun.com/xml/ns/j2ee"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"         version="2.4"></web-app>

 

2

<?xml version="1.0" encoding="ISO-8859-1" ?><web-app xmlns="http://java.sun.com/xml/ns/j2ee"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"         version="2.4">         <welcome-file-list>             <welcome-file>/index.jsp</welcome-file>         </welcome-file-list></web-app>

 

3

 

4

jsp=java server page

 

 

<%@ page language="java" import="java.util.*" contentType="text/html;charset=utf-8" %><html>    <body>        <h1 align="center">啤酒推薦</h1>        <p>        <%            List styles = (List)request.getAttribute("styles");            Iterator it = styles.iterator();            while (it.hasNext()) {                out.print("<br>try: " + it.next());            }        %>    </body></html>

 

 

5

<%@ page language="java" import="java.util.*" contentType="text/html;charset=utf-8" %><html>    <body>        <h1 align="center">啤酒推薦</h1>        <p>        <%            List styles = (List)request.getAttribute("styles");            Iterator it = styles.iterator();            while (it.hasNext()) {                out.print("<br>try: " + it.next());            }        %>        <%            out.print("<br /><br />今天天氣貌似不錯!");            // out是一個內建的對象!        %>    </body></html>

 

6

兩種聲明方式的差異。參考:http://www.cnblogs.com/zengmiaogen/archive/2012/04/15/2450414.html

我們既可以在<%%>中聲明變數,也可以在<%!%>中聲明變數,

那麼,這兩種聲明方式有什麼區別呢?事實上,<%!%>相當於類的執行個體域,而<%%>相當於方法體。

// TO-DO

 

7

<%@ page language="java" import="java.util.*" contentType="text/html;charset=utf-8" %><html>    <body>        <p>this is a simple test</p>        <%!            String words = "this is a simple test.";            int sum(int x, int y) {                return x + y;            }        %>        <p>words=<%=words%></p>        <p>3+4=<%=sum(3,4)%></p>    </body></html>

 

8

 

當我們訪問jsp頁面的時候,jsp引擎(web容器)首先判斷是否是第一次請求...

 

這個時候我就想,要是可以直接看到它編譯出來的位元組碼就好了,那麼到哪裡查看呢?——答案是在tomcat的work目錄下,不僅有位元組碼,還有原始碼,我們查看一下上一節的代碼。

/* * Generated by the Jasper component of Apache Tomcat * Version: Apache Tomcat/7.0.77 * Generated at: 2017-06-09 10:07:20 UTC * Note: The last modified time of this file was set to *       the last modified time of the source file after *       generation to assist with modification tracking. */package org.apache.jsp;import javax.servlet.*;import javax.servlet.http.*;import javax.servlet.jsp.*;import java.util.*;public final class test_jsp extends org.apache.jasper.runtime.HttpJspBase    implements org.apache.jasper.runtime.JspSourceDependent {            String words = "this is a simple test.";            int sum(int x, int y) {                return x + y;            }          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 volatile javax.el.ExpressionFactory _el_expressionfactory;  private volatile org.apache.tomcat.InstanceManager _jsp_instancemanager;  public java.util.Map<java.lang.String,java.lang.Long> getDependants() {    return _jspx_dependants;  }  public javax.el.ExpressionFactory _jsp_getExpressionFactory() {    if (_el_expressionfactory == null) {      synchronized (this) {        if (_el_expressionfactory == null) {          _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();        }      }    }    return _el_expressionfactory;  }  public org.apache.tomcat.InstanceManager _jsp_getInstanceManager() {    if (_jsp_instancemanager == null) {      synchronized (this) {        if (_jsp_instancemanager == null) {          _jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());        }      }    }    return _jsp_instancemanager;  }  public void _jspInit() {  }  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=utf-8");      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\n");      out.write("\r\n");      out.write("<html>\r\n");      out.write("    <body>\r\n");      out.write("    \t<p>this is a simple test</p>\r\n");      out.write("    \t");      out.write("\r\n");      out.write("    \t<p>words=");      out.print(words);      out.write("</p>\r\n");      out.write("    \t<p>3+4=");      out.print(sum(3,4));      out.write("</p>\r\n");      out.write("    </body>\r\n");      out.write("</html>");    } catch (java.lang.Throwable t) {      if (!(t instanceof javax.servlet.jsp.SkipPageException)){        out = _jspx_out;        if (out != null && out.getBufferSize() != 0)          try {            if (response.isCommitted()) {              out.flush();            } else {              out.clearBuffer();            }          } catch (java.io.IOException e) {}        if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);        else throw new ServletException(t);      }    } finally {      _jspxFactory.releasePageContext(_jspx_page_context);    }  }}

 

9

<%@ page language="java" import="java.util.*" contentType="text/html;charset=utf-8" %><html>    <body>        <p>這是我的乘法口訣表!</p>        <%            for (int i = 1; i <= 9; ++i) {                for (int j = 1; j <= i; ++j) {                    out.print(i + " * " + j + " = " + i*j + "   ");                }                out.print("<br />");            }        %>    </body></html>

 看完視頻後修改了一下,&nbsp;是空格預留位置

<%@ page language="java" import="java.util.*" contentType="text/html;charset=utf-8" %><html>    <body>        <p>這是我的乘法口訣表!</p>                <%!            void printTable() {                for (int i = 1; i <= 9; ++i) {                    for (int j = 1; j <= i; ++j) {                        out.print(i + " * " + j + " = " + i*j + "&nbsp;&nbsp;&nbsp;&nbsp;");                    }                    out.print("<br />");                }            }        %>        <%            printTable();        %>    </body></html>

 

JSP筆記01——基礎文法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.