今天测试为一个3个网页的跳转问题
一个登录页面 一个输入页面 一个显示成功页面
其中也是连接数据库
提交数据向数据库
代码如下
登录
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<script language="JavaScript">
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
function isValidate(form)
{
// 得到用户输入的信息
userid = form.userid.value;
userpass = form.userpass.value;
// 判断用户名长度
if(!minLength(userid,6))
{
alert("用户名长度小于6位!");
form.userid.focus();
return false;
}
if(!maxLength(userid,8))
{
alert("用户名长度大于8位!");
form.userid.focus();
return false;
}
if(!maxLength(userid,8))
{
alert("用户名长度大于8位!");
form.userid.focus();
return false;
}
// 判断口令长度
if(!minLength(userpass,6))
{
alert("口令长度小于6位!");
form.userpass.focus();
return false;
}
if(!maxLength(userpass,8))
{
alert("口令长度大于8位!");
form.userpass.focus();
return false;
}
// 判断用户名和口令是否相同
if(userpass!="123456")
{
alert("口令错误!");
form.userpass.focus();
return false;
}
return true;
}
//验证是否满足最小长度
function minLength(str,length)
{
if(str.length>=length)
return true;
else
return false;
}
// 判断是否满足最大长度
function maxLength(str,length)
{
if(str.length<=length)
return true;
else
return false;
}
</script>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>学生登陆</title>
</head>
<body>
<h2>学生登录</h2>
<form name="form1" action = "Course.jsp" method="post"
onsubmit = "return isValidate(form1)">
用户名:<input type="text" name="id" ><br>
口令: <input type="password" name="password"><br>
<input type="reset" value="重置">
<input type="submit" value="提交"><br>
</form>
</body>
</html>
数据输入
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<script language="JavaScript">
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
function isValidate(form)
{
// 得到用户输入的信息
userteacher = form.teacher.value;
userwhere = form.where.value;
// 判断老师名称
if(!minLength(userteacher))
{
alert("教师名称错误!");
form.userteacher.focuqs();
return false;
}
// 判断教室名称
if(!minLength(userwhere))
{
alert("教室名称错误!");
form.userwhere.focus();
return false;
}
//验证教师名称
function minLength(str)
{
var str1 = "TeacherWangjianmin";
var str1 = "TeacherLiulijia";
var str1 = "TeacherLiudan";
var str1 = "TeacherWanghui";
var str1 = "TeacherYangziguang";
if(str==str1||str==str2||str==str3||str==str4||str==str5)
return true;
else
return false;
}
// 判断教室名称
function maxLength(str)
{
var str1 = "BasicTeachingBuilding";
var str2 = "TheFirstTeachingBuilding";
var str3 = "TheSecondTeachingBuilding";
var str4 = "TheThirdTeachingBuilding";
if(str==str1||str==str2||str==str3||str==str4)
return true;
else
return false;
}
</script>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>新课程添加</title>
</head>
<body>
<jsp:useBean id="id" class="Student.id"></jsp:useBean>
<jsp:setProperty name="id" property="*"/>
<form name="form1" action="daoru.jsp" method="post"
onsubmit = "return isValidate(form1)">
<table align="center">
<tr>
<td>姓名</td>
<td><input type="text" name="xingming">
</td>
</tr>
<tr>
<td>课程名称</td>
<td><input type="text" name="name">
</td>
</tr>
<tr>
<td>任课老师</td>
<td><input type="text" name="teacher">
</td>
</tr>
<tr>
<td>上课地点</td>
<td><input type="text" name="where">
</td>
</tr>
<tr>
<td><input type="hidden" name="id" value=<%=id.getId() %>>
</td>
</tr>
<tr>
<td><input type="submit" value="保存"></td>
</tr>
</table>
</form>
</body>
</html>
连接数据库并结束
<%@page import="Student.Courseadd"%>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>添加</title>
</head>
<body>
<jsp:useBean id="course" class="Student.Courseadd"></jsp:useBean>
<jsp:setProperty name="course" property="*"/>
<%
Connection conn =null;
try{
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
out.print("驱动器加载失败");
}
String url = ("jdbc:mysql://localhost:3306/course_web?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone = GMT&allowPublicKeyRetrieval=true");
String username = "root";
String password = "15531238359";
try{
conn = DriverManager.getConnection(url,username,password);
}catch (SQLException e){
out.println(e.getMessage());
out.println("数据库连接失败");
}
if(conn != null){
String sql = "insert into course(id,coursename,teacher,place,name)values(?,?,?,?,?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1,course.getId());
ps.setString(2,course.getName());
ps.setString(3,course.getTeacher());
ps.setString(4,course.getWhere());
ps.setString(5,course.getXingming());
int row = ps.executeUpdate();
if(row>0){
out.println("成功添加了" + row + "条数据!");
}
}
}catch (Exception e){
e.printStackTrace();
out.print("课程信息添加失败!");
}
%>
<br>
<a href="begin.jsp">返回</a>
</body>
</html>
其中还有两个类
package Student;
public class id {
String id;
String password;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
package Student;
public class id {
String id;
String password;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
其中大致的流程就是登录界面的一个以学生学号作为账号 密码默认都是123456
然后提交表单
之后也是正常的提交数据
这期间有一个就是第一页的学号要提交到第三个页面的数据录入数据库部分
这个地方我是使用了一个隐藏表单
将其隐藏在了第二页
然后提交到数据库
其他都是一点简单的内容