黑马day05 jsp语法相关知识

来源:互联网 发布:域名在哪里购买 编辑:程序博客网 时间:2024/06/05 17:26
 1.jsp技术
        jsp是sun提供动态web资源开发技术。为了解决在Servlet中拼写html内容css、js内容十分不方便的问题,sun提供了这样一门技术。如果说Servlet是在java中嵌套HTML,则jsp就是在HTML中嵌套java代码,从而十分便于组织html页面
        
        jsp页面在第一次被访问到时会被jsp翻译引擎翻译成一个Servlet,从此对这个jsp页面的访问都是由这个Servlet执行后进行输出
        
    2.jsp语法
        (1)JSP模版元素 :jsp页面中书写的HTML内容称作JSP的模版元素,在翻译过来的Servlet中直接被out.write()输出到浏览器页面上了
            
        (2)JSP表达式 <%= java表达式 %> 在翻译过来的Servlet中,计算java表达式的值后,被out输出到浏览器上
        
        (3)JSP脚本片断 <% 若干java语句 %> 在翻译过来的Servlet中,直接被复制粘贴到了对应的位置执行.
            在一个JSP页面中可以有多个脚本片断,在两个或多个脚本片断之间可以嵌入文本、HTML标记和其他JSP元素
            多个脚本片断中的代码可以相互访问,犹如将所有的代码放在一对<%%>之中的情况
            单个脚本片断中的Java语句可以是不完整的,但是,多个脚本片断组合后的结果必须是完整的Java语句
        (4)JSP声明  <%! 若干java语句 %> 在翻译过来的Servlet中会被放置到和Service方法同级的位置,变成了类的一个成员
            
        (5)JSP注释 
         <%-- 注释的内容 --%> 被jsp注释注释掉的内容,在jsp翻译引擎将jsp翻译成Servlet的过程中会被丢弃,在翻译过来的Servlet中没有这些信息
         <%//java注释%> java注释被当作jsp脚本片段被翻译到了Servlet中,在.java文件被翻译成.class文件的时候注释信息被丢弃

         <!-- HTML注释 --> html注释被当作模版元素输出到了浏览器上,浏览器认识html注释不予显示

代码:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <title></title><meta http-equiv=" pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">      </head>  <!-- 去哪里看被翻译的jsp呢?tomcat目录的work目录下的localhost....找出相应的翻译的源代码 -->  <body>    <%--jsp表达式(不以分号结束)--> 在翻译过来的servlet直接计算出结果,使用out输出到浏览器中--%>    <%=1+1     %>     <%--jsp脚本片段-->直接拷贝内容到jsp被翻译的servlet中 --%>     <%     int i=0;     out.print(i);      %>      <%--jsp声明-->在翻译过来的servlet直接放到与service()方法同级的 位置,变成了类的成员--%>      <%!int j=7; %>  </body></html>
对应翻译的代码:
package org.apache.jsp.jsp;import javax.servlet.*;import javax.servlet.http.*;import javax.servlet.jsp.*;import java.util.*;public final class jspDemo1_jsp extends org.apache.jasper.runtime.HttpJspBase    implements org.apache.jasper.runtime.JspSourceDependent {int j=7;   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=UTF-8");      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("\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("    \r\n");      out.write("    <title></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\r\n");      out.write("  </head>\r\n");      out.write("  <!-- 去哪里看被翻译的jsp呢?--> -->\r\n");      out.write("  <body>\r\n");      out.write("    ");      out.write("\r\n");      out.write("    ");      out.print(1+1     );      out.write("\r\n");      out.write("     ");      out.write("\r\n");      out.write("     ");     int i=0;     out.print(i);            out.write("\r\n");      out.write("      ");      out.write("\r\n");      out.write("      ");      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);    }  }}
我们可以看到j变量放在了与service 同级位置作为成员属性。i是service()的方法的局部变量

对于表达式。直接计算。


0 0
原创粉丝点击