Java第六次作业
1.安装MySQL数据库,建立用户表 uid uname upwd 并插入3条数据
2.制作jsp登录页面 login.jsp 提交到dologin.jsp,使用jdbc连数据库,判断输入的用户名密码是否存在
3.如果存在,把用户名保存在SESSION中,跳转到welcome.jsp,welcome.jsp中读取session中的用户名,显示欢迎你xxx
4.若不存在,跳到登录页面。
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
<style type="text/css">
body{
background-color: azure;
}
form{
position: fixed;
top: 250px;
left: 650px;
background-color: #ADD8E6;
border-radius: 5px;
}
div{
font-size: 22px;
margin-top: 20px;
padding-left: 40px;
}
input{
width: 200px;
height: 20px;
border-radius: 5px;
}
#tj{
width: 80px;
height: 30px;
margin-left: 140px;
border-radius: 5px;
font-size: 16px;
margin-bottom: 20px;
}
</style>
</head>
<body>
<form action="/school/Login.jsp" method="post">
<div>账 号: <input type="text" name="username"></div>
<div>密 码: <input type="text" name="password"></div>
<div>验 证 码:<input type="text" name = "yanzhengma"> <img src="getcode" alt="看不清,换一张" onclick="change(this)"/></div>
<div><input type="submit" id="tj" value="登录" ></div>
</form>
</body>
<script type="text/javascript">
function change(img) {
img.src = "getcode?"+new Date().getTime();
}
</script>
</html>

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="java.sql.*" %><%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 'dologin.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"> --> </head> <body> <% String Name = request.getParameter("user"); //获取输入的用户名 String ps = request.getParameter("password"); // 密码 String code = request.getParameter("code"); //验证码 Name.trim(); //去空格 ps.trim(); PreparedStatement pre = null; Connection con =null; Statement sql; ResultSet rs; request.setCharacterEncoding("utf-8"); try{ Class.forName("com.mysql.jdbc.Driver"); }catch(Exception e){ out.print("<h1>加载错误"+e); } String user = "root"; String password="root"; con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test",user,password); try{ sql = con.createStatement(); String SQLL = "select * from user where uname=? and upwd=?"; pre=con.prepareStatement(SQLL); pre.setString(1,Name); pre.setString(2,ps); rs = pre.executeQuery(); if(rs.next()){ String na = rs.getString(2); session.setAttribute("uname",na); response.sendRedirect("welcome.jsp"); }else{ %> <script > //弹窗提示 alert('输入密码或用户名错误'); </script> <% response.sendRedirect("index.jsp"); } }catch(SQLException e){ out.print("<h1>查询错误"+e); } %> </body></html>
<%@ 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 'welcome.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"> --> </head> <body> 欢迎<%=session.getAttribute("uname") %>! </body></html>

浙公网安备 33010602011771号