jsp

page指令

使用此属性,可以定义一个jsp页面的相关属性

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

设置该页面的类型为text/html类型,其中编码为Unicode 类型的UTF-8 语言为java

从Tomcat安装目录中可以找到可以使用的MIME类型

<mime-mapping>
    <extension>ait</extension>
    <mime-type>application/vnd.dvb.ait</mime-type>
</mime-mapping>

错误页设置

设置page指令的errorPage属性,设置错误页

<%--
  Created by IntelliJ IDEA.
  User: ming
  Date: 19-3-6
  Time: 下午9:29
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="application/vnd.dvb.ait;charset=UTF-8" language="java" %>
<%@ page errorPage="error.jsp" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
    <%
      // 运行错误
      int result = 10 / 0;
    %>

  </body>
</html>

设置如果出现错误,跳转到error.jsp文件

错误页的跳转属于服务器端跳转

数据库连接操作

maven安装驱动
访问网址

https://search.maven.org/artifact/mysql/mysql-connector-java/8.0.15/jar

输入依赖

<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>8.0.15</version>
</dependency>

jsp书写代码,并网页展示

<%@ page import="java.sql.*" %>
<html>
<body>
<h2>Hello World!</h2>
<%!
    // 数据库驱动程序
    public static final String DBDRIVER = "com.mysql.cj.jdbc.Driver";
    // 数据库连接地址
    public static final String DBURL = "jdbc:mysql://47.94.95.84:32786/test";
    // 用户名
    public static final String DBUSER = "root";
    // 密码
    public static final String DBPASS = "ABCcba20170607";
%>
<%
    // 声明数据库连接对象
    Connection connection = null;
    // 连接
    PreparedStatement preparedStatement = null;
    // 结果集
    ResultSet resultSet = null;
    String sql = "SELECT * from emp";
%>
<%
    try{
        Class.forName(DBDRIVER);
        // 数据库连接
        connection = DriverManager.getConnection(DBURL, DBUSER, DBPASS);
        // 实例化sql
        preparedStatement = connection.prepareStatement(sql);
        // 查询
        resultSet = preparedStatement.executeQuery();
        while(resultSet.next()){
            int empon = resultSet.getInt(1);
            String ename = resultSet.getString(2);
            String job = resultSet.getString(3);
            %>
                <%=empon%>
                <%=ename%>
                <%=job%>
            <%
        }
    }catch (Exception e){
        System.out.println(e);
    }finally {
        resultSet.close();
        preparedStatement.close();
        connection.close();
    }
%>
</body>
</html>

包含指令

网页分为头尾中

静态包含

在jsp编译的时候插入一个包含文本或代码的文件。包含过程为静态的,代码文件包含,生成一个完整的jsp文件,再次包含生成class文件,加载进入容器。

<html>
<body>
    <%@include file="info.html"%>
    <%@include file="info.inc"%>
</body>
</html>

动态包含

<%--
  Created by IntelliJ IDEA.
  User: ming
  Date: 19-3-9
  Time: 下午3:43
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
参数1<%=request.getParameter("name")%>
参数2<%=request.getParameter("info")%>
<%--
  Created by IntelliJ IDEA.
  User: ming
  Date: 19-3-9
  Time: 下午4:51
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <%
        String username = "LiHua";
    %>
    <h1>动态传递参数</h1>
    <jsp:include page="./receive_param.jsp">
        <jsp:param name="name" value="<%=username%>"/>
        <jsp:param name="info" value="ming"/>
    </jsp:include>
</body>
</html>

运行以后可以发现显示出结果

两种包含,静态包含是先包含,后处理,动态包含为先处理后包含

跳转指令

此跳转为服务端跳转
即一个request传递到另外一个page

<%--
  Created by IntelliJ IDEA.
  User: ming
  Date: 19-3-9
  Time: 下午5:14
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<h1>这是跳转后的页面</h1>
<h2><%=request.getParameter("name")%></h2>
<h2><%=request.getParameter("info")%></h2>
<%--
  Created by IntelliJ IDEA.
  User: ming
  Date: 19-3-9
  Time: 下午5:11
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String username = "LiHua";
%>
<jsp:forward page="forward_demo02.jsp">
    <jsp:param name="name" value="<%=username%>"/>
    <jsp:param name="info" value="ming"/>
</jsp:forward>