• JSP入门详解


    一、基础知识详解

      1.JSP中使用html注释<!-- -->客户端可见,使用jsp注释<%-- --%>客户端不可见(查看源文件只看到空行),单行和多行注释也看不到(//或者/*  */)

      直接在body里面//或者/* */是不幸的,需要在<%%>里面或者<%! %>

      2.page include taglib指令

      pageEncoding是JSP页面本身的编码,contentType是服务器端发送给客户端时候的编码。

      3.JSP脚本和JSP声明

      JSP脚本

    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    

     脚本的本质是插入到servlet里的service方法。

    例如:
     <% 
      if (user != null ) { 
     %>
      Hello <B><%=user%></B>
     <% 
      } else { 
     %>
      You haven't login!
     <%
      }
     %>
     转译成:
     if (user != null ) { 
      out.println("Hello <B>" + user + "</B>"); 
     } else {  
      out.println("You haven't login!"); 
     }

     JSP声明

    <%!
        String s = "aa";
        int res(int a ,int b) {
            return a+b;
        }
    %>
    

      注意:生命的本质是将生命的变量加入到servlet勒种。

      4.JSP表达式

      调用上面的JSP声明

    你好<%= s%> <br/> //表达式后面没有分号
    x+y=<%=add(90,8)%> <br/>
    

     注意:表达式里不能有分号,另外函数必须要有返回值,不能是void,表达式的本质是JSP页面转换成servlet后使用out.println("aa")将对应的值输出。

      5.JSP生命周期

      如果是第一次请求JSP页面,那么tomcat调用jspInit()方法将JSP页面转录成字节码文件,在tomcat下的work下的Catalina目录下可以看到对应的.class文件,如果不是第一次那么会调用jspService方法来显示,每次修改jsp页面,.class会重新编译。

     二、指令和脚本方式调用声明

     1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
     2 <%
     3 String path = request.getContextPath();
     4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
     5 %>
     6 
     7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
     8 <html>
     9   <head>
    10     <base href="<%=basePath%>">
    11     
    12     <title>My JSP 'index.jsp' starting page</title>
    13     <meta http-equiv="pragma" content="no-cache">
    14     <meta http-equiv="cache-control" content="no-cache">
    15     <meta http-equiv="expires" content="0">    
    16     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    17     <meta http-equiv="description" content="This is my page">
    18     <!--
    19     <link rel="stylesheet" type="text/css" href="styles.css">
    20     -->
    21   </head>
    22   <body>
    23     <h1>九九乘法表</h1> <br>
    24     
    25     <%!
    26         //表达式方式 
    27         String printMultiTable() {
    28             String s = "";
    29             //必须要有int,否则提示变量未声明,无法解析
    30             for(int i=1; i<=9; i++) {
    31                 for(int j=1; j<=i; j++) 
    32                 //原来一直提示错误,下面的这行注释里有个%的结束标记,
    33                 //黄色和上面的匹配了,删了几哈好了
    34                 //不能"<%=i*j"这里面是函数和变量
    35                     s += i+"*" +j+ "=" +(i*j) + "&nbsp&nbsp&nbsp";
    36                 s += "<br/>";
    37             }
    38             return s;
    39         }    
    40         //out对象是JspWriter的实例化
    41         void printMultiTableByEx(JspWriter out)throws Exception {
    42             for(int i=1; i<=9; i++) {
    43                 for(int j=1; j<=i; j++) 
    44                     out.println(i+"*" +j+ "=" +(i*j) + "&nbsp&nbsp&nbsp"); 
    45                 out.println("<br/>");
    46             }
    47         }
    48      %>     
    49      <hr>
    50      <p>表达式方式调用声明</p><br/>
    51      <%=printMultiTable()%>
    52      <p>脚本方式调用声明</p>
    53      <!-- 脚本方式需要有分号 -->
    54      <%printMultiTableByEx(out);%>
    55   </body>
    56 <ml>

  • 相关阅读:
    3.5---用栈实现队列(CC150)
    3.3---集合栈(CC150)
    3.2---最小栈(CC150)
    3.1---一个数组实现三个栈(CC150)
    2.7---判断链表是否是回文(CC150)
    SpringCloud实战5-Feign声明式服务调用
    Spring Cloud(二):服务注册与发现 Eureka【Finchley 版】
    Spring Cloud Eureka 自我保护机制
    Spring Cloud Edgware新特性之七:可选的EnableDiscoveryClient注解
    Spring Cloud 声明式服务调用 Feign
  • 原文地址:https://www.cnblogs.com/hxsyl/p/5552035.html
Copyright © 2020-2023  润新知