日期:2014-05-16 浏览次数:20471 次
JSP是Servlet的扩展,在没有JSP之前,就已经出现了Servlet技术。Servlet是利用输出流动态生成HTML页面,包括每一个HTML标签和每个在HTML页面中出现的内容。
<!-- 表明此为一个JSP页面 --> <%@ page contentType="text/html; charset=gb2312" language="java" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE>第一个JSP页面</TITLE> </HEAD> <BODY> <!-- 下面是Java脚本--> <%for(int i = 0 ; i < 10; i++) { out.println(i); %> <br> <%}%> </BODY> </HTML>当启动Tomcat之后,可以在Tomcat的Catalina\localhost\jsptest\org\apache\jsp目录下找到如下文件(假如Web应用名为jsptest,上面JSP页的名为test1.jsp):test1_jsp.java和test1_jsp.class。这两个文件都是Tomcat生成的,Tomcat根据JSP页面生成对应Servlet的Java文件及class文件。
//JSP页面经过Tomcat编译后默认的包 package org.apache.jsp; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.jsp.*; //继承HttpJspBase类,该类其实是个Servlet的子类 public final class test1_jsp extends org.apache.jasper.runtime.HttpJspBase implements org.apache.jasper.runtime.JspSourceDependent { private static java.util.Vector _jspx_dependants; public java.util.List 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; charset=gb2312"); pageContext = _jspxFactory.getPageContext(this, request, response, null, true, 8192, true); _jspx_page_context = pageContext; application = pageContext.getServletContext(); config = pageContext.getServletConfig(); session = pageContext.getSession(); //页面输出流