Java Server Pages(JSP)是J2EE Web層用來動態產生網頁的另一種主要技術。JSP技術是servlet技術的延伸。與servlet相比,JSP提供了一種自然的產生網頁的方法,從下面的一個例子中可以看出來。
本執行個體實現前面servlet篇幅中說明的返回HttpServletRequest的頭資訊,在tomcat中測試通過。代碼如下:
<%@page import="java.util.Enumeration" %>
<html>
<head>
<title>RequestHeaderJSP</title>
</head>
<body bgcolor="white">
<center>
<font color="#009999">
<strong>List of all Headers in JSP Request</strong>
</font>
</center>
<hr>
<h3>Request Line is</h3>
<B>Method:</B><%=request.getMethod()%>
<B>URI:</B><%=request.getRequestURI()%>
<b>Protocol:</b><%=request.getProtocol()%>
<br>
<center><h3>Header Name and Values</h3></center>
<table border="1" align="center">
<tr bgcolor="#99cee6"><th>Name</th><th>Value</th><tr>
<%
Enumeration headerNames = request.getHeaderNames();
while(headerNames.hasMoreElements()){
String headerName = (String)headerNames.nextElement();
%>
<tr><td><%=headerName%></td>
<td><%=request.getHeader(headerName)%></td>
<%}%>
</table>
</body>
</html>
該JSP在第一次請求時被Tomcat翻譯成的servlet代碼如下:
package org.apache.jsp.jsp;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import java.util.Enumeration;
public final class servletRequestHeader_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent {
private static java.util.List _jspx_dependants;
public Object getDependants() {
return _jspx_dependants;
}
public void _jspService(HttpServletRequest request, HttpServletResponse response)
throws java.io.IOException, ServletException {
JspFactory _jspxFactory = null;
PageContext pageContext = null;
HttpSession session = null;
ServletContext application = null;
ServletConfig config = null;
JspWriter out = null;
Object page = this;
JspWriter _jspx_out = null;
PageContext _jspx_page_context = null;
try {
_jspxFactory = JspFactory.getDefaultFactory();
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("/r/n");
out.write("<html>/r/n");
out.write("<head>/r/n");
out.write("<title>RequestHeaderJSP</title>/r/n");
out.write("</head>/r/n");
out.write("<body bgcolor=/"white/">/r/n");
out.write("<center>/r/n");
out.write("<font color=/"#009999/">/r/n");
out.write("<strong>List of all Headers in JSP Request</strong>/r/n");
out.write("</font>/r/n");
out.write("</center>/r/n");
out.write("<hr>/r/n");
out.write("<h3>Request Line is</h3>/r/n");
out.write("<B>Method:</B>");
out.print(request.getMethod());
out.write("/r/n");
out.write("<B>URI:</B>");
out.print(request.getRequestURI());
out.write("/r/n");
out.write("<b>Protocol:</b>");
out.print(request.getProtocol());
out.write("/r/n");
out.write("<br>/r/n");
out.write("<center><h3>Header Name and Values</h3></center>/r/n");
out.write("<table border=/"1/" align=/"center/">/r/n");
out.write("<tr bgcolor=/"#99cee6/"><th>Name</th><th>Value</th><tr>/r/n");
Enumeration headerNames = request.getHeaderNames();
while(headerNames.hasMoreElements()){
String headerName = (String)headerNames.nextElement();
out.write("/r/n");
out.write("<tr><td>");
out.print(headerName);
out.write("</td>/r/n");
out.write("<td>");
out.print(request.getHeader(headerName));
out.write("</td>/r/n");
}
out.write("/r/n");
out.write("</table>/r/n");
out.write("</body>/r/n");
out.write("</html>");
} catch (Throwable t) {
if (!(t instanceof SkipPageException)){
out = _jspx_out;
if (out != null && out.getBufferSize() != 0)
out.clearBuffer();
if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
}
} finally {
if (_jspxFactory != null) _jspxFactory.releasePageContext(_jspx_page_context);
}
}
}
HttpJspBase是Tomcat中所有由JSP產生的servlet程式的抽象父類,它由Tomcat的JSP處理構件在Web程式運行時使用,JSP編程人員不需要使用這個類。
JSP中的隱含對象
在前面的代碼中可以看到使用到了request及其幾個方法。request是一個隱含預定義對象,不需要在JSP代碼中手動建立。JSP中還包含以下這些隱含對象。
1.application:代表從servlet configuration獲得的javax.servlet.ServletContext對象。
2.session:代表javax.servlet.http.HttpSession對象。
3.request:代表javax.servlet.HttpServletRequest對象。
4.response:代表javax.servlet.HttpServletResponse對象。
5.config:代表javax.servlet.ServletConfig對象。
6.page:代表javax.servlet.jsp.HttpJspPage對象。對應JSP產生的servlet。
7.pageContext:對應javax.servlet.jsp.PageContext。用於提供共用page資訊的API。
8.out:代表javax.servlet.jsp.JspWriter對象,用於輸出響應結果。
9.exception:代表java.lang.Throwable對象,只有錯誤處理JSP可以獲得。