1、用户在登录界面输入用户名、密码; 2、若用户名、密码正确,则显示欢迎登录信息; 用jsp实现
一只小可爱
2024-06-25 07:04:17
最佳回答
1、在web工程中的src下创建包entity-类dao。代码如下:package entity;import j**a.sql.connection;import j**a.sql.drivermanager;import j**a.sql.resultset;import j**a.sql.statement;public class dao {/* * jdbc直连sql server2005;数据库logindb: * * * 由于static修饰的变量和方法在建的对象**享,加之使用此程序的人可能很多,所以将不变的、 * 并且常用的变量和方法修饰为static *///jdbc驱动名称public static string driver="com.microsoft.sqlserver.jdbc.sqlserverdriver";//数据库登录用户名public static string user="sa";//数据库登录密码public static string password="sa";//数据库名称public static string dbname="logindb";//jdbc连接urlpublic static string url="jdbc:sqlserver://localhost:1433;databasename="+dbname;/* * 获取数据库连接 */public static connection getconnection(){//建议在使用变量前先声明,后在使用时创建connection con=**;try{//加载驱动class.forname(driver);//连接con = drivermanager.getconnection(url,user,password);}catch(exception e){system.out.println("连接数据库失败!");e.printstacktrace();}return con;}/* * 断开数据库连接 */public static void closeconnection(connection con){//建议在使用变量前先声明,后在使用时创建try{con.close();}catch(exception e){system.out.println("关闭数据库失败!");e.printstacktrace();}}/* * 查询数据库验证用户名和密码 * * * 表account的字段名:name,password都是varchar(30) * * * */public int search(string name,string password){int flag=-1,i;//-1表示错误;0用户名不存在;1用户名存在,密码错误;2用户名和密码都正确connection con;statement stmt=**;resultset rs=**;string sql="";con=dao.getconnection();try{stmt=con.createstatement();sql="select * from account where name='"+name+"'";rs=stmt.executequery(sql);if(!rs.next()){flag=0;}else{rs.close();sql="select * from account where name='"+name+"' and password='"+password+"'";rs=stmt.executequery(sql);if(!rs.next()){flag=1;}else{flag=2;}}}catch(exception e){e.printstacktrace();flag=-1;}dao.closeconnection(con);return flag;}}2、在web工程-webroot下创建index.html和**_index.jsp.①、index,html全部代码:<!doctype html public "-//w3c//dtd html 4.0//en"><html><head><title>主页</title><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="th** ** my page"><meta http-equiv="content-type" content="text/html; charset=utf-8"><!--<link rel="stylesheet" type="text/css" href="./styles.css">--></head><body><div><form action="**_index.jsp" method="post"><label>用户名:</label><input type="text" name="nametxt"/><label>密码:</label><input type="password" name="passwordtxt"/><input type="submit" value="登陆"/><input type="reset" value="重置"/></form></div></body></html>②、**_index.jsp全部代码如下:<%@ page language="j**a" import="j**a.util.*,entity.dao"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><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="th** ** my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body><%string name = request.getparameter("nametxt");string password = request.getparameter("passwordtxt");int flag;dao dao = new dao();flag = dao.search(name, password);if (flag == 0) {out.println("用户不存在! ");} else if (flag == 1) {out.println("密码错误! ");} else if (flag == 2) {out.println("欢迎登录信息! ");} else {out.println("出现错误! ");}%></body></html><!--作者:聊城职业技术学院10软件陆勇-->特别提醒:此属于jdbc直连数据库,需要特殊的类包支持;需要数据库 20210311