java-Web(项目)-登录与注册

使用分层开发、并对数据访问配置进行优化改写注册、登录:
实现代码:
登录:

package com.homework.login;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.homework4.utils.DbConnUtils;

public class DoLogin {
	
	//验证登录是否成功
	public String doLogin(String _username,String _password){
		Connection conn=null;
		PreparedStatement pst=null;
		ResultSet rs=null;
		String result="";
		String sql="select *  from loginuser where userName=? and userPwd=?";
		try {
			conn=DbConnUtils.getConnection();
			 pst=conn.prepareStatement(sql);
			 pst.setString(1, _username);
			 pst.setString(2, _password);
			rs= pst.executeQuery();
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		try {
			if(rs.next()){
				result= "success";
			}else{
				result= "fail";
			}
			
			DbConnUtils.closeAll(rs, pst, conn);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return result;
	}
}

实体类:

package com.homework2.pojo;

public class Employee {
	private String RegisterName;
	private String repassword;
	private int sex;
	private String prical;
	private String email;
	
	public String getRegisterName() {
		return RegisterName;
	}
	public void setRegisterName(String registerName) {
		RegisterName = registerName;
	}
	public String getRepassword() {
		return repassword;
	}
	public void setRepassword(String repassword) {
		this.repassword = repassword;
	}
	public int getSex() {
		return sex;
	}
	public void setSex(int sex) {
		this.sex = sex;
	}
	public String getPrical() {
		return prical;
	}
	public void setPrical(String prical) {
		this.prical = prical;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	
	
}

注册方法:

package com.homework3.register;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.homework4.utils.DbConnUtils;
import com.homework2.pojo.Employee;

public class DoRegister {
	public String doRegister(Employee _employee){
		Connection conn=null;
		PreparedStatement pst=null;
		
		String result="";
		int count=0;
		String sql="insert into loginuser(userName,userPwd,sex,princal,email) values(?,?,?,?,?,?)";
		try {
			conn=DbConnUtils.getConnection();
			 pst=conn.prepareStatement(sql);
			 pst.setString(1, _employee.getRegisterName());
			 pst.setString(2, _employee.getRepassword());
			 pst.setInt(3, _employee.getSex());
			 pst.setString(4, _employee.getPrical());
			 pst.setString(5, _employee.getEmail());
		
			count=pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	
			if(count>0){
				result= "success";
			}else{
				result= "fail";
			}
			
			DbConnUtils.closeAll(null,pst, conn);
		
		
		return result;
	}
}

创建工具连接:

package com.homework4.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class DbConnUtils {

	static String url="jdbc:mysql://127.0.0.1:3306/hospital1?useUnicode=true&characterEncoding=UTF-8";
	static String username="root";
	static String password="root";
	
	public static Connection getConnection(){
		Connection conn=null;
		try{
			Class.forName("com.mysql.jdbc.Driver");
			//创建数据库连接
			 conn=DriverManager.getConnection(url, username, password);
				
		}catch(Exception e){
			e.printStackTrace();
		}
		return conn;
	}
	
	
	public static void closeAll(ResultSet rs,Statement st,Connection conn){
		
		//关闭结果集
		if(rs!=null){
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		if(st!=null){
			try {
				st.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		if(conn!=null){
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static ResultSet getQueryList(Connection _conn,Statement _st,String _sql){
		ResultSet rs=null;
		try {
			 rs=_st.executeQuery(_sql);
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rs;
	}
}

登录注册的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 'login.jsp' starting page</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="css/common.css">
<link rel="stylesheet" type="text/css" href="css/login.css">


</head>

<body>
	<div id="regiter">
	</div>
	<div>
		<form action="doLogin.jsp" method="post">
			<table>
				<tr>
					<td><span style="font-size: large; font-weight: 600;">&nbsp;&nbsp;&nbsp;登录系统</span></td>
				</tr>
				
				<tr>
					<td><input type="text" name="username" value="账号" size="30" maxlength="15" style="border-color: cornflowerblue;" id="d1" onblur="checkUsername()" /><div id="d1Div"></div></td>
				</tr>
				
				<tr>
					<td><input type="password" name="pwd" size="30" maxlength="20" style="border-color: cornflowerblue;" id="d2"onblur="checkpassword()" /><div id="d2Div"></div></td>
				</tr>
				<tr>
					<td><span style="color:red;">
						<%-- 	<%
								if (session.getAttribute("errors") != null) {
									out.print(session.getAttribute("errors"));
								}
							%> --%>
							<%
							if (session.getAttribute("errors") != null) {
							%>
							
							<%=session.getAttribute("errors")%>
							
							 <%
								}
							 %>
					</span>
					</td>
				</tr>
				<tr>
					<td><input type="text" name="code" value="验证码" size="15" maxlength="4" style="border-color: cornflowerblue;" id="d3" onblur="checkcode()" /><span style="font-size: x-small;">7236//</span><div id="d3Div"></div></td>
				</tr>
				<tr>
					<td><input type="submit" value="登录" /> 
					<a href="regiter.jsp">注册</a>
					<input type="reset" value="取消" />
					</td>
				</tr>
			</table>
		</form>
	</div>
</body>
</html>
<%@ page language="java" import="java.util.*,com.openlab.login.*"
	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 'doLogin.jsp' starting page</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>

	<%
		//处理post请求乱码
		request.setCharacterEncoding("UTF-8");

		String username = request.getParameter("username");
		String password = request.getParameter("password");
		
		DoLogin doLogin = new DoLogin();
		String result = doLogin.doLogin(username, password);
		if (result.equals("success")) {
			//登陆成功
			request.setAttribute("login", username); 
			RequestDispatcher rd=request.getRequestDispatcher("index.jsp");
			rd.forward(request, response);
		} else {
			session.setAttribute("errors", "用户名密码不正确,请重新输入...");
			//登陆失败
			response.sendRedirect("login.jsp");
		}
	%>



</body>
</html>
<%@ 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 'regiter.jsp' starting page</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="css/common.css">

</head>

<body>
	<h2>欢迎来西安xx医院</h2>
	<div>
		<form action="doRegister.jsp" method="post">
			<table>
				<tr>
					<td>用户名:</td>
					<td><input type="text" name="registerName" /></td>
				</tr>

				<tr>
					<td>密码:</td>
					<td><input type="password" name="registerPassword" /></td>
				</tr>
				<tr>
					<td>性别:</td>
					<td><input type="radio" name="sex" value="0" checked="checked" /><input type="radio" name="sex" value="1" /></td>
				</tr>

				<tr>
					<td>身份:</td>
					<td><select name="princal">
							<option>--请选择--</option>
							<option value="doctor">院长</option>
							<option value="nurse">副院长</option>
							<option value="yuanzhang">专家</option>
							<option value="admin">医生</option>
					</select></td>
				</tr>

				<tr>
					<td>email:</td>
					<td><input type="text" name="email" /></td>
				</tr>

				<tr>
					<td>
						<%
							if (session.getAttribute("errorsRegister") != null) {
								out.print(session.getAttribute("errorsRegister"));
							}
						%>
					</td>
				</tr>
				<tr>

					<td><input type="submit" value="注册" />
					</td>

					<td><input type="reset" value="取消" />
					</td>
				</tr>
			</table>
		</form>
	</div>
</body>
</html>
<%@page import="com.openlab.pojo.Employee,com.openlab.register.*"%>
<%@ 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 'doRegister.jsp' starting page</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="css/common.css">

</head>

<body>
	<h2>注册页面</h2>
	<%
		request.setCharacterEncoding("UTF-8");
		String registerName = request.getParameter("registerName");
		String registerPassword = request.getParameter("registerPassword");
		String sex = request.getParameter("sex");
		String princal = request.getParameter("princal");
		String email = request.getParameter("email");

		Employee _employee = new Employee();
		_employee.setRegisterName(registerName);
		_employee.setRepassword(registerPassword);
		_employee.setSex(Integer.parseInt(sex));
		_employee.setPrical(princal);
		_employee.setEmail(email);

		DoRegister doregister = new DoRegister();
		String result = doregister.doRegister(_employee);

		if (result.equals("success")) {
			response.sendRedirect("login.jsp");
			
		} else {
			session.setAttribute("errorsRegister", "注册失败...");

			response.sendRedirect("register.jsp");
		}
	%>



</body>
</html>

实现效果:
在这里插入图片描述
在这里插入图片描述

以上均为个人所写,有些许瑕疵,会抓紧时间改进,如有错误,请批评指出,也欢迎大家来探讨,谢谢!

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值