优秀的编程知识分享平台

网站首页 > 技术文章 正文

《JSP》第04节:JSP基础语法之程序代码片段

nanyue 2024-07-29 01:23:47 技术文章 6 ℃

前面几个小节,已经把JSP程序的创建以及运行原理都介绍完了,这一小节开始介绍具体的JSP语法。JSP本质上是Servlet,Servlet是采用Java语言编写的,所以JSP语法也是要满足Java语法。

JSP是经过JSP引擎渲染成Servlet程序,那么渲染过程中,就需要知道渲染的规则,这些规则也就是我们需要学习的JSP基础语法,这一小节学习JSP程序代码片段的语法。

1.1、基础语法

什么是代码片段呢???这里的代码片段,其实就是指:在JSP文件中使用JSP的语法,编写Java代码片段。JSP代码片段的使用语法如下:

<%
  在这里面编写Java代码
%>

注意:JSP代码片段,会被嵌入到编译之后的_jspService()方法里面,所以使用JSP代码片段,就相当于是在_jspService()方法体里面编写Java代码

案例代码如下:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>JSP基础语法之代码段</title>
</head>
<body>
    <h3>JSP基础语法之代码段</h3>
    <%
        // JSP基础语法之代码段
        out.println("使用JSP代码片段输出的内容");
    %>
</body>
</html>

运行结果如下图所示:

1.2、代码片段的本质

JSP代码片段的本质是什么呢???我们来看看上面JSP文件,编译之后对应的Java源代码:

/*
 * Generated by the Jasper component of Apache Tomcat
 * Version: Apache Tomcat/8.5.98
 * Generated at: 2024-02-24 10:32:43 UTC
 * Note: The last modified time of this file was set to
 *       the last modified time of the source file after
 *       generation to assist with modification tracking.
 */
package com.gitcode.servlet;

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

public final class fragment_jsp extends org.apache.jasper.runtime.HttpJspBase
    implements org.apache.jasper.runtime.JspSourceDependent,
                 org.apache.jasper.runtime.JspSourceImports {

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

  private static java.util.Map<String, Long> _jspx_dependants;

  private static final java.util.Set<String> _jspx_imports_packages;

  private static final java.util.Set<String> _jspx_imports_classes;

  static {
    _jspx_imports_packages = new java.util.HashSet<>();
    _jspx_imports_packages.add("javax.servlet");
    _jspx_imports_packages.add("javax.servlet.http");
    _jspx_imports_packages.add("javax.servlet.jsp");
    _jspx_imports_classes = null;
  }

  private volatile javax.el.ExpressionFactory _el_expressionfactory;
  private volatile org.apache.tomcat.InstanceManager _jsp_instancemanager;

  public java.util.Map<String, Long> getDependants() {
    return _jspx_dependants;
  }

  public java.util.Set<String> getPackageImports() {
    return _jspx_imports_packages;
  }

  public java.util.Set<String> getClassImports() {
    return _jspx_imports_classes;
  }

  public javax.el.ExpressionFactory _jsp_getExpressionFactory() {
    if (_el_expressionfactory == null) {
      synchronized (this) {
        if (_el_expressionfactory == null) {
          _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
        }
      }
    }
    return _el_expressionfactory;
  }

  public org.apache.tomcat.InstanceManager _jsp_getInstanceManager() {
    if (_jsp_instancemanager == null) {
      synchronized (this) {
        if (_jsp_instancemanager == null) {
          _jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());
        }
      }
    }
    return _jsp_instancemanager;
  }

  public void _jspInit() {
  }

  public void _jspDestroy() {
  }

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

    final String _jspx_method = request.getMethod();
    if (!"GET".equals(_jspx_method) && !"POST".equals(_jspx_method) && !"HEAD".equals(_jspx_method) && !DispatcherType.ERROR.equals(request.getDispatcherType())) {
      response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "JSP 只允许 GET、POST 或 HEAD。Jasper 还允许 OPTIONS");
      return;
    }

    final PageContext pageContext;
    HttpSession session = null;
    final ServletContext application;
    final ServletConfig config;
    JspWriter out = null;
    final 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("<html>\r\n");
      out.write("<head>\r\n");
      out.write("    <title>JSP基础语法之代码段</title>\r\n");
      out.write("</head>\r\n");
      out.write("<body>\r\n");
      out.write("    <h3>JSP基础语法之代码段</h3>\r\n");
      out.write("    ");

        // JSP基础语法之代码段
        out.println("使用JSP代码片段输出的内容");
    
      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 {
            if (response.isCommitted()) {
              out.flush();
            } else {
              out.clearBuffer();
            }
          } catch (java.io.IOException e) {}
        if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
        else throw new ServletException(t);
      }
    } finally {
      _jspxFactory.releasePageContext(_jspx_page_context);
    }
  }
}

通过编译之后的源代码,可以看到,JSP代码片段中的内容会被嵌入到_jspService()方法里面。

到此,JSP程序代码片段就介绍完啦。

今天就到这里,未完待续~~

最近发表
标签列表