JSP指令
- JSP指令只有三个
(1)page
aotuFlush:自动刷新
contentType:页面文本类型 “text/html”
errorPage:如果存在错误页面,就跳转至指定的错误页面,可以在web.xml里面配置
language:页面使用的语言 jsp中使用的语言默认java
pageEncoding:页面编码
import:导jar包 因为jsp本质就是一个servlet。
(2)include:导入其他页面到本页,两个页面合一
file:要导入的页面,一般都是不完整的网页,只包含部分
(3)taglib:标签库 - include指令
header.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<div>
<h1>头部信息</h1>
</div>
footer.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<div>
<h1>尾部信息</h1>
</div>
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<%@include file="common/header.jsp"%>
<h1>我是index页面</h1>
<%@include file="common/footer.jsp"%>
</body>
</html>
- web.xml下的error-page
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<error-page>
<location>/error/404.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<!--
location:错误页面的地址
-->
<location>/error/500.jsp</location>
</error-page>
<error-page>
<exception-type>java.lang.NullPointerException</exception-type>
<location>/error/500.jsp</location>
</error-page>
</web-app>
- jsp:include
<jsp:include page="/common/header.jsp"/>
<h1>tag标签</h1>
<jsp:include page="/common/footer.jsp"/>
- jsp:forward jsp:param
tag.jsp
<jsp:forward page="index.jsp">
<jsp:param name="username" value="haoyifan"/>
<jsp:param name="age" value="18"/>
</jsp:forward>
index.jsp
名字:
<%=request.getParameter("username")%>
年龄:<%=request.getParameter("age")%>
- userbean
<jsp:useBean id="student" class="com.baidu.Student" scope="page"/>
<%
student.setAge(1000);
student.setName("郝一凡");
%>
<%=student.getAge()%>
<%=student.getName()%>
//等价于
<jsp:useBean id="student" class="com.baidu.Student" scope="page"/>
<jsp:setProperty name="student" property="age" value="2"/>
<jsp:setProperty name="student" property="name" value="辛卫东"/>
<jsp:getProperty name="student" property="name"/>
<jsp:getProperty name="student" property="age"/>
实体类
package com.baidu;
public class Student {
private String name;
private int age;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
@Override
public String toString() {
return super.toString();
}
}