JSP的分层开发

jsp的分层开发
工具包的构造

package com.openlab.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/hospital?useUnicode=true&characterEncoding=UTF-8";
	static String username="root";
	static String password="root";*/
	
	
	static String driver=ConfigManager.getProperty("driver");
	static String url=ConfigManager.getProperty("url");
	static String username=ConfigManager.getProperty("username1");
	static String password=ConfigManager.getProperty("password1");
	public static Connection getConnection(){
		Connection conn=null;
		try{
			Class.forName(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;
	}
}
package com.openlab.dao.impl;

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

import com.openlab.Utils.DBConn;
import com.openlab.dao.Login;

public class LoginImpl  implements Login{
	//验证登录是否成功
		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();
				
				DBConn db=new DBConn();
				conn=db.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);
				DBConn.closeAll(rs, pst, null);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
			return result;
		}
}

package com.openlab.dao.impl;

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

import com.openlab.Utils.DbConnUtils;
import com.openlab.dao.Register;
import com.openlab.pojo.Employee;

public class RegisterImpl  implements Register{
	public String doRegister(Employee _employee){
		Connection conn=null;
		PreparedStatement pst=null;
		
		String result="";
		int count=0;
		String sql="insert into loginuser(userName,userPwd,userRePwd,sex,princal,email) values(?,?,?,?,?,?)";
		try {
			conn=DbConnUtils.getConnection();
			 pst=conn.prepareStatement(sql);
			 pst.setString(1, _employee.getRegisterName());
			 pst.setString(2, _employee.getRepassword());
			 pst.setString(3, _employee.getRerepassword());
			 pst.setInt(4, _employee.getSex());
			 pst.setString(5, _employee.getPrical());
			 pst.setString(6, _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.openlab.pojo;

public class Employee {
	private String RegisterName;
	private String repassword;
	private String rerepassword;
	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 String getRerepassword() {
		return rerepassword;
	}
	public void setRerepassword(String rerepassword) {
		this.rerepassword = rerepassword;
	}
	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;
	}
}

登录界面的编写

<%@ 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>

<%
/* Cookie cookies[] = request.getCookies();
		boolean flag = false;
		if (cookies != null) {
			for (int i = 0; i < cookies.length; i++) {
				if (cookies[i].getName().equals("admin")) {
					flag = true;
					response.sendRedirect("index1.jsp");
					break;
				}
			}
		}
 */
		
 %>
	<h2>登录页面</h2>
	<div id="regiter">
	<a href="regiter.jsp">注册</a>
	</div>
	
	
	<div>
		<form action="login" method="post">
			<table>
				<tr>
					<td>用户名:<input type="text" name="username" /></td>
				</tr>

				<tr>
					<td>密码:<input type="password" name="password" /></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="submit" value="登录" /> <input type="reset"
						value="取消" />
					</td>
				</tr>
			</table>
		</form>
	</div>
</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>欢迎来西安市交通大学第一附属医院</h2>
	<div>
		<form action="register" 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="password" name="reregisterPassword" /></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>

完成代码的分层处理
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
如有错误,请大家指正。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值