對于Servlet 學習過後,那麼接觸到JSP,那麼可以發現JSP程式設計則會突然喜愛上,因為JSP不僅僅也是一個servlet,還能幫我們直接從servlet的容器(Tomcat)為我們直接生成對應的servlet代碼。至于更加詳細的介紹大家可以自行baidu~goole
下面讓我們首先寫一個JSP,然後大家就明白了其用途:
1. 首先我們編寫如下一個jsp檔案,這裡起名 himi.jsp,其内容如下:
<html>
<head>
</head>
<body>
<%
out.println("HelloWorld!");
%>
</body>
</html>
OK,将其之間放置我們上一篇建立的webapp根目錄即可;
然後嘗試通路一下這個himi.jsp 觀察如下:(這裡Himi在tomcat目錄下建立的webapp叫“MyWebApp”)
<a href="http://www.himigame.com/wp-content/uploads/2012/05/13.png"></a>
OK,發現Servlet 傳回給Client端一個“ Hello World! ”字元串
大家先到tomcat目錄下的/work/Catalina/localhost/MyWebApp/org/apache/jsp檔案夾下我們可以看到如下兩個檔案:
a)himi_jsp.class b)himi_jsp.java
不二話打開 himi_jsp.java 如下:
/*
* Generated by the Jasper component of Apache Tomcat
* Version: Apache Tomcat/7.0.27
* Generated at: 2012-05-24 16:07:57 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.*;
public final class himi_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");
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("<html>\n");
out.write("\t<head>\n");
out.write("\t</head>\n");
out.write("\n");
out.write("\t<body>\n");
out.write("\t\t");
out.write("\t</body>\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 { out.clearBuffer(); } catch (java.io.IOException e) {}
if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
}
} finally {
_jspxFactory.releasePageContext(_jspx_page_context);
}
}
看到這裡大家應該明白了,當用戶端通路jsp後,jsp預設生成一個 servlet 并且編譯位元組碼,(當然是第一次通路生成并編譯,後續通路就不會再次編譯了);
JSP 文法:
拐回頭來看 himi.jsp 代碼:
<a href="http://blog.51cto.com/xiaominghimi/969797#">?</a>
1
2
3
4
5
6
7
8
9
10
<code><</code><code>html</code><code>></code>
<code> </code><code><</code><code>head</code><code>></code>
<code> </code><code></</code><code>head</code><code>></code>
<code> </code><code><</code><code>body</code><code>></code>
<code> </code><code><%</code>
<code> </code><code>out.println("HelloWorld!");</code>
<code> </code><code>%></code>
<code> </code><code></</code><code>body</code><code>></code>
<code></</code><code>html</code><code>></code>
【JSP 自動生成的servlet代碼,其實内置了一些對象,如下:】
out 就是PrintWriter;
req 就是HttpServletRequest,
resp 就是HttpServletResponse
1. <% ... %> 兩個符号之間是我們的servlet代碼;也就是程式代碼塊,在其中可以任意放置java代碼;
也就是說用戶端通路jsp後預設将我們himi.jsp中的内容轉換成servlet;
2. 其中注視的方式:
1) <%--... ...--%> 2) <%//... ...%> 3) <%/*... ...*/%>
要注意:以上注視都是伺服器端的注視方式,如果你在jsp使用用戶端注視方式:
<!-- ... --> 那要格外注意!如果你在用戶端注視方式加入<%...%>如下:
<!-- <% %> -->中是無法阻止伺服器端的代碼!隻能保證<%..%>不保證在用戶端顯示出來罷了;
3. <%! ... %> 表示将其中的代碼成為自動生成servlet類的成員變量或者成員函數;
4. <%= ... %> 等同于 <% out.println(); %>
5. request.getParameter("xxx"); 可以動态擷取到xxx這個屬性的參數;
例如傳入一個 xxx.jsp?xxx=yyy
6. <%@Directive 屬性="屬性值"%> 編譯期間的指令;
Directive 常用: a) page b) include c) taglib
JSP伺服器端和用戶端代碼互相調用方法:
示例代碼段1:
<code>xx.jsp</code>
<code><</code><code>script</code><code>></code>
<code> </code><code><%out.println("var a =10");%></code>
<code></</code><code>script</code><code>></code>
示例代碼段2:
<code> </code><code>var a =' <% out.print(23); %> ';</code>
注意 :
1)JSP伺服器端代碼<%...%>總執行在Client端代碼之前
2)代碼段2 要注意 “ print(23) ”而不是“println(23)” 因為println是會換行造成用戶端文法錯誤!;
本文轉自 xiaominghimi 51CTO部落格,原文連結:http://blog.51cto.com/xiaominghimi/969797,如需轉載請自行聯系原作者