芝麻_糊

导航

JSP学习(2)---四种基本语法与三种编译指令

JSP的异常可以不处理,即使是checked异常。

四种基本语法:

jsp声明,jsp注释,jsp表达式,jsp脚本

三种编译指令:

page,include,taglib

下面是具体的练习。

show.jsp

<%-- 编译指令--%>
<%@page contentType="text/html;charset=UTF-8" language="java" errorPage="error.jsp"%>
<%@page info="this is a jsp page"%>
<html>
<head>
<title>
欢迎
</title>
</head>
<%-- 1.jsp声明变量和方法--%>
<%!
private int count;
private int num;
public String print(){
    return "hello";
}
%>
<body>

<%-- 2.jsp注释,不会出现在浏览器的源代码中。只在服务端。--%>
<!--html注释-->
你访问!
<%out.print(new java.util.Date());%>
<%for(int i=0;i<5;i++){
    out.print("<font size='"+i+"'>" );%>
    hello world</font>
    
    
<%}%>

<%-- 变量和方法使用--%>
<%out.print(count++);
%>
<%out.print(print());%>
<%-- 3.JSP表达式,尾部不能有分号--%>
<%=num++%>
<%=getServletInfo()%>

<table border="1">
<%-- 4.jsp脚本--%>
<%for(int j=0;j<8;j++){%>
<%!private int n;%>
<tr><td>n++</td></tr>
    
<%}%>

<%-- 这里出现异常,跳转向error.jsp--%>
<%!int m=7;
int p=4;
%>
<%=m/p%>
</table>
</body>
</html>

error.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" isErrorPage="true"%>
<html>
<head><title>异常页面</title></head>
<body>发生了内部异常</body>
</html>

include.jsp中包含show.jsp

<%-- 编译指令需要和被包含页面一直,否则出错--%>
<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@include file="show.jsp"%>
<html>
<head><title>包含页面</title></head>
<body>包含了show.jsp页面</body>
</html>

include指令:

包含于被包含页面出现指令冲突

 正常运行结果如下:

 

posted on 2018-01-02 14:38  芝麻_糊  阅读(626)  评论(0编辑  收藏  举报