码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • javaweb--jsp


    jsp学习

    • jsp的本质
    • jsp中嵌入java的三种方式
    • JSP内置对象9个
    • HTTP请求状态码
      • 案例:用户登录

    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("\n");
          out.write("\n");
          out.write("    \n");
          out.write("        \n");
          out.write("        "</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
          out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span>request<span class="token punctuation">.</span><span class="token function">getServletContext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getServerInfo</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>
          out<span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span><span class="token string">"\n");
          out.write("        \n");
          out.write("        \n");
          out.write("    \n");
          out.write("\n");
          out.write("    \n");
          out.write("        
    \n"); out.write("
    \n"); out.write(" ); 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\n"); out.write(" ); 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\n"); out.write(" ); 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\n"); out.write(" ); 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\n"); out.write(" Wiki\n"); out.write(" ); 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\n"); out.write(" ); 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\n"); out.write("
    \n"
    ); out.write("
    \n"
    ); out.write("
    \n"); out.write("

    "); 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("

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

    If you're seeing this, you've successfully installed Tomcat. Congratulations!

    \n"
    ); out.write("
    \n"
    ); out.write("
    \n"); out.write(" \"[tomcat\n"); out.write("
    \n"); out.write("

    Recommended Reading:

    \n"
    ); out.write("

    ); 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

    \n"
    ); out.write("

    ); 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

    \n"
    ); out.write("

    ); 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

    \n"
    ); out.write("
    \n"
    ); out.write("
    \n"
    ); out.write("
    \n"); out.write("
    \n"); out.write(" Server Status\n"); out.write("
    \n"
    ); out.write("
    \n"); out.write(" Manager App\n"); out.write("
    \n"
    ); out.write("
    \n"); out.write(" Host Manager\n"); out.write("
    \n"
    ); out.write("
    \n"
    ); out.write("
    \n"
    ); out.write("
    \n"
    ); out.write("
    \n"); out.write("

    Developer Quick Start

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

    ); 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

    \n"
    ); out.write("

    ); 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

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

    ); 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 & AAA

    \n"
    ); out.write("

    ); 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

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

    ); 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

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

    Servlet Specifications

    \n"
    ); out.write("

    Tomcat Versions

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

    Managing Tomcat

    \n"
    ); out.write("

    For security, access to the manager webapp is restricted.\n"); out.write(" Users are defined in:

    \n"
    ); out.write("
    $CATALINA_HOME/conf/tomcat-users.xml
    \n"
    ); out.write("

    In Tomcat 9.0 access to the manager application is split between\n"); out.write(" different users.   ); 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...

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

    ); 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

    \n"
    ); out.write("

    ); 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

    \n"
    ); out.write("

    ); 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

    \n"
    ); out.write("

    ); 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

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

    Documentation

    \n"
    ); out.write("

    ); 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

    \n"
    ); out.write("

    ); 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

    \n"
    ); out.write("

    Tomcat Wiki

    \n"
    ); out.write("

    Find additional important configuration information in:

    \n"
    ); out.write("
    $CATALINA_HOME/RUNNING.txt
    \n"
    ); out.write("

    Developers may be interested in:

    \n"
    ); out.write("
      \n"); out.write("
    • Tomcat 9.0 Bug Database
    • \n"
      ); out.write("
    • ); 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
    • \n"
      ); out.write("
    • Tomcat 9.0 Git Repository at GitHub
    • \n"
      ); out.write("
    \n"
    ); out.write("
    \n"
    ); out.write("
    \n"
    ); out.write("
    \n"); out.write("
    \n"); out.write("

    Getting Help

    \n"
    ); out.write("

    ); 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 and ); 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

    \n"
    ); out.write("

    The following mailing lists are available:

    \n"
    ); out.write("
      \n"); out.write("
    • ); 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
      \n"); out.write(" Important announcements, releases, security vulnerability notifications. (Low volume).
      \n"); out.write("
    • \n"
      ); out.write("
    • ); 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
      \n"); out.write(" User support and discussion\n"); out.write("
    • \n"
      ); out.write("
    • ); 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
      \n"); out.write(" User support and discussion for ); 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\n"); out.write("
    • \n"
      ); out.write("
    • ); 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
      \n"); out.write(" Development mailing list, including commit messages\n"); out.write("
    • \n"
      ); out.write("
    \n"
    ); out.write("
    \n"
    ); out.write("
    \n"
    ); out.write("
    \n"
    ); out.write("
    \n"
    ); out.write("
    \n"); out.write("
    \n"); out.write("
    \n"); out.write("

    Other Downloads

    \n"
    ); out.write("
      \n"); out.write("
    • ); 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
    • \n"
      ); out.write("
    • ); 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
    • \n"
      ); out.write("
    • ); 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
    • \n"
      ); out.write("
    • ); 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
    • \n"
      ); out.write("
    \n"
    ); out.write("
    \n"
    ); out.write("
    \n"
    ); out.write("
    \n"); out.write("
    \n"); out.write("

    Other Documentation

    \n"
    ); out.write("
      \n"); out.write("
    • ); 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
    • \n"
      ); out.write("
    • ); 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
    • \n"
      ); out.write("
    • ); 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
    • \n"
      ); out.write("
    • ); 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
    • \n"
      ); out.write("
    \n"
    ); out.write("
    \n"
    ); out.write("
    \n"
    ); out.write("
    \n"); out.write("
    \n"); out.write("

    Get Involved

    \n"
    ); out.write("
      \n"); out.write("
    • ); 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
    • \n"
      ); out.write("
    • ); 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
    • \n"
      ); out.write("
    • ); 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
    • \n"
      ); out.write("
    • Wiki
    • \n"
      ); out.write("
    \n"
    ); out.write("
    \n"
    ); out.write("
    \n"
    ); out.write("
    \n"); out.write("
    \n"); out.write("

    Miscellaneous

    \n"
    ); out.write("
      \n"); out.write("
    • ); 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
    • \n"
      ); out.write("
    • ); 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
    • \n"
      ); out.write("
    • Sponsorship
    • \n"
      ); out.write("
    • Thanks
    • \n"
      ); out.write("
    \n"
    ); out.write("
    \n"
    ); out.write("
    \n"
    ); out.write("
    \n"); out.write("
    \n"); out.write("

    Apache Software Foundation

    \n"
    ); out.write("
      \n"); out.write("
    • ); 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
    • \n"
      ); out.write("
    • ); 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
    • \n"
      ); out.write("
    • Apache Home
    • \n"
      ); out.write("
    • ); 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
    • \n"
      ); out.write("
    \n"
    ); out.write("
    \n"
    ); out.write("
    \n"
    ); out.write("
    \n"
    ); out.write("
    \n"
    ); out.write("

    Copyright ©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

    \n"
    ); out.write("
    \n"
    ); out.write(" \n"); out.write("\n"); out.write("\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); } } }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337
    • 338
    • 339
    • 340
    • 341
    • 342
    • 343
    • 344
    • 345
    • 346
    • 347
    • 348
    • 349
    • 350
    • 351
    • 352
    • 353
    • 354
    • 355
    • 356
    • 357
    • 358
    • 359
    • 360
    • 361
    • 362
    • 363
    • 364
    • 365
    • 366
    • 367
    • 368
    • 369
    • 370
    • 371
    • 372
    • 373
    • 374
    • 375
    • 376
    • 377
    • 378
    • 379
    • 380
    • 381
    • 382
    • 383
    • 384
    • 385
    • 386
    • 387
    • 388
    • 389
    • 390
    • 391
    • 392
    • 393
    • 394
    • 395
    • 396
    • 397
    • 398
    • 399
    • 400
    • 401
    • 402
    • 403
    • 404
    • 405
    • 406
    • 407
    • 408
    • 409
    • 410
    • 411
    • 412
    • 413
    • 414
    • 415
    • 416
    • 417
    • 418
    • 419
    • 420
    • 421
    • 422
    • 423
    • 424
    • 425
    • 426
    • 427
    • 428
    • 429
    • 430
    • 431
    • 432
    • 433
    • 434

    jsp中嵌入java的三种方式

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

    <%
        String s = "hello";
        System.out.println(s);
    %>
    
    • 1
    • 2
    • 3
    • 4

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

    
    <%!
        public void test() {
            System.out.println("hello world");
        }
    %>
    <%
        test();
    %>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

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

    <%=s.getClass()
    %>
    
    • 1
    • 2

    案例:

    <%@ 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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    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>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    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>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    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>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
  • 相关阅读:
    OSPF—— LSA-1和 LSA-2
    超级干货!如何挖公益SRC实战/SQL注入
    python基于PHP+MySQL的图书分享平台
    Influxdb数据库部署文档
    PyCharm运行Nosetests并导出测试报告
    c++ 图的重构识别
    分布式架构网约车平台后端原型系统设计与实现
    k8s--基础--10.2--命令--kubectl--常用命令
    J2EE之通用分页02
    HTML网页设计【足球科普】学生DW静态网页设计
  • 原文地址:https://blog.csdn.net/weixin_43903745/article/details/126890759
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号