MyEclipse+MySQL制作简单的登录注册系统详细步骤、代码、注释(⼆)接上篇*本篇 含代码
然后⼩⽣创建了⼀个名为login的web project。
五:创建⽂件
1.开始创建jsp⽂件,注意:创建jsp⽂件势在Webroot⽂件夹下创建的,因为我们要实现登录注册系统,⾸先要有界⾯,我创建了login.jsp 和register.jsp,然后分别有两个jsp和数据库进⾏交互,check-login.jsp和check-register.jsp,当然,因为jsp是html和java结合的⽹页设计语⾔,所以为了页⾯好看⼀些,我⽤来⼀些js和css,具体各个⽂件代码如下:
因为要验证是否登录/注册成功也需要⼀个可以看见的页⾯,所以我还创建了⼀个succeed.jsp,分别和check-login.jsp和check-
register.jsp连接,
2.导⼊jar包
上⼀篇中,myeclipse和mysql连接时需要下载⼀个mysql的jar包,那么这⾥项⽬需要和数据库建⽴连接也同样还要⽤到那个jar包,我们把下载好的包直接导⼊到webroot>>web-inf>>lib⽂件夹中(如上图)就可以了。
六:码代码:(代码中有部分注释)
//login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录案例</title>
<link rel="stylesheet"
href="store.css"
type="text/css">
</head>
<body>
<div class="container">
<div class="row" >
<div class="col-md-4 col-md-offset-4">
<div class="panel panel-primary">
<div class="panel-heading">
登录
</div>
<div class="panel-body">
<form action="check-login.jsp" method="POST" name="loginForm">
<div class="form-group">
<label for="name">⽤户名</label><input type="text"
class="form-control" name="name" placeholder="请输⼊⽤户名">
</div>
<div class="form-group">
<label for="">密码</label><input type="password"
class="form-control" name="password" placeholder="请输⼊密码">
</div>
<button type="submit" class="btn btn-primary"
onclick="return checkForm()">登录</button>
&nb sp;
<a class="btn btn-primary"
href="localhost:8080/webstore/registerpage.jsp">注册</a>
</form>
</div>
</div>
</div>
</div>
</div>
</body>
mysql存储过程与触发器<script
src="login.js"
integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"/*为了防⽌ CDN 篡改 javascript ⽤的。*/
crossorigin="anonymous">
</script>
<script type="text/javascript">
function checkForm() {
var name = loginForm.name.value;
var password = loginForm.password.value;
//alert(name + password);
if(name ==""|| name ==null) {
alert("请输⼊⽤户名");
loginForm.name.focus();
return false;
} else if(password ==""|| password==null) {
alert("请输⼊密码");
loginForm.password.focus();
return false;
}
return true;
c语言数组初始化的原理}
</script>
</html>
/
/check-login.jsp
<%@page import="java.sql.*"%>
mysql是什么系统<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
Connection conn =null;
PreparedStatement ps =null;
ResultSet rs =null;
Class.forName("sql.jdbc.Driver");
String url ="jdbc:mysql://localhost:3306/webstore";
//定义mysql的连接,我是有⼀个webstore的数据库,所以最后为webstore
String username ="root";
String password ="123456";
//mysql的⽤户名和密码
conn = Connection(url, username, password);
request.setCharacterEncoding("utf-8");
String name = Parameter("name");
入门教程视频免费String pwd = Parameter("password");//传递来⾃login页⾯的信息
String sql="SELECT *FROM user WHERE name ='"+ name +"'AND password = '"+ pwd +"'";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
()) {
response.sendRedirect("../succeed.jsp");
}else{
response.sendRedirect("./login.jsp");//这⾥是登录失败的显⽰页⾯,我把它直接⼜连接到登录页⾯
}
%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注册</title>
<link rel="stylesheet"
href="store.css"
type="text/css">
</head>
<body>
<div class="container">
<div class="row" >
<div class="col-md-4 col-md-offset-4">
<div class="panel panel-primary">
<div class="panel-heading">
注册
</div>
<div class="panel-body">
<form action="check-register.jsp" method="POST" name="loginForm">
//表单,连接到check-register.jsp
<div class="form-group">
<label for="name">⽤户名</label><input type="text"
class="form-control" name="name" placeholder="请输⼊⽤户名">
</div>
<div class="form-group">
递归函数必须有一个明确的结束条件<label for="">密码</label><input type="password"
class="form-control" name="password" placeholder="请输⼊密码">
</div>
<div class="form-group">
<label for="">确认密码</label><input type="text"
class="form-control" name="refill" placeholder="请再次输⼊密码">
</div>
<button type="submit" class="btn btn-primary"
onclick="return checkForm()">确定注册</button>
<button class="btn btn-primary"type="reset" name=rere value="重填">重填</button>
&nb sp;
</form>
</div>
</div>
</div>
</div>
</div>
</body>
<script
src="login.js"
integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
crossorigin="anonymous">
</script>
<script type="text/javascript">
function checkForm() {
var name = loginForm.name.value;
var password = loginForm.password.value;
var refill =fill.value;
//alert(name + password);
if(name ==" "|| name ==null) {
alert("请输⼊⽤户名");
loginForm.name.focus();
return false;
} else if(password ==""|| password==null) {
微服务特点是什么alert("请输⼊密码");
loginForm.password.focus();
return false;
} else if(password!=refill){
alert("前后密码输⼊不⼀致,请重新输⼊!");
location.href="localhost:8080/login/register.jsp";
//让其重新填写,我不知道如何只是清空后⾯的密码栏,所以直接重新到达注册页,还望⼤师赐教!
return false;
}
return true;
}
</script>
</html>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论