JSP第七次作业
用户表: uid (主键,自动增长) uname upwd
使用分层实现注册。(必做)
使用分层实现登录。(选做)
1.实体类Bean.java
package gkd; public class Bean { private int id; private String uname; private String upwd; //访问器 public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getUpwd() { return upwd; } public void setUpwd(String upwd) { this.upwd = upwd; } //构造器 public Bean(int id, String uname, String upwd) { super(); this.id = id; this.uname = uname; this.upwd = upwd; } public Bean() { super(); } }
2.数据库操作类Dao.java
package gkd; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; public class Dao { //用户数据访问类 //添加用户 public int addBean(Bean b){ int i=0; try { //加载驱动 Class.forName("com.mysql.jdbc.Driver"); //建立连接 Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/homweb", "root", "root"); //写SQL语句 String sql="insert into boss(uname,upwd) values(?,?)"; //执行 PreparedStatement ps=con.prepareStatement(sql); //ps.setInt(1, b.getId()); ps.setString(1, b.getUname()); ps.setString(2, b.getUpwd()); i=ps.executeUpdate(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return i; }//登录 public int denBean(Bean b){ int i=0; ResultSet rs =null; try { //加载驱动 Class.forName("com.mysql.jdbc.Driver"); //建立连接 Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/homweb", "root", "root"); //写SQL语句 String sql="select uname,upwd from boss where uname=? and upwd=?"; //执行 PreparedStatement ps=con.prepareStatement(sql); //ps.setInt(1, b.getId()); ps.setString(1, b.getUname()); ps.setString(2, b.getUpwd()); rs = ps.executeQuery(); if(rs.next()){ System.out.print("用户登录成功"); i=1; }else{ System.out.print("用户登录失败"); i=0; } rs.close(); ps.close(); con.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return i; } }
3.登录页面index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <% String s3 = (String)request.getParameter("code"); String rightcode = (String)session.getAttribute("rCode"); if(s3 != null && rightcode != null){ if(s3.equals(rightcode)){ session.setAttribute("code", s3); // pageContext.forward("seccess.jsp"); }else{ out.println("验证码输入不正确,请重新输入!"); } } %> <html> <head> <title>登录页面</title> <script type="text/javascript"> function zC(){ sessionStorage.removeItem("uname"); sessionStorage.removeItem("upwd"); window.location.href = "zhu.jsp"; } </script> </head> <body style="background:url(image/why.png)"> <form action="control.jsp" method="post"> <div align="center"> <table> <tr> <td>用户名:</td> <td><input type="text" name="uname" size="18"></td> </tr> <tr> <td>密码:</td> <td><input type="password" name="upwd" size="19"></td> </tr> <tr> <td> 验证码: </td> <td> <input type="text" name="code" size="18"><br/> </td> <td> <img src="num.jsp" width="60px" height="40px" /></td> <td> <a href="num.jsp" >换一张</a><br></td> </tr> <tr> <td colspan="2" align="center"> <input type="submit" value="登录"> <button type="button" name="button2" value="注册" onClick="zC()">注册</button> </td> </tr> </table> </div> </form> </body> </html>
5.跳转页面control.jsp
<%@page import="gkd.Dao"%> <%@page import="gkd.Bean"%> <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% Bean b = new Bean(); /* int id = Integer.parseInt(request.getParameter("id")); b.setId(id); */ String uname = request.getParameter("uname"); b.setUname(uname); String upwd = request.getParameter("upwd"); b.setUpwd(upwd); String s3 = (String)request.getParameter("code"); Dao d=new Dao(); if(d.denBean(b)>0){ //登录成功 session.setAttribute("uname",uname); session.setAttribute("upwd",upwd); session.setAttribute("code",s3); // session.setAttribute("rCode",s3); response.sendRedirect("seccess.jsp"); }else{ response.sendRedirect("fail.jsp"); } %>
6.注册页面zhu.jsp
%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<style>
h1{
color: blue;
}
</style>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body bgcolor="pink">
<div align="center">
<h1>新用户注册</h1>
<form action="ce.jsp" method="post">
<table >
<tr align="right">
<td>请输入用户名:</td>
<td><input type="text" name=uname autofocus="autofocus"></td>
</tr>
<tr align="right">
<td>请输入密码:</td>
<td><input type="text" name=upwd></td>
</tr>
<tr align="right">
<td>请输入确认密码:</td>
<td><input type="text" name=refill></td>
</tr>
</table>
<input type="submit" name=register value="注册" >
<input type="reset" name=refill value="重填" >
</form>
</div>
</body>
</html>
注册跳转ce.jsp
<%@page import="gkd.Dao"%> <%@page import="gkd.Bean"%> <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% Bean b = new Bean(); /* int id = Integer.parseInt(request.getParameter("id")); b.setId(id); */ String uname = request.getParameter("uname"); b.setUname(uname); String upwd = request.getParameter("upwd"); b.setUpwd(upwd); // String s3 = (String)request.getParameter("code"); Dao d=new Dao(); if(d.addBean(b)>0){ //跳转注册成功页面 session.setAttribute("uname",uname); session.setAttribute("upwd",upwd); // session.setAttribute("code",s3); // session.setAttribute("rCode",s3); response.sendRedirect(request.getContextPath()+"/zhucon.jsp"); }else{ //错误页面 response.sendRedirect("fail.jsp"); } %>
登录成功seccess.jsp和注册成功zhucon.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <style> h1{ color: blue; } span{ color: green; } </style> <script type="text/javascript"> function zX(){ sessionStorage.removeItem("uname"); sessionStorage.removeItem("upwd"); window.location.href = "index.jsp"; } </script> <head> <title>welcome</title> </head> <body bgcolor=#ffccff> <% String s1 = (String)session.getAttribute("uname"); String s2 = (String)session.getAttribute("upwd"); String s3 = (String)session.getAttribute("code"); %> <div align="center"> <h1>WECLOME</h1> <h1> 欢迎登录</h1> 你输入的登录信息 <table> <tr> <td width="80" height="20">用户名:</td> <td> <h2><span><%=s1%></span></h2></td> </tr> <tr> <td height="20">密 码:</td> <td> <h2><span><%=s2%></span></h2></td> </tr> <tr> <td height="20">验证码:</td> <td><%=s3%></td> </tr> </table> <button type="button" name="button1" value="退出" onClick="zX()">退出</button> </div> </body> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <style> h1{ color: blue; } span{ color: green; } </style> <script type="text/javascript"> function zX(){ sessionStorage.removeItem("uname"); sessionStorage.removeItem("upwd"); window.location.href = "index.jsp"; } </script> <head> <title>welcome</title> </head> <body bgcolor=#ffccff> <% String s1 = (String)session.getAttribute("uname"); String s2 = (String)session.getAttribute("upwd"); %> <div align="center"> <h1>WECLOME</h1> <h1> 欢迎注册</h1> 你输入的注册信息 <table> <tr> <td width="80" height="20">用户名:</td> <td> <h2><span><%=s1%></span></h2></td> </tr> <tr> <td height="20">密 码:</td> <td> <h2><span><%=s2%></span></h2></td> </tr> </table> <button type="button" name="button1" value="退出" onClick="zX()">返回登录</button> </div> </body> </html>
失败页面fail.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <style> h1{ color: red; } h2{ color: blue; } </style> <head> <title>fail</title> <script type="text/javascript"> function zX(){ sessionStorage.removeItem("uname"); sessionStorage.removeItem("upwd"); window.location.href = "index.jsp"; } </script> </head> <body> <div align="center"> <h1>请检查信息</h1> <h2>用户名:<%=request.getSession().getAttribute("uname")%></h2> <h2>密码:<%=request.getSession().getAttribute("upwd")%></h2> <button type="button" name="button1" value="注销" onClick="zX()">注销</button> </div> </body> </html>
操作过程截图
点击注册
注册成功
返回登录,登录成功
数据库newfile.sql语句
CREATE TABLE `boss` ( `id` int PRIMARY KEY AUTO_INCREMENT, `uname` varchar(20), `upwd` varchar(20) ) insert into boss(uname,upwd) values('dd','879'); SET FOREIGN_KEY_CHECKS = 1;