内容简介:jsp使用此属性,可以定义一个jsp页面的相关属性设置该页面的类型为text/html类型,其中编码为Unicode 类型的UTF-8 语言为java
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>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Writing Apache Modules with Perl and C
Lincoln Stein、Doug MacEachern / O'Reilly Media, Inc. / 1999-03 / USD 39.95
Apache is the most popular Web server on the Internet because it is free, reliable, and extensible. The availability of the source code and the modular design of Apache makes it possible to extend Web......一起来看看 《Writing Apache Modules with Perl and C》 这本书的介绍吧!
正则表达式在线测试
正则表达式在线测试
HEX CMYK 转换工具
HEX CMYK 互转工具