第六周作业
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" 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></title> <style type="text/css"> body{ font-size:13px; font-family:"宋体";} /*全局控制*/ body,form,input,p{ padding:0; margin:0; border:0;} /*重置浏览器的默认样式*/ form{ width:300px; height:200px; padding-top:20px; margin:60px auto; /*使表单在浏览器中居中*/ background:#2aa 6ea; /*为表单添加背景颜色*/ border-radius:30px; /*设置圆角边框*/ border:2px solid #4faccb; } p{ margin-top:20px; text-align:center; } p span{ width:30px; display:inline-block; text-align:right; } .num,.pass{ /*对文本框设置共同的宽、高、边框、内边距*/ width:152px; height:18px; border:1px solid #38a1bf; padding:2px 2px 2px 22px; } .num{ /*定义第一个文本框的背景、文本颜色*/ background:url(images/1.jpg) no-repeat 5px center #FFF; color:#999; } .pass{ /*定义第二个文本框的背景*/ background:url(images/2.jpag) no-repeat 5px center #FFF; } .btn01,.btn02{ width:60px; height:25px; border-radius:3px; /*设置圆角边框*/ border:1px solid #6b5d50; margin-left:30px; } .btn02{ background:#3aa7ea;} /*设置第一个按钮的背景色*/ </style> </head> <body> <form action="dologin.jsp" method="post" autocomplete="on"> <p> <span>账号:</span> <input type="text" name="user" class="num" /> </p> <p> <span>密码:</span> <input type="password" name="password" class="pass"/> </p> <p><span>验证码</span><input type="text" name="code"/>qwer</p> <p> <input type="submit" class="btn02" value="登录"/> </p> </form> </body> </html>
<%@page import="java.sql.ResultSet"%> <%@page import="java.sql.PreparedStatement"%> <%@page import="java.sql.DriverManager"%> <%@page import="java.sql.Connection"%> <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" import=" javax.servlet.http.HttpSession" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <% request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=utf-8"); response.setCharacterEncoding("UTF-8"); HttpSession s = request.getSession(); String username = request.getParameter("username"); String password1 = request.getParameter("password"); Class clazz = Class.forName("com.mysql.jdbc.Driver"); // 2.提供另外三个连接的基本信息 String url = "jdbc:mysql://localhost:3306/school"; String user = "root"; String password = "root"; Connection conn = DriverManager.getConnection(url, user, password); PreparedStatement ps = conn.prepareStatement("select uname,upwd from user where uname = ? and upwd = ?"); ps.setString(1, username); ps.setString(2, password1); ResultSet rs = ps.executeQuery(); if(rs.next()){ session.setAttribute("username", username); session.setAttribute("password", password1); response.sendRedirect("/school/success.jsp"); }else{ response.sendRedirect("/school/loginyanzheng2.jsp"); } %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> </body> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>title</title> </head> <body> <% String username = (String) request.getSession().getAttribute("username"); %> 欢迎你<%=username%> </body> </html>