–脚本元素的类型
–JSP声明
? JSP声明中JSP/servlet之间的对应
? JSP声明:示例
–JSP表达式
? JSP表达式中JSP/servlet的对应
? JSP表达式:示例
–JSP scriptlet(代码块)
? JSP代码块中JSP/servlet的对应
–JSP声明和预定义变量
–JSP处理的两个阶段
##############################################
首先我们看下JAVA中如何申明变量、如何申明方法及方法与方法之间如何调用的哈~
JDBC+Servlet+JSP整合开发之23.JSP脚本元素_JDBC
看下结果
JDBC+Servlet+JSP整合开发之23.JSP脚本元素_脚本_02
下面如果我们用JSP来写这个程序如何写哈~
? 脚本元素的类型
–声明

? 格式:<%! code %>
? 逐字地插入到servlet类的定义体中,不在任何方法之内
–表达式
? 格式:<%= expression %>
? 求值并插入到servlet的输出中
? 也就是会产生类似于out.print(expression)的语句
–scriptlet (代码块)
? 格式:<% code %>
? 逐字地插入到servlet的_jspService方法中(由service调用)
? JSP声明
–格式
? <%! Java Code %>
–结果
? 代码被逐字地插入到servlet类的定义中,在任何现有方法以外
–示例
<%! private int someField = 5; %>
<%! private void someMethod(...) {...} %>
–设计上的考虑
? 字段当然有用,但对于方法而言,通常在单独的Java类中定义更好一些
–XML兼容的语法
? <jsp:declaration>Java Code</jsp:declaration>
–JSP声明中JSP/servlet之间的对应
?原始的JSP
JDBC+Servlet+JSP整合开发之23.JSP脚本元素_JDBC_03
?生成的servlet代码
JDBC+Servlet+JSP整合开发之23.JSP脚本元素_元素_04
–JSP声明:示例
JDBC+Servlet+JSP整合开发之23.JSP脚本元素_JDBC_05
JDBC+Servlet+JSP整合开发之23.JSP脚本元素_JDBC_06
? JSP表达式
–格式
? <%= Java Expression %>
–结果
? 表达式被求值,转换成字符串,将按照它在JSP页面中出现的位置插入到HTML页面中。
? 即,表达式放置在out.print内
–示例
?当前时间:<%= new java.util.Date() %>
? 您的主机名:<%= request.getRemoteHost() %>
–XML兼容的语法
? <jsp:expression>Java Expression</jsp:expression>
?在单个页面中不能混合使用不同的版本。如果使用jsp:expression就必须整个页面都使用XML
–JSP表达式中JSP/servlet的对应
?原始的JSP
JDBC+Servlet+JSP整合开发之23.JSP脚本元素_元素_07
?所产生的具有代表性的servlet代码
JDBC+Servlet+JSP整合开发之23.JSP脚本元素_脚本_08
–JSP表达式:示例
JDBC+Servlet+JSP整合开发之23.JSP脚本元素_Servlet_09
JDBC+Servlet+JSP整合开发之23.JSP脚本元素_Servlet_10
看下效果
JDBC+Servlet+JSP整合开发之23.JSP脚本元素_JSP_11
JSP页面都会转换成Servlet,我们来看下
JDBC+Servlet+JSP整合开发之23.JSP脚本元素_JDBC_12
declaration_jsp.java
package org.apache.jsp;    

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

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

                private String name = "michael";    
                private int age = 20;    
                private String print(){    
                        return name+":"+age;    
                }    
    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");    
            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("\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("        \r\n");    
            out.write("        <title>Test declarationJspPage...</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<!-- declare variable and method    -->\r\n");    
            out.write("        ");    
            out.write("\r\n");    
            out.write("        \r\n");    
            out.write("        Name:");    
            out.print(name );    
            out.write("<br/>\r\n");    
            out.write("        Age:");    
            out.print(age );    
            out.write("<br/>\r\n");    
            out.write("        Name and age:");    
            out.print(print() );    
            out.write("\r\n");    
            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);    
        }    
    }    
}
来看下计算器的实例
JDBC+Servlet+JSP整合开发之23.JSP脚本元素_脚本_13
看下结果
JDBC+Servlet+JSP整合开发之23.JSP脚本元素_JSP_14
字段当然有用,但对于方法而言,通常在单独的Java类中定义更好一些
Calculator.java
JDBC+Servlet+JSP整合开发之23.JSP脚本元素_Servlet_15
declaration.jsp
JDBC+Servlet+JSP整合开发之23.JSP脚本元素_JDBC_16
看下效果
JDBC+Servlet+JSP整合开发之23.JSP脚本元素_脚本_17
再看一个页面被访问的次数
JDBC+Servlet+JSP整合开发之23.JSP脚本元素_元素_18
看下效果:
JDBC+Servlet+JSP整合开发之23.JSP脚本元素_JDBC_19
exception.jsp
JDBC+Servlet+JSP整合开发之23.JSP脚本元素_脚本_20
看下结果
JDBC+Servlet+JSP整合开发之23.JSP脚本元素_JSP_21 
exception_jsp.java
package org.apache.jsp;    

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

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

        String name = "michael";    
        public String getTime(){    
                return new Date().toLocaleString();    
        }    
    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("<!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 'exception.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("        Time1:");    
            out.print(new Date().toLocaleString() );    
            out.write("<br/>\r\n");    
            out.write("        \r\n");    
            out.write("        ");    
            out.write("\r\n");    
            out.write("        Name:");    
            out.print(name );    
            out.write("<br/>\r\n");    
            out.write("        Time2:");    
            out.print(getTime() );    
            out.write("<br/>\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 scriptlet(代码块)
–格式
? <% Java Code %>
–结果
? 代码被逐字地插入到servlet的_jspService方法中
–示例
<%
String queryData = request.getQueryString();
out.println("Attached GET data: " + queryData);
%>
<% response.setContentType("text/plain"); %>
–XML兼容的语法
? <jsp:scriptlet>Java Code</jsp:scriptlet>
–JSP scriptlet(代码块)中JSP/servlet的对应
?原始的JSP
JDBC+Servlet+JSP整合开发之23.JSP脚本元素_脚本_22
?所产生的具有代表性的servlet代码
JDBC+Servlet+JSP整合开发之23.JSP脚本元素_元素_23
Scriptlet.jsp
JDBC+Servlet+JSP整合开发之23.JSP脚本元素_元素_24
看下效果:
JDBC+Servlet+JSP整合开发之23.JSP脚本元素_JSP_25
Scriptlet_jsp.java
package org.apache.jsp;    

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

public final class Scriptlet_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("<!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 'Scriptlet.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("        ");    

                if(Calendar.AM_PM==Calendar.AM){    
                        out.println("Good morning!");    
                }else{    
                        out.println("Good afternoon!");    
                }    
                out.println("<hr/>");    
                String queryString = request.getQueryString();    
                out.println(queryString);    
                out.println("<hr/>");    
                String name = request.getParameter("name");    
                out.println(name);    
                out.println("<br/>");    
                String age = request.getParameter("age");    
                out.println(age);    
                out.println("<br/>");    
                String blog = request.getParameter("blog");    
                out.println(blog);    
            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声明和预定义变量
–问题
? 预定义变量(request, response, out, session, 等)是jspService方法中的局部变量。因而,由JSP声明定义的方法或辅助类中的方法都不能使用它们。怎么处理这种情况呢?
–解决方案:将它们作为参数传递,例如 <%!
private void someMethod(HttpSession s) {
doSomethingWith(s);
}
%>
<% someMethod(session); %>
–要注意:JspWriter的println方法抛出IOException异常
? 使用println的方法要使用“throws IOException”
? JSP处理的两个阶段
–JSP页面转换阶段
?页面被编译成Java类,所有的HTML标签和JSP标签被处理,来生成一个Servlet类,然而,脚本和表达式不被执行。
–请求处理阶段
?当客户端请求服务器时,处于请求处理阶段
?在该阶段脚本和表达式被执行
##############################################