JSP作为WEB的开发基础,有其重要的地位,那么熟练掌握JSP的语法及应用就成了重中之重。

首先我们一起先从JSP的基本语法学起:(以下内容来自李兴华视频手稿整理)

scriptlet简介

 

Script表示的是脚本小程序,像之前out.println()这个语句是缩写在<%%>之中的,很明显,这里面 缩写的语句就是一个script. 

在jsp中最重要的部分就是Scriptlet(脚本小程序),所有嵌入在HTML代码中的java程序都必须使用

Scriplet标记出来,在jsp中一共有三种scriplet代码

  •    第一种:<% %>,在此scriplet中可以定义局部变量、编写语句;
  •    第二种:<% ! %>, 在此scriplet中可以定义全局变量、方法、类;
  •    第三种:<% = %>,用于输出一个变脸或一个具体内容。

 

第一种script<%%>


  1. <html> 
  2. <head> 
  3. <body> 
  4. <
  5.      int x=10
  6.      String info="www.baidu.com"
  7.     out.println("<h2>x="+x++ +"</h2>"); 
  8.     out.println("<h2>info="+info+"</h2>"); 
  9.  %> 
  10. </body> 
  11. </head> 
  12. </html> 

将纯java代码插入到jsp页面中时要放在<%%>

 

 

第二种script<%!%>


 

 

主要的功能是定义全局变量、方法、类,假设下面定义方法和类,进行验证 

 

 

 

  1. <%! 
  2.       public static final String info="www.baidu.com"
  3.  %> 
  4.  <%! 
  5.        public int add(int x,int y){ 
  6.          return x+y; 
  7.       } 
  8.  %> 
  9.  <%! 
  10.      class Person{ 
  11.        private String name; 
  12.        private int age; 
  13.        public Person(String name,int age){ 
  14.            this.name=name; 
  15.            this.age=age;  
  16.        } 
  17.        public String toString(){ 
  18.            return "name="+this.name+";age="+ 
  19.                this.age; 
  20.        } 
  21.     } 
  22.  %> 
  23.  <
  24.         out.println("<h3>info="+info+"</h3>"); 
  25.        out.println("<h3>3+5="+add(3,5)+"</h3>"); 
  26.        out.println("<h3>"+new Person("zhengsan",30)+"</h3>"); 
  27.  %> 

 

 如果现在在<%!%>中定义的是变量的话,那么则表示的是全局变量,每次刷新时都不会重复声明 

 

 

  1. <%! 
  2.       public static final String info="www.baidu.com"
  3.      int  x=10
  4.  %> 
  5.  <
  6.     out.println("<h2>" + x++ +"</h2>"); 
  7.  %> 
  8.  <%! 
  9.        public int add(int x,int y){ 
  10.          return x+y; 
  11.       } 
  12.  %> 
  13.  <%! 
  14.      class Person{ 
  15.        private String name; 
  16.        private int age; 
  17.        public Person(String name,int age){ 
  18.            this.name=name; 
  19.            this.age=age;  
  20.        } 
  21.        public String toString(){ 
  22.            return "name="+this.name+";age="+ 
  23.                this.age; 
  24.        } 
  25.     } 
  26.  %> 
  27.  <
  28.       out.println("<h3>info="+info+"</h3>"); 
  29.       out.println("<h3>3+5="+add(3,5)+"</h3>"); 
  30.        out.println("<h3>"+new Person("zhengsan",30)+"</h3>"); 
  31.  %> 

 

一般而言,使用<%!%>的操作都是声明全局常量较多,而所谓的定义方法或定义类基本基本上是不适用的,也没有必要这么去做

第三种 Script: <%=%>

 

这种Script主要的功能是用于输出的操作,可以输出一个变量或一个具体的内容

 

  1. <
  2.     String info="www"
  3.        int temp=30
  4.  %> 
  5.  <h3>info=<%=info%></h3> 
  6.  <h3>temp=<%=temp%></h3> 
  7.  <h3>name=<%="zhaoyuqiang"%></h3> 

 

问题:   现在如果要输出之前学习过的out.println,现在又学习了<%=%> 该使用哪一种更好呢?

那么我们说呢  在这种情况下呢 最好是使用<%=%>比较方便。

 

out.println();这种方法在jsp中完全摒弃掉

下面两个程序是打印表格的,分别用了out.println()和<%=%>两种方法:


  1. <html> 
  2.  <head> 
  3.  <title>zhaoyuqiangde javaweb开发</title> 
  4.  </head> 
  5.  <body> 
  6.  <
  7.       int rows=10
  8.      int cols=10
  9.      out.println("<table border=\"1\" width=\"100%\">"); 
  10.      for(int x=0;x<rows;x++){ 
  11.          out.println("<tr>"); 
  12.          for(int y=0;y<cols;y++){ 
  13.            out.println("<td>"+(x*y)+"</td>");  //用了out.println()
  14.          } 
  15.          out.println("</tr>"); 
  16.      } 
  17.      out.println("</table>"); 
  18.  %> 
  19.  </body> 
  20.  </html> 

 

这种输出最直接的问题就是HTML代码和java代码完全混合在了一起

   而且生成的代码也不舒服,没有缩进

最关键的是jsp的内容是要显示给用户看的,美工是需要修改的,而美工不懂程序,只能使用工具。

 

  1. 使用表达式<%=%>
  2. <html> 
  3.  <head> 
  4.  <title>zhaoyuqiangde javaweb开发</title> 
  5.  </head> 
  6.  <body> 
  7.  <
  8.       int rows=10
  9.      int cols=10
  10.  %> 
  11.     <table border="1" width="100%"> 
  12.  <
  13.      for(int x=0;x<rows;x++){ 
  14.  %> 
  15.     <tr> 
  16.  <
  17.          for(int y=0;y<cols;y++){ 
  18.  %> 
  19.     <td bgcolor="#oocc33"><%=x*y%></td> 
  20.  <
  21.          } 
  22.  %> 
  23.         </tr> 
  24.  <
  25.      } 
  26.  %> 
  27.      </table> 
  28.   
  29.  </body> 
  30.  </html> 

 

 

  

 

 

 此种操作虽然代码复杂了,但是达到了一点,HTML代码页java相分离了

所以一定要注意的是,以后在使用输出的时候,坚决的不再使用out.pirntln(0),而使用<%=%>

 

 

在jsp中,这三种方法贯穿始终,可以说是最简单的了