要求
tomcat8.0、mysql
需要一个mysql的驱动器,放在WEB-INFO的lib。版本号如下:

数据库文件
在mysql数据库中建立一个名字叫db_01的数据库。在数据库的建立一个名字叫tb_user的表,表格的字段为UName,Pwd。

index.jsp

<%@ 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</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><center><font face = "宋体" size = "6" color = "#000">欢迎JSP</font><hr><div><img alt="" width = "600" height = "400" src="D:\Images.jpg"></div><table width = "200" border ="1" bordercolor = "#00F"><tr><!--<td><input type = "button" value = "登      陆" onclick = "window.location.href('login.jsp')"></td>---><td><input type = "button" value = "登      陆" onclick = "window.open('login.jsp')"></td><td><input type = "button" value = "注      册" onclick = "window.open('register.jsp')"></td></tr> </table></center></body>
</html>

login.jsp


<%@ 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</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">--><body><center><font face="楷体" size="6" color="#000" >登录界面</font><%  String flag = request.getParameter("errNo");  try{if(flag!=null)out.println("用户名不存在或密码错误");}catch(Exception e){e.printStackTrace();}%><form action = "loginCh.jsp" method="post"><table width="300" height = "180" border="5" bordercolor="#A0A0A0"> <tr><th>账  户:</th><td><input type="text" name="name"  value = "请输入用户名" maxlength = "16" onfocus = "if(this.value == '请输入用户名') this.value =''"></td></tr><tr><th>密  码:</th><td><input type="password" name="pwd" maxlength = "20"></td></tr><tr><td colspan = "2" align = "center"><input type="submit" name="submit" value="登       录"><input type="button" value="返       回""window.location.href('/webText')"></td></tr></table></form></center></body>
</html>

checkRegister.jsp


<%@ page language="java" import="java.util.*,java.sql.*" 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</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 user = new String(request.getParameter("username").getBytes("ISO-8859-1"),"UTF-8");  String pwd = request.getParameter("password");String driverClass = "com.mysql.jdbc.Driver";String url = "jdbc:mysql://localhost:3306/db_01";String username = "root";String password = "root";Class.forName(driverClass);//加载驱动 Connection conn = DriverManager.getConnection(url,username,password);//得到连接PreparedStatement pStmt = conn.prepareStatement("select * from tb_user where UName = '" + user + "'");ResultSet rs = pStmt.executeQuery();if(rs.next()){out.println("<script language='javascript'>alert('该用户已存在,请重新注册!');window.location.href='register.jsp';</script>");}else{PreparedStatement tmt = conn.prepareStatement("Insert into tb_user values('" + user + "','" + pwd + "')");int rst = tmt.executeUpdate();if (rst != 0){out.println("<script language='javascript'>alert('用户注册成功!');window.location.href='index.jsp';</script>");  }else{out.println("<script language='javascript'>alert('用户注册失败!');window.location.href='register.jsp';</script>");  }}%></body>
</html>

loginCh.jsp


<%@ page language="java" import="java.util.*,java.sql.*" 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</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 user = new String(request.getParameter("username").getBytes("ISO-8859-1"),"UTF-8");  String pwd = request.getParameter("password");String driverClass = "com.mysql.jdbc.Driver";String url = "jdbc:mysql://localhost:3306/db_01";String username = "root";String password = "root";Class.forName(driverClass);//加载驱动 Connection conn = DriverManager.getConnection(url,username,password);//得到连接PreparedStatement pStmt = conn.prepareStatement("select * from tb_user where UName = '" + user + "'");ResultSet rs = pStmt.executeQuery();if(rs.next()){out.println("<script language='javascript'>alert('该用户已存在,请重新注册!');window.location.href='register.jsp';</script>");}else{PreparedStatement tmt = conn.prepareStatement("Insert into tb_user values('" + user + "','" + pwd + "')");int rst = tmt.executeUpdate();if (rst != 0){out.println("<script language='javascript'>alert('用户注册成功!');window.location.href='index.jsp';</script>");  }else{out.println("<script language='javascript'>alert('用户注册失败!');window.location.href='register.jsp';</script>");  }}%></body>
</html>

register.jsp

<%@ 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</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">--><script>function addCheck(){var username=document.getElementById("username").value;var password=document.getElementById("password").value;var newword=document.getElementById("newword").value;if(username==""){alert("用户名不能为空!");document.getElementById("username").focus();  return false;}if(password==""){alert("密码不能为空!");document.getElementById("password").focus();return false;}if(password != newword){alert("两次输入密码不相同!");document.getElementById("newword").focus();return false;}}function validate(){var flag = addCheck();if(flag == false)return false;return true;}</script><body><center><font face="楷体" size="6" color="#000">注册界面</font><form action = "checkRegister.jsp" method = "post" onsubmit = "return validate()"><table width="300" height = "180" border="5" bordercolor="#A0A0A0"><tr><th>用户名:</th><td><input type="text" name="username" value="输入16个字符以内" maxlength = "16" onfocus = "if(this.value == '输入16个字符以内') this.value =''"></td></tr><tr><th>输入密码:</th><td><input type="text" name="password" value="输入20个字符以内" maxlength = "20" onfocus = "if(this.value == '输入20个字符以内') this.value =''"></td></tr><tr><th>确认密码:</th><td><input type="text" name="newword" value="重新输入密码" maxlength = "20" onfocus = "if(this.value == '重新输入密码') this.value =''"></td></tr><tr><td colspan = "2" align = "center"><input type="submit" value="注  册">    <input type="reset" value="重  置"></td></tr></table></form></center></body>
</html>

success.jsp


<%@ 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>Feilong_登录成功</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><center><%String name = new String(request.getParameter("username").getBytes("8859_1"));out.println("欢迎你:" + name);%><br><a href="login.jsp">重新登陆</a></center></body>
</html>

jsp连接数据库实现注册登陆(超级详细)相关推荐

  1. JSP连接数据库实现注册登录

    1.使用单例来连接数据库     //数据库的连接使用单例来连接一次即可,执行完sql语句后如果没有结束就不能关流 不然后边就不能再去执行sql语句 public class JDBCUtils1 { ...

  2. JSP连接数据库实现注册登录(附带上传头像)

    完成登录程序,可进行注册,需进行头像的上传,登录成功之后,可进行注销和进行个人信息修改,并且可以修改头像. 公共方法: 文件自动取名FileNameAuto package cn.lizhi.pub; ...

  3. jsp mysql完整注册登陆_JSP+MySQL最简单的登录和注册的实现 --Java Web温习

    一.开发环境 开发工具:eclipse 2018-09 操作系统:win10 二.实现 1.目录结构: 2.数据库(创建tmp数据库,新建user表,user表结构如下) 3.功能简介 功能比较简单, ...

  4. 编程小白的计算机毕业设计指导开发教程-JSP+Servlet实现注册登陆功能

    一.Servlet+JSP+JavaBean开发模式(MVC)介绍 Servlet+JSP+JavaBean模式(MVC)适合开发复杂的web应用,在这种模式下,servlet负责处理用户请求,jsp ...

  5. JSP连接数据库进行注册登录及展示页面及下拉框查询

    思路:在进入展示页面时 可以进行查询数据库 并展示出来再进行下拉框的构造 先写出dao层的接口 int insert(String username,String password,int age); ...

  6. Java如何实现手动连接数据库(Mysql或Oracle) | 超级详细,建议收藏

  7. php注册登录描述,基于PHP实现用户登录注册功能的详细教程

    教程前先给大家看看小编的实现成果吧! 图1: 图2: 图3: 教程: 实现这个功能我们需要五个php文件: login.php (登录界面,如图2) 登录 登录页面 用户名: required=&qu ...

  8. 超级详细配置SSM (Intellij idea + Maven + Spring + SpringMVC + MyBatis + c3p0 )

    时间2018/12/31,使用的包都是最新的和用的人最多的QAQ. Maven仓库查找包的地址 https://mvnrepository.com/ 在经历了70多个小时的奋斗后(花了60个小时学习了 ...

  9. Android Studio超级详细安装教程(AMD)

    Android Studio超级详细安装教程(AMD) !!!必看!!! 本教程适合CPU为AMD的计算机使用,如果你的CPU为intel/英特尔请移步至Android Studio超级详细安装教程( ...

最新文章

  1. 作为管理者的你,如何用人?一点建议给你
  2. PHP遇到json解决的两个办法,转为数组,直接取值
  3. 《WinForm开发系列之控件篇》Item28 LinkView(暂无)
  4. 阿里大数据分析与应用(part2)--大数据分析的流程与常用技术
  5. freemarker 对null 的处理
  6. 排序算法对比、总结(Python代码)
  7. linux如何杀死进程最快,如何在Linux系统中杀掉内存消耗最大的进程?
  8. 几款远程工具介绍(Xshell)(SecureCRT)(putty)
  9. 转载---KMP算法(Matrix67原创)
  10. 最少拦截系统,简单dp,(学长说这是贪心?!。。。。。。也是醉了)
  11. C++之boost库报错:note: in expansion of macro BOOST_MPL_ASSERT_NOT
  12. Python安装包下载、环境配置与工具包安装教程(详细版)
  13. 关于图像格式jpg、gif、png、svg、bmp、WebP的区别以及应用场景
  14. 做了一款DTU,网络通信模块化,代码开源,可以二次开发
  15. 机器学习(3)——有监督学习
  16. 编程灵异事件簿(一)
  17. 伊利诺伊计算机专业本科好入吗,伊利诺伊大学香槟分校本科有计算机专业吗?...
  18. 一次从ZoomeyeAPI脚本编写到提权的***实践
  19. 考拉验数(自动化验数)设计方案和实现
  20. 如何编写接口测试用例

热门文章

  1. 逻辑和编程语言(PTIME 的逻辑/命题和谓词逻辑)
  2. 安装Anaconda3时遇到的问题
  3. Facebook android 第三方登录
  4. Elastix GOIP 网关配合
  5. MATLAB:线条属性
  6. excel中提取月份_在Excel中自动添加月份表
  7. 【R语言实验】聚类分析
  8. kdj超卖_KDJ指标的超买与超卖
  9. 安装linux后错误1962,安装mysql出错make[2]: *** [mysql.o] 错误 1???
  10. 《C语言内核深度解析》——笔记及拓展(1)