javaweb--jsp

jsp的本质

jsp的本质就是一个Servlet,JSP主要负责和用户交互,将最终的界面呈现给用户,HTML+CSS+JS+Java的混合文件
当服务器接收到一个后缀是jsp的请求时,将给请求交给jsp引擎去处理,每一个jsp页面第一次被访问的时候,jsp引擎会将它翻译成一个servlet文件,由web容器调用servlet去响应

下面就是jsp引擎翻译出来的index_jsp.java文件,jsp中的有关页面的元素都会调用write方法写到web容器中,同时jsp比直接使用html文件的好处在于,jsp中还可以写入java,因为最终会被翻译成.java文件

/*
 * Generated by the Jasper component of Apache Tomcat
 * Version: Apache Tomcat/9.0.65
 * Generated at: 2022-09-14 08:14:35 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 org.apache.jsp;

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

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

  private static final javax.servlet.jsp.JspFactory _jspxFactory =
          javax.servlet.jsp.JspFactory.getDefaultFactory();

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

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

  private static final java.util.Set<java.lang.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<java.lang.String,java.lang.Long> getDependants() {
    return _jspx_dependants;
  }

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

  public java.util.Set<java.lang.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 javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)
      throws java.io.IOException, javax.servlet.ServletException {

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

    final javax.servlet.jsp.PageContext pageContext;
    final javax.servlet.ServletContext application;
    final javax.servlet.ServletConfig config;
    javax.servlet.jsp.JspWriter out = null;
    final java.lang.Object page = this;
    javax.servlet.jsp.JspWriter _jspx_out = null;
    javax.servlet.jsp.PageContext _jspx_page_context = null;


    try {
      response.setContentType("text/html; charset=UTF-8");
      pageContext = _jspxFactory.getPageContext(this, request, response,
      			null, false, 8192, true);
      _jspx_page_context = pageContext;
      application = pageContext.getServletContext();
      config = pageContext.getServletConfig();
      out = pageContext.getOut();
      _jspx_out = out;

      out.write('\n');
      out.write('\n');

java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy");
request.setAttribute("year", sdf.format(new java.util.Date()));
request.setAttribute("tomcatUrl", "https://tomcat.apache.org/");
request.setAttribute("tomcatDocUrl", "/docs/");
request.setAttribute("tomcatExamplesUrl", "/examples/");

      out.write("\n");
      out.write("<!DOCTYPE html>\n");
      out.write("<html lang=\"en\">\n");
      out.write("    <head>\n");
      out.write("        <meta charset=\"UTF-8\" />\n");
      out.write("        <title>");
      out.print(request.getServletContext().getServerInfo() );
      out.write("</title>\n");
      out.write("        <link href=\"favicon.ico\" rel=\"icon\" type=\"image/x-icon\" />\n");
      out.write("        <link href=\"tomcat.css\" rel=\"stylesheet\" type=\"text/css\" />\n");
      out.write("    </head>\n");
      out.write("\n");
      out.write("    <body>\n");
      out.write("        <div id=\"wrapper\">\n");
      out.write("            <div id=\"navigation\" class=\"curved container\">\n");
      out.write("                <span id=\"nav-home\"><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("\">Home</a></span>\n");
      out.write("                <span id=\"nav-hosts\"><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("\">Documentation</a></span>\n");
      out.write("                <span id=\"nav-config\"><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("config/\">Configuration</a></span>\n");
      out.write("                <span id=\"nav-examples\"><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatExamplesUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("\">Examples</a></span>\n");
      out.write("                <span id=\"nav-wiki\"><a href=\"https://cwiki.apache.org/confluence/display/TOMCAT/\">Wiki</a></span>\n");
      out.write("                <span id=\"nav-lists\"><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("lists.html\">Mailing Lists</a></span>\n");
      out.write("                <span id=\"nav-help\"><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("findhelp.html\">Find Help</a></span>\n");
      out.write("                <br class=\"separator\" />\n");
      out.write("            </div>\n");
      out.write("            <div id=\"asf-box\">\n");
      out.write("                <h1>");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${pageContext.servletContext.serverInfo}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("</h1>\n");
      out.write("            </div>\n");
      out.write("            <div id=\"upper\" class=\"curved container\">\n");
      out.write("                <div id=\"congrats\" class=\"curved container\">\n");
      out.write("                    <h2>If you're seeing this, you've successfully installed Tomcat. Congratulations!</h2>\n");
      out.write("                </div>\n");
      out.write("                <div id=\"notice\">\n");
      out.write("                    <img id=\"tomcat-logo\" src=\"tomcat.svg\" alt=\"[tomcat logo]\" />\n");
      out.write("                    <div id=\"tasks\">\n");
      out.write("                        <h3>Recommended Reading:</h3>\n");
      out.write("                        <h4><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("security-howto.html\">Security Considerations How-To</a></h4>\n");
      out.write("                        <h4><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("manager-howto.html\">Manager Application How-To</a></h4>\n");
      out.write("                        <h4><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("cluster-howto.html\">Clustering/Session Replication How-To</a></h4>\n");
      out.write("                    </div>\n");
      out.write("                </div>\n");
      out.write("                <div id=\"actions\">\n");
      out.write("                    <div class=\"button\">\n");
      out.write("                        <a class=\"container shadow\" href=\"/manager/status\"><span>Server Status</span></a>\n");
      out.write("                    </div>\n");
      out.write("                    <div class=\"button\">\n");
      out.write("                        <a class=\"container shadow\" href=\"/manager/html\"><span>Manager App</span></a>\n");
      out.write("                    </div>\n");
      out.write("                    <div class=\"button\">\n");
      out.write("                        <a class=\"container shadow\" href=\"/host-manager/html\"><span>Host Manager</span></a>\n");
      out.write("                    </div>\n");
      out.write("                </div>\n");
      out.write("                <br class=\"separator\" />\n");
      out.write("            </div>\n");
      out.write("            <div id=\"middle\" class=\"curved container\">\n");
      out.write("                <h3>Developer Quick Start</h3>\n");
      out.write("                <div class=\"col25\">\n");
      out.write("                    <div class=\"container\">\n");
      out.write("                        <p><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("setup.html\">Tomcat Setup</a></p>\n");
      out.write("                        <p><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("appdev/\">First Web Application</a></p>\n");
      out.write("                    </div>\n");
      out.write("                </div>\n");
      out.write("                <div class=\"col25\">\n");
      out.write("                    <div class=\"container\">\n");
      out.write("                        <p><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("realm-howto.html\">Realms &amp; AAA</a></p>\n");
      out.write("                        <p><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("jndi-datasource-examples-howto.html\">JDBC DataSources</a></p>\n");
      out.write("                    </div>\n");
      out.write("                </div>\n");
      out.write("                <div class=\"col25\">\n");
      out.write("                    <div class=\"container\">\n");
      out.write("                        <p><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatExamplesUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("\">Examples</a></p>\n");
      out.write("                    </div>\n");
      out.write("                </div>\n");
      out.write("                <div class=\"col25\">\n");
      out.write("                    <div class=\"container\">\n");
      out.write("                        <p><a href=\"https://cwiki.apache.org/confluence/display/TOMCAT/Specifications\">Servlet Specifications</a></p>\n");
      out.write("                        <p><a href=\"https://cwiki.apache.org/confluence/display/TOMCAT/Tomcat+Versions\">Tomcat Versions</a></p>\n");
      out.write("                    </div>\n");
      out.write("                </div>\n");
      out.write("                <br class=\"separator\" />\n");
      out.write("            </div>\n");
      out.write("            <div id=\"lower\">\n");
      out.write("                <div id=\"low-manage\" class=\"\">\n");
      out.write("                    <div class=\"curved container\">\n");
      out.write("                        <h3>Managing Tomcat</h3>\n");
      out.write("                        <p>For security, access to the <a href=\"/manager/html\">manager webapp</a> is restricted.\n");
      out.write("                        Users are defined in:</p>\n");
      out.write("                        <pre>$CATALINA_HOME/conf/tomcat-users.xml</pre>\n");
      out.write("                        <p>In Tomcat 9.0 access to the manager application is split between\n");
      out.write("                           different users. &nbsp; <a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("manager-howto.html\">Read more...</a></p>\n");
      out.write("                        <br />\n");
      out.write("                        <h4><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("RELEASE-NOTES.txt\">Release Notes</a></h4>\n");
      out.write("                        <h4><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("changelog.html\">Changelog</a></h4>\n");
      out.write("                        <h4><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("migration.html\">Migration Guide</a></h4>\n");
      out.write("                        <h4><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("security.html\">Security Notices</a></h4>\n");
      out.write("                    </div>\n");
      out.write("                </div>\n");
      out.write("                <div id=\"low-docs\" class=\"\">\n");
      out.write("                    <div class=\"curved container\">\n");
      out.write("                        <h3>Documentation</h3>\n");
      out.write("                        <h4><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("\">Tomcat 9.0 Documentation</a></h4>\n");
      out.write("                        <h4><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("config/\">Tomcat 9.0 Configuration</a></h4>\n");
      out.write("                        <h4><a href=\"https://cwiki.apache.org/confluence/display/TOMCAT/\">Tomcat Wiki</a></h4>\n");
      out.write("                        <p>Find additional important configuration information in:</p>\n");
      out.write("                        <pre>$CATALINA_HOME/RUNNING.txt</pre>\n");
      out.write("                        <p>Developers may be interested in:</p>\n");
      out.write("                        <ul>\n");
      out.write("                            <li><a href=\"https://tomcat.apache.org/bugreport.html\">Tomcat 9.0 Bug Database</a></li>\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("api/index.html\">Tomcat 9.0 JavaDocs</a></li>\n");
      out.write("                            <li><a href=\"https://github.com/apache/tomcat/tree/9.0.x\">Tomcat 9.0 Git Repository at GitHub</a></li>\n");
      out.write("                        </ul>\n");
      out.write("                    </div>\n");
      out.write("                </div>\n");
      out.write("                <div id=\"low-help\" class=\"\">\n");
      out.write("                    <div class=\"curved container\">\n");
      out.write("                        <h3>Getting Help</h3>\n");
      out.write("                        <h4><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("faq/\">FAQ</a> and <a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("lists.html\">Mailing Lists</a></h4>\n");
      out.write("                        <p>The following mailing lists are available:</p>\n");
      out.write("                        <ul>\n");
      out.write("                            <li id=\"list-announce\"><strong><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("lists.html#tomcat-announce\">tomcat-announce</a><br />\n");
      out.write("                                Important announcements, releases, security vulnerability notifications. (Low volume).</strong>\n");
      out.write("                            </li>\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("lists.html#tomcat-users\">tomcat-users</a><br />\n");
      out.write("                                User support and discussion\n");
      out.write("                            </li>\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("lists.html#taglibs-user\">taglibs-user</a><br />\n");
      out.write("                                User support and discussion for <a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("taglibs/\">Apache Taglibs</a>\n");
      out.write("                            </li>\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("lists.html#tomcat-dev\">tomcat-dev</a><br />\n");
      out.write("                                Development mailing list, including commit messages\n");
      out.write("                            </li>\n");
      out.write("                        </ul>\n");
      out.write("                    </div>\n");
      out.write("                </div>\n");
      out.write("                <br class=\"separator\" />\n");
      out.write("            </div>\n");
      out.write("            <div id=\"footer\" class=\"curved container\">\n");
      out.write("                <div class=\"col20\">\n");
      out.write("                    <div class=\"container\">\n");
      out.write("                        <h4>Other Downloads</h4>\n");
      out.write("                        <ul>\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("download-connectors.cgi\">Tomcat Connectors</a></li>\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("download-native.cgi\">Tomcat Native</a></li>\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("taglibs/\">Taglibs</a></li>\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("deployer-howto.html\">Deployer</a></li>\n");
      out.write("                        </ul>\n");
      out.write("                    </div>\n");
      out.write("                </div>\n");
      out.write("                <div class=\"col20\">\n");
      out.write("                    <div class=\"container\">\n");
      out.write("                        <h4>Other Documentation</h4>\n");
      out.write("                        <ul>\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("connectors-doc/\">Tomcat Connectors</a></li>\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("connectors-doc/\">mod_jk Documentation</a></li>\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("native-doc/\">Tomcat Native</a></li>\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("deployer-howto.html\">Deployer</a></li>\n");
      out.write("                        </ul>\n");
      out.write("                    </div>\n");
      out.write("                </div>\n");
      out.write("                <div class=\"col20\">\n");
      out.write("                    <div class=\"container\">\n");
      out.write("                        <h4>Get Involved</h4>\n");
      out.write("                        <ul>\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("getinvolved.html\">Overview</a></li>\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("source.html\">Source Repositories</a></li>\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("lists.html\">Mailing Lists</a></li>\n");
      out.write("                            <li><a href=\"https://cwiki.apache.org/confluence/display/TOMCAT/\">Wiki</a></li>\n");
      out.write("                        </ul>\n");
      out.write("                    </div>\n");
      out.write("                </div>\n");
      out.write("                <div class=\"col20\">\n");
      out.write("                    <div class=\"container\">\n");
      out.write("                        <h4>Miscellaneous</h4>\n");
      out.write("                        <ul>\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("contact.html\">Contact</a></li>\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("legal.html\">Legal</a></li>\n");
      out.write("                            <li><a href=\"https://www.apache.org/foundation/sponsorship.html\">Sponsorship</a></li>\n");
      out.write("                            <li><a href=\"https://www.apache.org/foundation/thanks.html\">Thanks</a></li>\n");
      out.write("                        </ul>\n");
      out.write("                    </div>\n");
      out.write("                </div>\n");
      out.write("                <div class=\"col20\">\n");
      out.write("                    <div class=\"container\">\n");
      out.write("                        <h4>Apache Software Foundation</h4>\n");
      out.write("                        <ul>\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("whoweare.html\">Who We Are</a></li>\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("heritage.html\">Heritage</a></li>\n");
      out.write("                            <li><a href=\"https://www.apache.org\">Apache Home</a></li>\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("resources.html\">Resources</a></li>\n");
      out.write("                        </ul>\n");
      out.write("                    </div>\n");
      out.write("                </div>\n");
      out.write("                <br class=\"separator\" />\n");
      out.write("            </div>\n");
      out.write("            <p class=\"copyright\">Copyright &copy;1999-");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${year}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write(" Apache Software Foundation.  All Rights Reserved</p>\n");
      out.write("        </div>\n");
      out.write("    </body>\n");
      out.write("\n");
      out.write("</html>\n");
    } catch (java.lang.Throwable t) {
      if (!(t instanceof javax.servlet.jsp.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中嵌入java的三种方式

方式一:jsp脚本,执行java逻辑代码

<%
    String s = "hello";
    System.out.println(s);
%>

方式二:jsp声明,定义java方法


<%!
    public void test() {
        System.out.println("hello world");
    }
%>
<%
    test();
%>

方式三:jsp表达式,把java对象直接输出到html页面

<%=s.getClass()
%>

案例:

<%@ page import="java.util.ArrayList" %>
<%@ page import="com.entity.User" %>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
    <title>JSP - Hello World</title>
</head>
<body>
<%
    ArrayList<User> users = new ArrayList<>();
    users.add(new User("张三", 18));
    users.add(new User("李四", 28));
    users.add(new User("王五", 17));
    users.add(new User("赵六", 43));
%>
<table>
    <tr>
        <th>姓名</th>
        <th>年龄</th>
    </tr>
    <%
        for (int i = 0; i < users.size(); i++) {
    %>
    <tr>
        <td>
            <%=users.get(i).getName()%>
        </td>
        <td>
            <%=users.get(i).getAge()%>
        </td>
    </tr>
    <%
        }
    %>
</table>

</body>
</html>

JSP内置对象9个

request:表示一次请求,HttpServletRequest
response:表示一次响应,HttpServletResponse
pageContext:页面上下文,获取页面信息,PageContext
session:表示一次会话,保存用户信息,HttpSession
application:表示当前web应用,全局对象,保存所有用户共享信息,ServletContext
config:当前JSP对应的Servlet的ServletConfig对象,获取当前Servlet信息
out:向浏览器输出数据,JSPWriter
page:当前JSP对应的Servlet对象,Servlet
exception:表示JSP页面发生的异常,Exception

常用的是:request、response、session、application、pageContext

request 常用方法:

  1. String getParameter(String key) 获取客户端传来的参数
  2. void setAttribute(String key,Object value) 通过键值对的形式保存数据,服务器内部
  3. Object getAttribute(String key) 通过key取出value,服务器内部
  4. RequestDispatcher getRequestDispatcher(String path)返回一个RequestDispatcher对象,该对象的forward方法用于请求转发(转发后停在最终页面)
  5. String[] getParameterValues()获取客户端传来的多个同名参数
  6. void setCharacterEncoding(String charset)指定每个请求的编码

HTTP请求状态码

200:正常
404:找不到资源
400:请求类型不匹配
500:java程序抛出的异常

response常用方法
1、 sendRedirect(String path)重定向
转发getRequestDispatcher和重定向sendRedirect的区别:

  • 转发是将同一个请求传给下个页面。重定向是创建一个新的请求传给下个页面,之前的请求结束生命周期
  • 转发是同一个请求在服务器之间传递,地址栏不变,也叫服务器跳转。重定向是由客户端发送一次新的请求来访问跳转后的目标资源,地址栏改变,也叫客户端跳转
  • 如果两个页面之间需要使用request进行传值,则必须使用重定向

案例:用户登录

用户登录,如果用户名密码正确,则跳转首页(转发),并展示用户名,否则重新回到登录页面(重定向)

login.jsp

<%--
  Created by IntelliJ IDEA.
  User: bijing
  Date: 2022/9/16
  Time: 20:01
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Login</title>
</head>
<body>
<div>
    <form action="/check.jsp" method="post">
        <table>
            <tr>
                <td> 用户名:</td>
                <td>
                    <input type="text" name="userName">
                </td>
            </tr>
            <tr>
                <td>密码:</td>
                <td>
                    <input type="password" name="psd">
                </td>
            </tr>
        </table>
        <input type="submit" value="登录">
    </form>
</div>
</body>
</html>

check.jsp

<%--
  Created by IntelliJ IDEA.
  User: bijing
  Date: 2022/9/16
  Time: 20:34
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>check</title>
</head>
<body>
<%
    request.setCharacterEncoding("UTF-8");
    String userName = request.getParameter("userName");
    String psd = request.getParameter("psd");
    if (userName != null && psd != null) {
        System.out.println(userName + "_" + psd);
        if (userName.equals("张三") && psd.equals("12345")) {
            request.setAttribute("userName", userName);
            request.setAttribute("psd", psd);
            request.getRequestDispatcher("success.jsp").forward(request, response);
        } else {
            response.sendRedirect("login.jsp");
        }
    }
%>
</body>
</html>

success.jsp

<%--
  Created by IntelliJ IDEA.
  User: bijing
  Date: 2022/9/16
  Time: 20:11
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Success</title>
</head>
<body>
<%
    String userName = (String) request.getAttribute("userName");
    String psd = (String) request.getParameter("psd");
%>
<h1>用户名:<%=userName%>
</h1>
<h1>密码:<%=psd%>
</h1>

</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值