page指令是jsp中的一个重要的属性,使用此属性可以定义一个jsp页面的相关属性,其中有MIME类型,定义需要导入的包,错误页面的指定,等等。
page指令的常用属性有:
对于以上的指令,只有import指令可以重复出现,其他指令只能出现一次。
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ page errorPage="error.jsp"%> <% int result = 100/0; //此处发生错误,跳转到error页面 %>
<%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%> <%@ page isErrorPage="true"%> <body> <h2>404,请求的页面找不到!</h2> </body>
<%@ page contentType="text/html" pageEncoding="GBK"%> <%@ page import="java.sql.*"%> <html> <head> <title>list</title> </head> <body> <%!public static final String DBDRIVER = "com.mysql.jdbc.Driver"; public static final String DBURL = "jdbc:mysql://localhost:3306/test"; public static final String DBUSER = "root"; public static final String DBPASS = "zhengzehui";%> <% Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; %> <% try { Class.forName(DBDRIVER); conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS); String sql = "SELECT id,name,sex,age FROM student"; pstmt = conn.prepareStatement(sql); rs = pstmt.executeQuery(); %> <center> <table border="1" width="80%"> <tr> <td>ID</td> <td>姓名</td> <td>性别</td> <td>年龄</td> </tr> <% while (rs.next()) { int id = rs.getInt(1); String name = rs.getString(2); String sex = rs.getString(3); int age = rs.getInt(4); %> <tr> <td><%=id%></td> <td><%=name%></td> <td><%=sex%></td> <td><%=age%></td> </tr> <% } %> </table> </center> <% } catch (Exception e) { System.out.println(e); } finally { rs.close(); pstmt.close(); conn.close(); // 如果直接关闭连接也可以 } %> </body> </html>