jsp 实栗 jsp + jdbc 实现登录
实现思路
一个表单页,输入用户登录和密码,然后信息提交到jsp页面进行验证,如果可以服务器跳转到登录成功页,失败,跳转到错误页
跳转的时候窗口的URL地址会发生变化
代码如下
编写登录代码
登录
<!DOCTYPE html>
<html lang="zh_CN">
<head><meta charset="UTF-8"><title>登录</title>
</head>
<body><h1>登录操作</h1><form action="login_check.jsp" method="post"><h1>用户登录</h1><p>登录id<input type="text" name="id"/></p><p>登录密码<input type="password" name="password"/></p><input type="submit" value="登录"/><input type="reset" value="重置"/></form>
</body>
</html>
登录处理
<%@ page import="java.sql.*" %>
<%--Created by IntelliJ IDEA.User: mingDate: 19-3-9Time: 下午5:50To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body>
<%!// 数据库驱动程序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;// 标志位boolean falge = false;// 用户真实姓名String name = null;
%>
<%try{Class.forName(DBDRIVER);// 获得连接connection = DriverManager.getConnection(DBURL, DBUSER, DBPASS);// 编写sql验证ID 密码String sql = "SELECT name FROM user WHERE userid = ? AND password = ?";// 实例化操作对象preparedStatement = connection.prepareStatement(sql);// 设置查询内容preparedStatement.setString(1, request.getParameter("id"));preparedStatement.setString(2, request.getParameter("password"));// 执行查询resultSet = preparedStatement.executeQuery();// 如果可以查询到,表示合法用户if(resultSet.next()){name = resultSet.getString(1);// 修改标志位falge = true;}}catch (Exception e){e.printStackTrace();}finally {try{resultSet.close();preparedStatement.close();connection.close();}catch (Exception e){e.printStackTrace();}}
%>
<%// 登录成功if(falge){// 进行服务器端跳转
%><jsp:forward page="./login_sucess.jsp"><jsp:param name="uname" value="<%=name%>"/></jsp:forward>
<%}else{
%><jsp:forward page="./login_failure.html"/>
<% }
%>
</body>
</html>
登录完成
<%--Created by IntelliJ IDEA.User: mingDate: 19-3-9Time: 下午10:22To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body>
<h1>登录成功</h1>
<%=request.getParameter("uname")%>
</body>
</html>
登录失败
<%--Created by IntelliJ IDEA.User: mingDate: 19-3-9Time: 下午10:22To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body>
<h1>登录成功</h1>
<%=request.getParameter("uname")%>
</body>
</html>
效果演示
登录界面
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: mingDate: 19-3-6Time: 下午9:29To 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);// 实例化sqlpreparedStatement = 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: mingDate: 19-3-9Time: 下午3:43To 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: mingDate: 19-3-9Time: 下午4:51To 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: mingDate: 19-3-9Time: 下午5:14To 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: mingDate: 19-3-9Time: 下午5:11To 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>