转载

jsp-基础语法

<% // 运行错误 int result = 10 / 0; %>

设置如果出现错误,跳转到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.*" %>

Hello World!

<%! // 数据库驱动程序 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(); } %>

包含指令

网页分为头尾中

静态包含

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

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

动态包含

<%--
  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" %>
<% String username = "LiHua"; %>

动态传递参数

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

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

跳转指令

此跳转为服务端跳转

即一个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" %>

这是跳转后的页面

<%=request.getParameter("name")%>

<%=request.getParameter("info")%>

<%--
  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>

jsp-基础语法

未分类

原文  https://www.iming.info/2020/04/18/jsp-基础语法/
正文到此结束
Loading...