赞
踩
JSP的注释不会在客户端显示,HTML会。
EL表达式会自动过滤不存在的值。
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <html>
- <head>
- <title>Title</title>
- </head>
- <body>
- <%--输出Java变量或表达式的值--%>
- <%=new java.util.Date()%> <%--<%=%>等价于${}--%>
-
- <%--嵌入Java代码--%>
- <%
- int sum=0;
- for(int i=0;i<100;i++)
- sum+=i;
- out.print("<h1>"+sum+"<h1>");
-
- %>
-
- <%int x=10;
- out.println(x);
- %>
- <%out.println(x+1);
- %>
-
- <%--在代码中嵌入HTML元素--%>
- <%
- for(int i=0;i<5;i++)
- {
- %>
- <h1>Hello <%=i%></h1>
- <%
- i++;
- }
- %>
- <%--以上所有的内容都放在_jspService方法中,而下面的内容直接放在类中--%>
- <%--JSP声明,可以定义别的方法,也可以定义全局变量--%>
- <%!
- static
- {
- System.out.println("Loading");
- }
- private int glovalVar=0;
-
- public void f()
- {
- System.out.println("Enter f");
- }
- %>
-
- </body>
- </html>

使用公共头和公共尾:
- <html>
- <body>
- <h2>Hello World!</h2>
-
- <%--web标签,会将三个页面合而为一,这时候如果在不同的页面定义同样的变量就会报错--%>
- <%@include file="common/header.jsp"%>
- <img src="images/lblue.png" alt="Not Found">
- <%
- int i=0;
- %>
- <%@include file="common/footer.jsp"%>
-
- <%--jsp标签,会拼接三个页面,但是本质上还是三个页面--%>
- <%--<jsp:include page="/common/header.jsp"/>
- <img src="images/login.png"/>
- <jsp:include page="common/footer.jsp"/>--%>
- </body>
- </html>

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。