zoukankan      html  css  js  c++  java
  • 1.JSP入门

    一.JSP基础

    1.JSP概述

      JSP(Java Server Pages)是JavaWeb服务器端的动态资源。它与html页面的作用是相同的,显示数据和获取数据。

      JSP = html + Jsp脚本(代码片段) + JSP动态标签

    2.JSP语法 

    •  <%=…%>:输出!打到这里内容等同与打到response.getWriter().print(…)中了。

    <%=”hello”%>,可以

    <%=System.out.println(“hello”);%>,不可以!

    <%= Math.min(10,20)%>,可以

    • <%...%>:代码片段!打到这里内容等同与打到方法中。注:每次访问不会在原值上加加,因为是局部变量。并且多个代码片段之间是互通的!

    <%int i = 10;i = 100;%>,可以。

    <%System.out.print(“hello”);%>,可以

    <%private int a = 10;%>,不可以

    <%public volid fun1() {}%>,不可以

    • <%!...%>:定义(声明)!打到这里的内容等同与打到类体中。class A {…} 注:每次访问时会在原值上加加,因为是全局变量。

    <%! int a;%>:可以;

    <%! public void fun2() {..}%>:可以

    <%!private class A {}%>:可以

         注:在声明中,不能使用9大内置对象,因为查看JSP真身发现9大内置对象都在_jspservice下方,而声明实在_jspservice上方的,所以不能使用九大内置对象,只有java代码段和表达式可以使用这九大内置对象。

    JSP内置对象:不用创建即可直接使用!

    l  application:SevletContext

    l  request:HttpServletRequest

    l  response:HttpServletResponse

    l  session:HttpSession

    l  out:JspWriter,等同与response.getWriter()

    l  config:ServletConfig

    l  exception:异常对象,它是Throwable

    l  page:Object,它表示this。例如:Object page = this;

    l  pageContext:页面上下文!PageContext

        

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <title>JSP演示</title>
      </head>
      
      <body>
        <h1>JSP演示</h1>
        <%
            // Java语句
            String s1 = "hello jsp";
            // 不会输出到客户端,而是在服务器端的控制台打印
            System.out.println(s1);
        %>
      <!-- 输出到客户端浏览器上 -->
        输出变量:<%=s1 %><br/>
        输出int类型常量:<%=100 %><br/>
        输出String类型常量:<%="你好" %><br/>
        <br/>
        使用表达式输出常量是很傻的一件事,因为可以直接使用html即可,下面是输出上面的常量:<br/>
        100<br/>
        你好   
      </body>
    </html>

    输出三种形式:
      1.直接打字
      2.out.print("你好")
      3.response.getwriter(),print("你好")

    3.JSP原理

    1. jsp就是servlet!
    2. tomcat会把jsp转换成html,其实是把jsp变成.java
    3. 然后把.java编译成.class,其实这个类就是一个servlet!
    4. 而这个servlet中存在大量out.write(“<html>”),这就是最终的输出结果,即html

    4.jsp注释 

      我们现在已经知道JSP是需要先编译成.java,再编译成.class的。其中<%-- ... --%>中的内容在JSP编译成.java时会被忽略的,即JSP注释。

      也可以在JSP页面中使用html注释:<!-- … -->,但这个注释在JSP编译成的.java中是存在的,它不会被忽略,而且会被发送到客户端浏览器。但是在浏览器显示服务器发送过来的html时,因为<!-- … -->是html的注释,所以浏览器是不会显示它的。

    附:jsp真身代码

      

    package org.apache.jsp;
    
    import javax.servlet.*;
    import javax.servlet.http.*;
    import javax.servlet.jsp.*;
    import java.util.*;
    
    public final class regist_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;
    
      static {
        _jspx_dependants = new java.util.HashMap<java.lang.String,java.lang.Long>(2);
        _jspx_dependants.put("jar:file:/F:/tomcat/webapps/day11_30/WEB-INF/lib/jstl-1.2.jar!/META-INF/c.tld", Long.valueOf(1153356282000L));
        _jspx_dependants.put("/WEB-INF/lib/jstl-1.2.jar", Long.valueOf(1480475931708L));
      }
    
      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("java.util");
        _jspx_imports_packages.add("javax.servlet.jsp");
        _jspx_imports_packages.add("javax.servlet.http");
        _jspx_imports_classes = null;
      }
    
      private org.apache.jasper.runtime.TagHandlerPool _005fjspx_005ftagPool_005fc_005furl_0026_005fvalue_005fnobody;
    
      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() {
        _005fjspx_005ftagPool_005fc_005furl_0026_005fvalue_005fnobody = org.apache.jasper.runtime.TagHandlerPool.getTagHandlerPool(getServletConfig());
      }
    
      public void _jspDestroy() {
        _005fjspx_005ftagPool_005fc_005furl_0026_005fvalue_005fnobody.release();
      }
    
      public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)
            throws java.io.IOException, javax.servlet.ServletException {
    
    final java.lang.String _jspx_method = request.getMethod();
    if (!"GET".equals(_jspx_method) && !"POST".equals(_jspx_method) && !"HEAD".equals(_jspx_method) && !javax.servlet.DispatcherType.ERROR.equals(request.getDispatcherType())) {
    response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "JSPs only permit GET POST or HEAD");
    return;
    }
    
        final javax.servlet.jsp.PageContext pageContext;
        javax.servlet.http.HttpSession session = null;
        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, true, 8192, true);
          _jspx_page_context = pageContext;
          application = pageContext.getServletContext();
          config = pageContext.getServletConfig();
          session = pageContext.getSession();
          out = pageContext.getOut();
          _jspx_out = out;
    
          out.write("
    ");
          out.write("
    ");
    
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    
          out.write("
    ");
          out.write("
    ");
          out.write("<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    ");
          out.write("<html>
    ");
          out.write("  <head>
    ");
          out.write("    <base href="");
          out.print(basePath);
          out.write("">
    ");
          out.write("    
    ");
          out.write("    <title>My JSP 'regist.jsp' starting page</title>
    ");
          out.write("    
    ");
          out.write("	<meta http-equiv="pragma" content="no-cache">
    ");
          out.write("	<meta http-equiv="cache-control" content="no-cache">
    ");
          out.write("	<meta http-equiv="expires" content="0">    
    ");
          out.write("	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    ");
          out.write("	<meta http-equiv="description" content="This is my page">
    ");
          out.write("	<!--
    ");
          out.write("	<link rel="stylesheet" type="text/css" href="styles.css">
    ");
          out.write("	-->
    ");
          out.write("<script type="text/javascript">
    ");
          out.write("	function _change(){
    ");
          out.write("		var imgEle = document.getElementById("imgEle");
    ");
          out.write("		imgEle.src = "");
          if (_jspx_meth_c_005furl_005f0(_jspx_page_context))
            return;
          out.write("?"+ new Date().getTime(); 
    ");
          out.write("	}
    ");
          out.write("</script>
    ");
          out.write("  </head>
    ");
          out.write("  
    ");
          out.write("  <body>
    ");
          out.write("<h1>注册页面</h1>
    ");
          out.write("<!--
    ");
          out.write("1.显示表单页面
    ");
          out.write("2.显示服务器端的输入校验错误信息
    ");
          out.write("3.显示后台校验错误信息
    ");
          out.write("4.校验失败时回显数据
    ");
          out.write("-->
    ");
          out.write("<p style="color:red;font-size:10pt;font-weight:900">");
          out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${msg}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
          out.write("</p>
    ");
          out.write("<form action="");
          if (_jspx_meth_c_005furl_005f1(_jspx_page_context))
            return;
          out.write("" method="post">
    ");
          out.write("用户名:<input type="text" name="username" value="");
          out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${form.username}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
          out.write(""/>
    ");
          out.write("		<span style="color:red;font-size:10pt;font-weight:900">");
          out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${errors.username}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
          out.write("</span>
    ");
          out.write("		<br/>		
    ");
          out.write("性 别:<input type="text" name="gender" value="");
          out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${form.gender}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
          out.write(""/>
    ");
          out.write("		<span style="color:red;font-size:10pt;font-weight:900">");
          out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${errors.gender}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
          out.write("</span>
    ");
          out.write("		<br/>
    ");
          out.write("年 龄:<input type="text" name="age" value="");
          out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${form.age}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
          out.write(""/>
    ");
          out.write("		<span style="color:red;font-size:10pt;font-weight:900">");
          out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${errors.age}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
          out.write("</span>
    ");
          out.write("		<br/>
    ");
          out.write("密 码:<input type="password" name="password"/>
    ");
          out.write("		<span style="color:red;font-size:10pt;font-weight:900">");
          out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${errors.password}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
          out.write("</span>
    ");
          out.write("		<br/>
    ");
          out.write("确认密码:<input type="password" name="repassword"/>
    ");
          out.write("		<span style="color:red;font-size:10pt;font-weight:900">");
          out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${errors.repassword}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
          out.write("</span>
    ");
          out.write("		<br/>
    ");
          out.write("验证码:<input type="text" name="verifyCode" size="3" value="");
          out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${form.verifyCode}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
          out.write(""/>
    ");
          out.write("		<img  src="");
          if (_jspx_meth_c_005furl_005f2(_jspx_page_context))
            return;
          out.write("" id="imgEle"/>
    ");
          out.write("		<a href="javascript:_change()">换一张</a>
    ");
          out.write("		<span style="color:red;font-size:10pt;font-weight:900">");
          out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${errors.verifyCode}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
          out.write("</span>
    ");
          out.write("		<br/>
    ");
          out.write("	<input type="submit" value="注册"/>
    ");
          out.write("</form>
    ");
          out.write("  </body>
    ");
          out.write("</html>
    ");
        } 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);
        }
      }
    
      private boolean _jspx_meth_c_005furl_005f0(javax.servlet.jsp.PageContext _jspx_page_context)
              throws java.lang.Throwable {
        javax.servlet.jsp.PageContext pageContext = _jspx_page_context;
        javax.servlet.jsp.JspWriter out = _jspx_page_context.getOut();
        //  c:url
        org.apache.taglibs.standard.tag.rt.core.UrlTag _jspx_th_c_005furl_005f0 = (org.apache.taglibs.standard.tag.rt.core.UrlTag) _005fjspx_005ftagPool_005fc_005furl_0026_005fvalue_005fnobody.get(org.apache.taglibs.standard.tag.rt.core.UrlTag.class);
        _jspx_th_c_005furl_005f0.setPageContext(_jspx_page_context);
        _jspx_th_c_005furl_005f0.setParent(null);
        // /regist.jsp(26,16) name = value type = null reqTime = true required = false fragment = false deferredValue = false expectedTypeName = null deferredMethod = false methodSignature = null
        _jspx_th_c_005furl_005f0.setValue("/VerifyCodeServlet");
        int _jspx_eval_c_005furl_005f0 = _jspx_th_c_005furl_005f0.doStartTag();
        if (_jspx_th_c_005furl_005f0.doEndTag() == javax.servlet.jsp.tagext.Tag.SKIP_PAGE) {
          _005fjspx_005ftagPool_005fc_005furl_0026_005fvalue_005fnobody.reuse(_jspx_th_c_005furl_005f0);
          return true;
        }
        _005fjspx_005ftagPool_005fc_005furl_0026_005fvalue_005fnobody.reuse(_jspx_th_c_005furl_005f0);
        return false;
      }
    
      private boolean _jspx_meth_c_005furl_005f1(javax.servlet.jsp.PageContext _jspx_page_context)
              throws java.lang.Throwable {
        javax.servlet.jsp.PageContext pageContext = _jspx_page_context;
        javax.servlet.jsp.JspWriter out = _jspx_page_context.getOut();
        //  c:url
        org.apache.taglibs.standard.tag.rt.core.UrlTag _jspx_th_c_005furl_005f1 = (org.apache.taglibs.standard.tag.rt.core.UrlTag) _005fjspx_005ftagPool_005fc_005furl_0026_005fvalue_005fnobody.get(org.apache.taglibs.standard.tag.rt.core.UrlTag.class);
        _jspx_th_c_005furl_005f1.setPageContext(_jspx_page_context);
        _jspx_th_c_005furl_005f1.setParent(null);
        // /regist.jsp(40,14) name = value type = null reqTime = true required = false fragment = false deferredValue = false expectedTypeName = null deferredMethod = false methodSignature = null
        _jspx_th_c_005furl_005f1.setValue("RegistServlet");
        int _jspx_eval_c_005furl_005f1 = _jspx_th_c_005furl_005f1.doStartTag();
        if (_jspx_th_c_005furl_005f1.doEndTag() == javax.servlet.jsp.tagext.Tag.SKIP_PAGE) {
          _005fjspx_005ftagPool_005fc_005furl_0026_005fvalue_005fnobody.reuse(_jspx_th_c_005furl_005f1);
          return true;
        }
        _005fjspx_005ftagPool_005fc_005furl_0026_005fvalue_005fnobody.reuse(_jspx_th_c_005furl_005f1);
        return false;
      }
    
      private boolean _jspx_meth_c_005furl_005f2(javax.servlet.jsp.PageContext _jspx_page_context)
              throws java.lang.Throwable {
        javax.servlet.jsp.PageContext pageContext = _jspx_page_context;
        javax.servlet.jsp.JspWriter out = _jspx_page_context.getOut();
        //  c:url
        org.apache.taglibs.standard.tag.rt.core.UrlTag _jspx_th_c_005furl_005f2 = (org.apache.taglibs.standard.tag.rt.core.UrlTag) _005fjspx_005ftagPool_005fc_005furl_0026_005fvalue_005fnobody.get(org.apache.taglibs.standard.tag.rt.core.UrlTag.class);
        _jspx_th_c_005furl_005f2.setPageContext(_jspx_page_context);
        _jspx_th_c_005furl_005f2.setParent(null);
        // /regist.jsp(57,13) name = value type = null reqTime = true required = false fragment = false deferredValue = false expectedTypeName = null deferredMethod = false methodSignature = null
        _jspx_th_c_005furl_005f2.setValue("/VerifyCodeServlet");
        int _jspx_eval_c_005furl_005f2 = _jspx_th_c_005furl_005f2.doStartTag();
        if (_jspx_th_c_005furl_005f2.doEndTag() == javax.servlet.jsp.tagext.Tag.SKIP_PAGE) {
          _005fjspx_005ftagPool_005fc_005furl_0026_005fvalue_005fnobody.reuse(_jspx_th_c_005furl_005f2);
          return true;
        }
        _005fjspx_005ftagPool_005fc_005furl_0026_005fvalue_005fnobody.reuse(_jspx_th_c_005furl_005f2);
        return false;
      }
    }
  • 相关阅读:
    VintaSoft Barcode.NET SDK专业的条形码读写控件详细介绍
    数据库查询构建控件集Active Query Builder
    linux系统下集成开发工具选择
    linux常用命令
    ubuntu 10.10绚丽桌面效果及常用快捷键
    Restful服务1
    EasyJF简易的j2EE框架
    erlang学习1
    netbeans下 xdebug调试php
    介绍几个开源的网站
  • 原文地址:https://www.cnblogs.com/insistence/p/6274988.html
Copyright © 2011-2022 走看看