servlet实操之jsp基础
jsp语法
- 任何语言都有自己的语法,java中有jsp,jsp作为java技术的一种应用,它拥有自己扩充的语法(了解,知道即可),java所有语法都支持。
jsp表达式:
- <%----%>
<%--jsp表达式
作用:用来将程序输出到客户端
<%= 变量或者表达式%>
--%>
<%= new java.util.Date() %>
jsp脚本片段:
<%--jsp教本片段--%>
<%
int sum = 0;
for (int i = 0; i <10 ; i++) {
sum++;
}
out.println("<h1>sum="+sum+"</h1>");
%>
脚本片段的再实现:
<%
int x = 10;
out.println(x);
%>
<p>这是一个jsp文档</p>
<%
int y = 2;
out.println(x);
%>
<hr>
<%--在代码中嵌入html--%>
<%
for (int i = 0; i < 5; i++) {
%>
<h1>hello,hello<%=i+1%></h1>
<%
}
%>
jsp声明:
<%--jsp声明--%>
<%!
static {
System.out.println("loding servlet");
}
private int globalvar = 0;
public void shiy(){
System.out.println("进入了shiy");
}
%>
jsp声明:会被编译到jsp生成的java类中,其它的会被生成到_jspService中。
在jsp嵌入Java代码即可。
<%%>
<%=%>
<%!%>
<%----%>
jsp指令:
//配置error页面 也可在web.xml中配置
<%@ page errorPage="./error/500.jsp" %>>
//拼接页面
<%--<%@ include 将页面合二为一 各个页面代码不可冲突否则500--%>
<%@ include file="/common/header.jsp"%>
<h1>我是主体</h1>
<%@ include file="/common/footer.jsp"%>
<hr>
<%--<jsp:include page 拼接页面本质还是三个,互不干扰,扩展性更强--%>
<jsp:include page="common/header.jsp"/>
<h1>我是主体</h1>
<jsp:include page="common/footer.jsp"/>