一、创建一个web项目,参照JW/01_创建web项目及部署

二、在NAVICat 里建数据库 db_01,建表tb_user ,字段UName 、Pwd

三、在web下创建一个Directory, 设名字为JSPWorks

1. 创建jsp文件如图

代码如下:

1)shouye.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>
<div style="text-align: center;">
<span style="font-family: 宋体; font-size: x-large; color: #000; ">欢迎JSP</span><hr>
<%--<div>
<img alt="" width = "600" height = "400" src="">
</div>--%>
<table width = "200" border ="1" bordercolor = "#00F">
<td colspan = "2" align = "center"> <td><input type = "button" value = "登 陆" onclick = "window.open('JSPWorks/login.jsp')"></td>
<td><input type = "button" value = "注 册" onclick = "window.open('JSPWorks/register.jsp')"></td>
</tr>
</table>
</div>
</body>
</html>

2)login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%!
private Object Finally;
%><%
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>
<div style="text-align: center;">
<span style="font-family: 楷体; font-size: x-large; color: #000; ">登录界面</span>
<%
String flag = request.getParameter("errNo");
try{
if(flag!=null)
out.println("用户名为空或不存在或密码错误");
}catch(Exception e){
e.printStackTrace();
}
%>
<form action = "JSPWorks/loginCh.jsp">
<table width="300" height = "180" border="5" bordercolor="#A0A0A0">
<td colspan = "2" align = "center">
<th>账 户:</th>
<td><input type="text" name="user" value = "请输入用户名" maxlength = "16" onfocus = "if(this.value == '请输入用户名') this.value =''"></td>
</tr>
<td colspan = "2" align = "center">
<th>密 码:</th>
<td><input type="password" name="pwd" maxlength = "20"></td>
</tr>
<tr>
<td colspan = "2" align = "center">
<td><input type="submit" name="submit" value="登 录"></td>
<td><input type="button" value="返 回" onclick = "window.open('JSPWorks/shouye.jsp')"></td>
</td>
</tr>
</table>
</form>
</div>
</body>
</html>

3) 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("user").getBytes("ISO-8859-1"),"UTF-8");
String pwd = request.getParameter("pwd"); String driverClass = "com.mysql.cj.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/db_01";
String username = "root";
String password = "root";
try {
java.lang.Class.forName(driverClass);//加载驱动
Connection conn = DriverManager.getConnection(url, username, password);//得到连接 /* if(!conn.isClosed()) {
out.println("数据库连接成功!!");
}*/
PreparedStatement pStmt = conn.prepareStatement("select * from tb_user where UName = '" + user + "' and Pwd= '" + pwd + "'");
ResultSet rs = pStmt.executeQuery();
if(rs.next()) {
out.println("<br>用户:"+rs.getString(1)+"密码:"+rs.getString(2));
out.println("<script language='javascript'>alert('用户登录成功!将返回首页!');window.location.href='JSPWorks/shouye.jsp';</script>");
}else{
out.println("<script language='javascript'>alert('用户登录失败!将返回首页!');window.location.href='JSPWorks/shouye.jsp';</script>"); }
rs.close();
conn.close();
pStmt.close();
}
catch(ClassNotFoundException e){
System.out.println("数据库加载失败!");
e.printStackTrace();
}
catch(SQLException e1){
e1.printStackTrace();
}
catch(Exception e2){
e2.printStackTrace();
}
finally{
System.out.println("数据库获取成功!");
}
%>
</body>
</html>

4) 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>
<div style="text-align: center;">
<span style="font-family: 楷体; font-size: x-large; color: #000; ">注册界面</span>
<form action = "JSPWorks/checkRegister.jsp">
<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>
</div>
</body>
</html>

5) 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>检查注册</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.cj.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='JSPWorks/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='JSPWorks/shouye.jsp';</script>");
}else{
out.println("<script language='javascript'>alert('用户注册失败!');window.location.href='JSPWorks/register.jsp';</script>");
}
}
%>
</body>
</html>

2. 配置Tomcat

1)先run

2)

3)

4)

JavaWeb 07_创建web项目连接MySQL实现注册登录功能的更多相关文章

  1. Eclipse开发Web项目连接MySQL时找不到驱动的解决办法

    当我们使用Eclipse开发Web项目连接MySQL时后台报找不到驱动的错误,如下:解决办法: 1.这时我们首先要检查我们是否导入了连接MySQL数据库的jar包,如图,是否已经将jar包复制到项目下 ...

  2. idea中创建web项目搭建Hibernate框架连接oracle数据库

    hibernate框架 hibernate是数据化持久工具,也是一个开源代码的ORM解决方案.hibernate内部封装了通过jdbc访问数据库的操作,向商场应用提供面向对象的数据访问api. hib ...

  3. IntelliJ IDEA创建web项目及异常问题解决

    IDEA配置Tomcat: 1.下载Tomcat,本次使用的是apache-tomcat-6.0.43 2.IDEA配置Tomcat 在idea中的Settings(Ctrl+Alt+s)(或者点击图 ...

  4. maven 创建web项目的标准目录结构

      maven 创建web项目的标准目录结构 CreateTime--2018年4月18日21:05:37 Author:Marydon 1.标准目录介绍(开发目录) 2.在eclipse下,目录展示 ...

  5. SSM 框架-04-使用maven创建web项目

    SSM 框架-04-使用maven创建web项目 本篇介绍使用MAVEN来管理jar包,就不用一个一个去添加和下载jar包了,直接在maven配置文件中配置就可以了,maven可以帮助我们自动下载.本 ...

  6. Java Web工程连接MySQL数据库及Tomcat服务器页面中文乱码

    Java Web工程连接MySQL数据库 一. 准备工作 1.下载连接MySQL数据库的JDBC (可以去官网下,也可以去百度云找) 2.将下载的jar文件复制到Tomcat的lib目录下 3.新建一 ...

  7. eclipse maven创建web项目

    记录地址 jdk设置及文件包miss 实例下载地址 创建SSM整合项目 一.使用Eclipse中的maven插件创建web项目 1: 2: 3: 4: 5:maven web项目创建成功.(去掉ind ...

  8. 3.在eclipse中创建Web项目,并部署到Tomcat上

    1.找到创建web项目的菜单 2.创建web项目并选择web环境 3.查看创建好的web项目结构 4.在web项目的webContent文件夹下创建jsp页面 5.查看是否创建jsp页面成功,并编辑j ...

  9. 搭建django项目连接mysql数据库环境

    开通博客园这么久,即将写下第一篇博客,十分兴奋.首先了,庆祝自己写下了码农生涯博客园第一篇博客,其次了,庆祝自己经过了10个小时奋战,终于成功搭建django项目连接mysql数据库的环境.在此过程中 ...

随机推荐

  1. Ubuntu下pip3的安装、升级、卸载

    1.安装 sudo apt-get install python3-pip 2.升级 sudo pip3 install --upgrade pip 3.卸载 sudo apt-get remove ...

  2. JVM调优——JVM监控工具jvisualvm的使用及GC插件安装

    一.前言 在高并发的场景下,我们网站的的访问性能会降低,我们怎么优化,这是个问题!天天听JVM调优,实际上还是不知道怎么调优,调优也是看着网上说的修改一下JVM的堆的空间等等进行的.实际上我们应该在压 ...

  3. TCP/IP详解 读书笔记:TCP:传输控制协议

    TCP的服务 TCP为应用层提供一种面向连接的.可靠的字节流服务. 一个TCP连接中,仅有两方进行彼此通信,所以广播和多播不能用于TCP. TCP通过以下方式提供可靠性: 应用数据被切割为TCP认为最 ...

  4. 命令行与C++

    windows和Linux都接受使用命令行进行程序的运行,例如在windows下使用命令行命令.\notepad可以打开记事本,使用特定的命令行参数,如.\notepad 1.txt可以打开1.txt ...

  5. smbms项目核心功能实现

    SMBMS 数据库: 项目如何搭建? 考虑使用不使用Maven?依赖,Jar 1.项目搭建准备工作 搭建一个maven web项目 配置Tomcat 测试项目是否能够跑起来 导入项目中会遇到的jar包 ...

  6. Java基础——选择语句

    Java基础--选择语句     1. if语句 规律: 1. 首先计算表达式的值. 2. 若表达式为真,则执行对应语句,为假则不执行.   第一种: if(表达式) 语句;//多个语句可用{} 例如 ...

  7. .net 底层运行机制

    1.           CLR C#.NET 平台下,代码是怎么运行的 源代码-->托管模块-->程序集-JIT->编程CPU指令 1.1     在.NET框架下,首先将源代码编 ...

  8. Neo4j入门日志(一)导入数据

    本文主要来源于: neo4j的官方文档 使用的是neo4j官方提供的导入方式,即使用import,在cmd中进行导入. 1.导入的基本方式 bin/neo4j-admin import --datab ...

  9. TCP三次握手中SYN,ACK,seq ack的含义

    转至:https://www.cnblogs.com/muyi23333/articles/13841268.html 1.TCP 为什么三次握手而不是两次握手 1.防止已失效的连接请求又传送到服务器 ...

  10. JavaBean , EJB , spring , POJO

    1996年 , 发布了java bean 1.00-A  当时的java bean有什么用呢 javaBean最初是为Java GUI的可视化编程实现的.你拖动IDE构建工具创建一个GUI 组件(如多 ...