Java开发学习笔记之十一:开始JSP之旅

时间:2021-09-29 13:58:57

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可以获得。