–对JSP的需求
–JSP的结构
–JSP的好处
–JSP实例
?创建一个简单的JSP页面
###########################################
? JSP
–JSP 全称Java Server Page
?对JSP的需求
–使用servlet可以容易地完成下述任务:
? 读取表单数据
? 读取HTTP请求报头
? 设置HTTP状态代码和响应报头
? 使用cookie以及进行会话跟踪
?跨servlet共享数据
? 跨请求记录数据
? 获得有趣且报酬丰厚的工作
? 但使用servlet也有不尽如人意的地方:
–使用println语句生成HTML
–维护上述的HTML
? JSP的结构
–思想:
?大部分的页面使用常规的HTML
?用特殊的标签将servlet代码标记出来
?整个JSP页面最终转换成servlet(仅执行一次),实际
被调用的是servlet(每个请求)
–示例
JDBC+Servlet+JSP整合开发之22.JSP简介_第1张图片
使用Servlet输出0~9的页面
LoopServlet
JDBC+Servlet+JSP整合开发之22.JSP简介_第2张图片
测试
JDBC+Servlet+JSP整合开发之22.JSP简介_第3张图片
使用JSP输出0~9的页面
MyFirstJsp.jsp
JDBC+Servlet+JSP整合开发之22.JSP简介_第4张图片
测试
JDBC+Servlet+JSP整合开发之22.JSP简介_第5张图片
看一下JSP页面转换的Servlet文件存放在哪里哈~
JDBC+Servlet+JSP整合开发之22.JSP简介_第6张图片
MyFirstJsp_jsp.java
package org.apache.jsp;    

import javax.servlet.*;    
import javax.servlet.http.*;    
import javax.servlet.jsp.*;    
import java.util.*;    

public final class MyFirstJsp_jsp extends org.apache.jasper.runtime.HttpJspBase    
         implements org.apache.jasper.runtime.JspSourceDependent {    

     private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory();    

     private static java.util.List _jspx_dependants;    

     private javax.el.ExpressionFactory _el_expressionfactory;    
     private org.apache.AnnotationProcessor _jsp_annotationprocessor;    

     public Object getDependants() {    
         return _jspx_dependants;    
    }    

     public void _jspInit() {    
        _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();    
        _jsp_annotationprocessor = (org.apache.AnnotationProcessor) getServletConfig().getServletContext().getAttribute(org.apache.AnnotationProcessor. class.getName());    
    }    

     public void _jspDestroy() {    
    }    

     public void _jspService(HttpServletRequest request, HttpServletResponse response)    
                 throws java.io.IOException, ServletException {    

        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 {    
            response.setContentType( "text/html;charset=ISO-8859-1");    
            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');    
            out.write('\n');    

String path = request.getContextPath();    
String basePath = request.getScheme()+ "://"+request.getServerName()+":"+request.getServerPort()+path+"/";    

            out.write("\r\n");    
            out.write("\r\n");    
            out.write("//W3C//DTD HTML 4.01 Transitional//EN\">\r\n");    
            out.write("\r\n");    
            out.write("    \r\n");    
            out.write("        );    
            out.print(basePath);    
            out.write("\">\r\n");    
            out.write("        \r\n");    
            out.write("        My JSP 'MyFirstJsp.jsp' starting page\r\n");    
            out.write("        \r\n");    
            out.write("\t\r\n");    
            out.write("\t\r\n");    
            out.write("\t        \r\n");    
            out.write("\t\r\n");    
            out.write("\t\r\n");    
            out.write("\t\r\n");    
            out.write("\r\n");    
            out.write("    \r\n");    
            out.write("    \r\n");    
            out.write("    \r\n");    
            out.write("    \t

My First JSP!

\r\n"
);    
            out.write("        ");    

                for(int i=0;i<10;i++){    
                        out.println(i);    
                        out.print("
"
);    
                }    
            out.write("\r\n");    
            out.write("    \r\n");    
            out.write("\r\n");    
        } catch (Throwable t) {    
            if (!(t instanceof 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可以使下述任务更为容易:
? 输出HTML
? 阅读和维护这些HTML
–JSP使得下面的做法成为可能:
? 使用标准的HTML工具,比如Macromedia DreamWeaver或FrontPage
? 由小组的另外一些成员完成HTML布局,不关注Java编程
–JSP鼓励我们:
? 将创建内容(Java)代码同表示内容的(HTML)代码分离开来
? JSP实例
–创建一个简单的JSP页面
JDBC+Servlet+JSP整合开发之22.JSP简介_第7张图片
###########################################