<%....%>可以理解为局部的,当前页面只会这一部分只会运行一次,这里不能写函数
<%!...%>可以理解为全局的,每一次页面的刷新,都会用到上一次的遗留下来的值
<%=...%>原封不动地输出里面的内容
三种注释类型:
A:<!--....--%> 可以让注释显示在html中 B:<%--.....--%> C:<%/*.... */%>
例如:
<% int accessCounts=0 %>
<%= ++accessCount s%> 不论你刷新页面多少次,当前页面里accessCounts值只会为1;
<%! int accessCount=0%>
<%= ++accessCount %> 当你不断的刷新页面,accessCount的值就会不断的递增;
写一个简单的HelloWorld
<html> <head> <title>HelloWorld</title> </head> <body> <% out.println("HelloWorld"); %> </body> </html>
动态改变背景颜色
<html> <head> <title>Color Testing</title> </head> <!-- html注释 --> <% String bgColor = request.getParameter("bgColor"); boolean hasExplicitColor; if(bgColor!=null){ hasExplicitColor=true; }else{ hasExplicitColor=false; bgColor="white"; } %> <body bgColor =<%= bgColor %>> <h2 align="center">Color Testing</h2> <% if(hasExplicitColor){ out.println("You supplied an explicit background color of"+bgColor+"."); }else{ out.println("Using default background color of WHITE."+ "Supply the bgColor request attribute to try"+ "a standard color,an RPGGBB value,or to see"+ "if your browser supports x11 color names"); } %> </body> </html>
打印一些信息
<html> <head> <title>JSP Expressions</title> </head> <body> <h2>JSP Expressions</h2> <ul> <li>Current time:<%=new java.util.Date()%></li> <li>Your hostname:<%= request.getRemoteHost()%></li> <li>Your session ID:<%=session.getId()%></li> <li>The testParam form parameter:<%= request.getParameter("testParam")%></li> </ul> </body> </html>