今天开始分为各个模块来进行洗衣店管理信息系统的开发,今天介绍登陆模块,具体开发的流程如下,登陆页面的开发-------------登录功能的开发---------------数据库表的开发。
1 开发之前的准备工作
在一个项目的开发之前,需要搭建环境,包括建包,导包,等等。导包包括数据库的包,dbutils包等等,建包的结构包括web包,service包,dao包,utils包,domain包,如下图所示:
2 准备登陆页面
一般在Hbuilder 或者 webstorm中开发完毕后再导入eclipseide中
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>管理员登录</title>
<meta charset="utf-8">
<link href="css/stylelogin.css" rel='stylesheet' type='text/css' />
<meta name="viewport" content="width=device-width, initial-scale=1">
<script type="application/x-javascript"> addEventListener("load", function() { setTimeout(hideURLbar, 0); },false); function hideURLbar(){ window.scrollTo(0,1); } </script>
</head>
<body>
<!-----start-main---->
<div class="main">
<div class="login-form">
<h1>Admin Login</h1>
<div class="head">
<img src="images/user.png" alt=""/>
</div>
<form action="${pageContext.request.contextPath }/adminLogin" method="post">
<input type="text" class="text" name="mname" value="请输入管理员账号" οnfοcus="this.value = '';" οnblur="if (this.value == '') {this.value = 'USERNAME';}" >
<input type="password" name="mpassword" value="Password" οnfοcus="this.value = '';" οnblur="if (this.value == '') {this.value = 'Password';}">
<div class="submit">
<input type="submit" value="LOGIN" >
</div>
</form>
</div>
<!--//End-login-form-->
</div>
<!-----//end-main---->
</body>
</html>
效果如下
3 功能模块开发
这里着重分析一下登录功能的实现,当项目运行起来后,当我们在页面上输入登录名和密码之后,页面上的数据会因为form表单的缘故被带到web层,web层对输入的登录名和密码进行接收,并将数据传递到service层,service层进行异常处理,并传递至dao层,dao层接收到数据后查询数据库中的表并进行校验,校验后按照原路返回,并告诉客户是否登陆成功。
(1)web层
package com.itheima.web.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.itheima.domain.Manager;
import com.itheima.service.AdminService;
public class AdminLoginServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
HttpSession session = request.getSession();
//1.获取数据
String mname = request.getParameter("mname");//中文 张三
String mpassword = request.getParameter("mpassword");
//2.传递给service层
AdminService service = new AdminService();
Manager manager = null;
manager = service.login(mname,mpassword);
if(manager!=null){
session.setAttribute("manager", manager);
response.sendRedirect(request.getContextPath()+"/admin/home.jsp");
}else{
//失败 转发到登录页面 提出提示信息
response.sendRedirect(request.getContextPath()+"/admin/index.jsp");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
(2)service层
package com.itheima.service;
import java.sql.SQLException;
import com.itheima.dao.AdminDao;
import com.itheima.dao.UserDao;
import com.itheima.domain.Manager;
import com.itheima.domain.User;
public class AdminService {
public Manager login(String mname, String mpassword) {
AdminDao dao = new AdminDao();
Manager manager = null;
try {
manager = dao.login(mname,mpassword);
} catch (SQLException e) {
e.printStackTrace();
}
return manager;
}
}
(3)dao层
package com.itheima.dao;
import java.sql.SQLException;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import com.itheima.domain.Manager;
import com.itheima.domain.User;
import com.itheima.utils.DataSourceUtils;
public class AdminDao {
public Manager login(String mname, String mpassword) throws SQLException {
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select * from manager where mname=? and mpassword=?";
return runner.query(sql, new BeanHandler<Manager>(Manager.class), mname,mpassword);
}
}
4 数据库表的开发
关于表的设计,其实每个人都有每个人自己对表的设计,必要的字段有登录名和登陆密码,其他仁者见仁,智者见智。