JDBC+Servlet+JSP整合开发之22.JSP简介

–对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简介
使用Servlet输出0~9的页面
LoopServlet
JDBC+Servlet+JSP整合开发之22.JSP简介
测试
JDBC+Servlet+JSP整合开发之22.JSP简介
使用JSP输出0~9的页面
MyFirstJsp.jsp
JDBC+Servlet+JSP整合开发之22.JSP简介
测试
JDBC+Servlet+JSP整合开发之22.JSP简介
看一下JSP页面转换的Servlet文件存放在哪里哈~
JDBC+Servlet+JSP整合开发之22.JSP简介
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,    
                                    nulltrue, 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("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\r\n");    
            out.write("<html>\r\n");    
            out.write("    <head>\r\n");    
            out.write("        <base href=\"");    
            out.print(basePath);    
            out.write("\">\r\n");    
            out.write("        \r\n");    
            out.write("        <title>My JSP 'MyFirstJsp.jsp' starting page</title>\r\n");    
            out.write("        \r\n");    
            out.write("\t<meta http-equiv=\"pragma\" content=\"no-cache\">\r\n");    
            out.write("\t<meta http-equiv=\"cache-control\" content=\"no-cache\">\r\n");    
            out.write("\t<meta http-equiv=\"expires\" content=\"0\">        \r\n");    
            out.write("\t<meta http-equiv=\"keywords\" content=\"keyword1,keyword2,keyword3\">\r\n");    
            out.write("\t<meta http-equiv=\"description\" content=\"This is my page\">\r\n");    
            out.write("\t<!--\r\n");    
            out.write("\t<link rel=\"stylesheet\" type=\"text/css\" href=\"styles.css\">\r\n");    
            out.write("\t-->\r\n");    
            out.write("\r\n");    
            out.write("    </head>\r\n");    
            out.write("    \r\n");    
            out.write("    <body>\r\n");    
            out.write("    \t<h1>My First JSP!</h1>\r\n");    
            out.write("        ");    

                for(int i=0;i<10;i++){    
                        out.println(i);    
                        out.print("<br/>");    
                }    
            out.write("\r\n");    
            out.write("    </body>\r\n");    
            out.write("</html>\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简介
###########################################

附件:http://down.51cto.com/data/2355476




本文转自redking51CTO博客,原文链接:http://blog.51cto.com/redking/291153,如需转载请自行联系原作者

上一篇:阿里90后工程师利用ARM硬件特性开启安卓8终端“上帝模式”


下一篇:代码翻译尝试-使用Roaster解析和生成Java源码