<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'loginDeal3.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<!-- 引入jar包在此处 -->
<%@ page import="java.sql.*"%>
</head>
<body>
<%
//1.获取页面的用户,密码
String name = request.getParameter("userName");
String pwd = request.getParameter("userPwd");
//2.数据库连接
//**************重复使用代码区域可是********************
//2.1:将mysql的驱动类放到驱动类管理器(管理中心)进行注册
Class.forName("com.mysql.jdbc.Driver");
//2.2:链接mysql,需要必备三个参数:数据库位置,登录账号,登录密码
String url="jdbc:mysql://localhost:3306/stu2db";
String user="root";
String password="root";
//2.3:获得链接connection
Connection conn = DriverManager.getConnection(url,user,password);
//2.4编写sql查询语句,写法为:
String sql="selct * from tbuser Where nama=? and password= ?";
//2.5执行获取的preparedstatemment对象
PreparedStatement pst = conn.prepareStatement(sql);
//2.6给占位符?处赋值
pst.setObject(1,name);
pst.setObject(1,pwd);
//2.7执行查询,返回结果集
ResultSet rs=pst.executeQuery();
//*********重复使用代码区域结束*****************
//3.判断用户是否合法
While (rs.next()); {
//获取rs中的用户,密码
String rs_Name=rs.getString("name");
String rs_pwd= rs.getString("password");
//校验合法性
if(name.equals(rs_name)&& pwd.equals(rs_pwd)) {
out.print("<script> window.alert('登录成功');</script>");
out.print("<script> window.location.href='main.jsp';('登录成功');</script>");
}else{
out.print("<script> window.alert('登录失败');</script>");
out.print("<script> window.location.href='login.jsp';('登录失败');</script>");
}
}
%>
</body>
</html>