实验6.docx
- 文档编号:9529514
- 上传时间:2023-02-05
- 格式:DOCX
- 页数:29
- 大小:496.73KB
实验6.docx
《实验6.docx》由会员分享,可在线阅读,更多相关《实验6.docx(29页珍藏版)》请在冰豆网上搜索。
实验6
实验六基于JavaBean的身份验证与管理
时间:
4月30地点:
科技楼423
1、2班星期三上午10:
10-11:
50
实验内容:
自己在sqlserver数据库建立一表admins
列名
数据类型
长度
说明
username
varchar
20
姓名
password
varchar
10
密码
然后
(1)编程连接数据库,
(2)将下面的bean代码,以及相关jsp代码,实现用户登录,对密码的修改,增加用户的,删除用户等功能
1、与数据连接的DoDB.java
importjava.sql.*;
publicclassDoDB{
ConnectionCn;//连接对象,用于建立和管理数据库连接
StatementSt;//语句对象,用于执行SQL命令,完成数据库操作
ResultSetRs;//记录集对象,用于保存查询结果
publicDoDB(){//初始化数据库操作对象
try{
Stringurl="jdbc:
sqlserver:
//localhost:
1433;user=sa1;password=123";
Cn=DriverManager.getConnection(url);
this.St=this.Cn.createStatement();
this.Rs=null;
}catch(Exceptionex){
ex.printStackTrace();
}
}
publicStringdoUpdate(Stringsql){//执行SQL添加、删除和修改等更新命令
try{
this.St.executeUpdate(sql);
return"";
}catch(Exceptionex){
ex.printStackTrace();
returnex.getMessage();
}
}
publicResultSetdoQuery(Stringsql){//执行SQL查询命令,返回查询结果
try{
this.Rs=this.St.executeQuery(sql);
}catch(Exceptionex){
ex.printStackTrace();
}
returnthis.Rs;
}
publicvoidclose(){//执行清理操作
try{
if(!
this.Rs.wasNull()){
this.Rs.close();
this.Rs=null;
}
if(!
this.St.isClosed()){
this.St.close();
this.St=null;
}
if(!
this.Cn.isClosed()){
this.Cn.close();
this.Cn=null;
}
}catch(Exceptionex){
ex.printStackTrace();
}
}
}
2、用户信息UserCheck.java
publicclassUserCheck{
StringuserName="";//存储用户名
StringuserPassword="";//存储口令
booleanpassed=false;//存储用户是否通过登录验证
publicbooleanisPassed(){
returnpassed;
}
publicvoidsetPassed(booleanpassed){
this.passed=passed;
}
publicStringgetUserName(){
returnuserName;
}
publicvoidsetUserName(StringuserName){
this.userName=userName;
}
publicStringgetUserPassword(){
returnuserPassword;
}
publicvoidsetUserPassword(StringuserPassword){
this.userPassword=userPassword;
}
}
3、主登录界面index.jsp
<%@pagecontentType="text/html"pageEncoding="gb2312"%>
functionNoSpace(){
if(document.all.userName.value==""){
alert("用户名不能为空!
");
returnfalse;
}
if(document.all.userPassword.value==""){
alert("口令不能为空");
returnfalse;
}
returntrue;
}
用户名:
口令:
4密码检查checklog.jsp
<%@pagecontentType="text/html"pageEncoding="gb2312"%>
<%@pageimport="java.sql.*"%>
<%
request.setCharacterEncoding("gb2312");//设置请求信息使用的字符集
%>
useBeanid="db"class="ch09.DoDB"/> useBeanid="user"scope="session"class="ch09.UserCheck"/> setPropertyname="user"property="*"/> <%--user保存用户登录信息,注意jsp: setProperty用*代替属性名, --这样可自动用同名参数给属性赋值。 --user对象的作用范围为session,即在整个会话期间都有效。 --%> <% if(user.getUserName()! =""){ Stringsql="select*fromadminswhereusername='"+user.getUserName() +"'andpassword='"+user.getUserPassword()+"'"; ResultSetrs=db.doQuery(sql);//查询用户名和口令 if(rs.next()){//如果有返回记录,说明用户名和口令正确 user.setPassed(true);//设置登录标识 response.sendRedirect("viewdata.jsp");//跳转到数据显示页面 }else{//用户名和口令不正确,输入提示信息。 out.println(" } } db.close(); %> 用户名: value=" getPropertyname='user'property='userName'/>"size="20"/> 口令: value=" getPropertyname='user'property='userPassword'/>"size="21"/>
");
functionNoSpace(){
if(document.all.userName.value==""){
alert("用户名不能为空!
");
returnfalse;
}
if(document.all.userPassword.value==""){
alert("口令不能为空");
returnfalse;
}
returntrue;
}
5、显示系统用户信息网页viewdata.jsp
<%@pagecontentType="text/html"pageEncoding="gb2312"%>
<%@pageimport="java.sql.*"%>
useBeanid="db"class="ch09.DoDB"/> useBeanid="user"scope="session"class="ch09.UserCheck"/>
functiondodelete(username){
//显示删除确认提示对话框,在用户确认删除时,跳转到删除页面
if(confirm("确认删除用户:
"+username)==true){
//每条记录的username为关键字,所以将其作为删除参数
window.location="deletedata.jsp?
username="+username;
}
}
<%
response.setHeader("Expires","0");
if(!
user.isPassed()){
//如果用户没有登录,跳转到登录页面
response.sendRedirect("http:
//localhost:
8080/ch09");
}
ResultSetrs=db.doQuery("select*fromadmins");//返回数据库用户信息
out.println("当前系统用户信息如下:
");
out.println(" out.println(""); ");用户名 口令 编辑 删除
out.println("
");Stringun,pwd,str;
while(rs.next()){
un=rs.getString("username");
pwd=rs.getString("password");
out.println("
out.println(un);
out.println("
out.println(pwd);
out.println("
str=" username="+un+"&password="+pwd+">编辑"; out.println(str); out.println("
//str="
str=" username="+un+">删除"; out.println(str); out.println("
}
out.println("
");db.close();
%>
6、删除用户的网页deletedata.jsp
<%@pagecontentType="text/html"pageEncoding="gb2312"%>
<%@pageimport="java.sql.*"%>
useBeanid="db"class="ch09.DoDB"/> useBeanid="user"scope="session"class="ch09.UserCheck"/> <% if(! user.isPassed()){ //如果用户没有登录,跳转到登录页面 response.sendRedirect("http: //localhost: 8080/chapter04BeanDB/"); } request.setCharacterEncoding("gb2312"); Stringsql="deletefromadminswhereusername='"+request.getParameter("username")+"'"; Stringrel=db.doUpdate(sql); db.close(); if(rel.isEmpty()) //db.doUpdate方法返回空字符串说明更新操作成功完成 response.sendRedirect("viewdata.jsp"); else out.println("无法将数据存入数据库: "+rel);//操作无法完成,输出出错信息 %> 7、修改用户信息的网页editdata.jsp <%@pagecontentType="text/html"pageEncoding="gb2312"%> <%@pageimport="java.sql.*"%>
useBeanid="user"scope="session"class="ch09.UserCheck"/> <% if(! user.isPassed()){ //如果用户没有登录,跳转到登录页面 response.sendRedirect("http: //localhost: 8080/chapter04BeanDB/"); } request.setCharacterEncoding("gb2312"); %> 用户名: value="<%=request.getParameter("username")%>"size="20"/> 口令: value="<%=request.getParameter("password")%>"size="21"/> value="<%=request.getParameter("username")%>"/>
packageaaaaaa;
importjava.sql.*;
publicclassDoDB{
ConnectionCn;//连接对象,用于建立和管理数据库连接
StatementSt;//语句对象,用于执行SQL命令,完成数据库操作
ResultSetRs;//记录集对象,用于保存查询结果
publicDoDB(){//初始化数据库操作对象
try{
Stringurl="jdbc:
sqlserver:
//localhost:
1433;user=sa1;password=123";
Cn=DriverManager.getConnection(url);
this.St=this.Cn.createStatement();
this.Rs=null;
}catch(Exceptionex){
ex.printStackTrace();
}
}
publicStringdoUpdate(Stringsql){//执行SQL添加、删除和修改等更新命令
try{
this.St.executeUpdate(sql);
return"";
}catch(Exceptionex){
ex.printStackTrace();
returnex.getMessage();
}
}
publicResultSetdoQuery(Stringsql){//执行SQL查询命令,返回查询结果
try{
this.Rs=this.St.executeQuery(sql);
}catch(Exceptionex){
ex.printStackTrace();
}
returnthis.Rs;
}
publicvoidclose(){//执行清理操作
try{
if(!
this.Rs.wasNull()){
this.Rs.close();
this.Rs=null;
}
if(!
this.St.isClosed()){
this.St.close();
this.St=null;
}
if(!
this.Cn.isClosed()){
this.Cn.close();
this.Cn=null;
}
}catch(Exceptionex){
ex.printStackTrace();
}
}
}
packageaaaaaa;
publicclassUserCheck{
StringuserName="";//存储用户名
StringuserPassword="";//存储口令
booleanpassed=false;//存储用户是否通过登录验证
publicbooleanisPassed(){
returnpassed;
}
publicvoidsetPassed(booleanpassed){
this.passed=passed;
}
publicStringgetUserName(){
returnuserName;
}
publicvoidsetUserName(StringuserName){
this.userName=userName;
}
publicStringgetUserPassword(){
returnuserPassword;
}
publicvoidsetUserPassword(StringuserPassword){
this.userPassword=userPassword;
}
}
<%@pagecontentType="text/html"pageEncoding="gb2312"%>
functionNoSpace(){
if(document.all.userName.value==""){
alert("用户名不能为空!
");
returnfalse;
}
if(document.all.userPassword.value==""){
alert("口令不能为空");
returnfalse;
}
returntrue;
}