亲宝软件园·资讯

展开

javaweb实现登录注册

LUf_y_ 人气:0

1.工程文件

基本的文件布局,没使用任何css文件,就是简单实现登录注册,页面没有做任何美化;

使用的是MySQL8.0.12,8一下的mysql链接代码有微小差别,网上教程基本都是8一下的,随便找找就能找到;

2.DBBean.java

package Bean;
import java.sql.*;
public class DBBean {
	private String driverStr = "com.mysql.cj.jdbc.Driver";
	private String connStr = "jdbc:mysql://localhost:3306/users?serverTimezone=UTC";
	private String dbusername = "root";
	private String dbpassword = "lufengyu";
	public Connection conn = null;
	public Statement stmt = null;
	
	public DBBean()
	{
		try
		{
			Class.forName(driverStr);
			conn = DriverManager.getConnection(connStr,dbusername,dbpassword);
			stmt = conn.createStatement();
		}
		catch(Exception ex) {
			System.out.println(ex.getMessage());
			System.out.println("mysql链接失败");
		}
	}
	
}

dbusername和dbpassword个人修改过,默认的都是root,这部分就是链接数据库的功能;

3.login.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>登录页面</title>
</head>
<body>
	<div class="center" align="center">
		<h1 style="color:black">欢迎登录</h1>
		<form id="indexform" name="indexForm" action="check.jsp" method="post">
			<table border="0">
				<tr>
					<th>账号:</th>
					<td><input type="text" name="username"></td>
				</tr>
				<tr>
					<th>密码:</th>
					<td><input type="password" name="password"></td>
				</tr>
			</table>
			<br><input type="submit" value="登&nbsp;&nbsp;&nbsp;录">
		</form>
		<br><a  href="zhuce.jsp">没有账号?注册</a>
	</div>
</body>
</html>

4.check.jsp

<%@ page import="java.sql.*" language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="db" class="Bean.DBBean" scope="page"/>
<%
	request.setCharacterEncoding("utf-8");
	String username=(String)request.getParameter("username");
	String password=(String)request.getParameter("password");
	
	String sql="select * from sky where username="+"'"+username+"'";
	ResultSet rs = db.stmt.executeQuery(sql);
	if(rs.next()){
		if(password.equals(rs.getObject("password"))){
			response.sendRedirect("success.jsp");
		}
		else{
			out.print("<script language='javaScript'> alert('密码错误');</script>");
			response.setHeader("refresh", "0;url=login.jsp");
		}
	}
	else{
		out.print("<script language='javaScript'> alert('账号错误');</script>");
		response.setHeader("refresh", "0;url=login.jsp");
	}
%>
</body>
</html>

5.zhuce.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>注册</title>
</head>
<body>
	<div class="center" align="center">
		<h1>欢迎注册</h1>
		<form action="zhuceck.jsp" method="post">
			<table border="0">
				<tr>
					<th>账号:</th>
					<td><input type="text" name="username"></td>
				</tr>
				<tr>
					<th>密码:</th>
					<td><input type="password" name="password"></td>
				</tr>
			</table>
			<br><input type="submit" value="注&nbsp;&nbsp;&nbsp;册">
		</form>
	</div>
</body>
</html>

6.zhuceck.jsp

<%@ page language="java" import="java.util.*,java.sql.*" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="db" class="Bean.DBBean" scope="page"/>
<%
	request.setCharacterEncoding("utf-8");
	String username=(String)request.getParameter("username");
	String password=(String)request.getParameter("password");
	
	String sqlf="select * from sky where username ="+"'"+username+"'";
	String sqlc="insert into `sky`(`username`,`password`) values ('"+username+"','"+password+"')";
	ResultSet rs = db.stmt.executeQuery(sqlf);
	if(rs.next()){
		out.print("<script language='javascript'> alert('账号已经存在');</script>");
		response.setHeader("refresh", "0;url=zhuce.jsp");
	}
	else{
		db.stmt.executeUpdate(sqlc);
		response.sendRedirect("zhucesu.jsp");
	}
%>
</body>
</html>

7.success.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>登录成功</title>
</head>
<body style="text-align:center">
	<h1>登陆成功</h1>
</body>
</html>

8.zhucesu.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body style="text-align:center">
	<br><a href="login.jsp">注册成功,前往登录</a>
</body>
</html>

9.数据库创造

CREATE TABLE `sky`
(
    `id` int(5) NOT NULL AUTO_INCREMENT,
    `username` VARCHAR(45) DEFAULT NULL,
    `password` VARCHAR(45) DEFAULT NULL,
    UNIQUE KEY `username_UNIQUE` (`username`),
    PRIMARY KEY (`id`)
)ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
INSERT INTO `sky`(`id`,`username`,`password`)values
('1','admin','admin'),
('2','lfy','lufengyu');

结果(运行login.jsp)

菜鸡互啄!!

总结

加载全部内容

相关教程
猜你喜欢
用户评论