Design&Bulid 2 总结(上)
作者:互联网
1.前言
又到了一年一度的Design&Bulid,这次的项目是和其他专业的同学负责制作一个仓库管理系统。
项目分为两个阶段:第一阶段是与本专业的同学合作,第二阶段才是和其他专业合作。
2.要求
数据库管理和管理员端软件(电子商务专业学生)
与物联网学生共同设计数据库;并创建具有以下功能的web应用程序:
1、允许管理员登录和注销;
2、允许管理员在系统中创建仓库和添加/删除工人
3、显示每个仓库的货物状态(如货物位置)和信息(如货物种类);
额外功能:
1、生成数据分析报告和可视化。
2、自行设计的其他功能。
3.具体功能的实现
3.1 登录
老师手把手教的内容,没什么难度。
package com.example._2eg.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.example._2eg.jspservlet.dao.UserDAO;
import com.example._2eg.jspservlet.dao.impl.UserDAOImpl;
import com.example._2eg.vo.User;
@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet{
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException{
this.doPost(req,res);
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException{
User user = new User();
user.setUsername(req.getParameter("username"));
user.setPassword(req.getParameter("password"));
UserDAO dao = new UserDAOImpl();
try {
user = dao.queryByUsername(user);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(user != null){
HttpSession session=req.getSession();
session.setAttribute("username", user.getUsername());
try {
dao.updateStatus(user);
} catch (Exception e) {
e.printStackTrace();
}
req.getRequestDispatcher("./welcome.jsp").forward(req,res);
} else {
res.sendRedirect("./error.jsp");
}
}
}
queryByUsername:直接与DAO层交互,根据用户名在数据库查找用户,返回一个User对象。
updateStatus:直接与DAO层交互,修改数据库中用户的登录状态,主要是做一个登录验证。
这个方法本来是给注销准备的,但是验收的时候老师提醒了一下,只修改数据库层面的状态是不够的
3.2 注销
这里认为只要在数据库里修改状态就OK,但是注销之后发现按浏览器的回退键就可以回到之前的页面。
所以这功能有啥用啊
package com.example._2eg.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.example._2eg.jspservlet.dao.UserDAO;
import com.example._2eg.jspservlet.dao.impl.UserDAOImpl;
import com.example._2eg.vo.User;
@WebServlet("/logoutServlet")
public class LogoutServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession(true);
User user = new User();
user.setUsername((String) session.getAttribute("username"));
user.setStatus(1);
UserDAO dao = new UserDAOImpl();
try {
dao.updateStatus(user);
} catch (Exception e) {
e.printStackTrace();
}
session.invalidate();
resp.sendRedirect("./login.jsp");
}
}
3.3 创建仓库
这一部分主要是另一个同学负责的。
package com.example._2eg.servlet;
import com.example._2eg.service.WarehouseService;
import com.example._2eg.service.impl.WarehouseServiceImpl;
import com.example._2eg.vo.Warehouse;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
@WebServlet(name = "WarehouseAddServlet", value = "/warehouseAddServlet")
public class WarehouseAddServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Warehouse warehouse = new Warehouse();
warehouse.setWid(Integer.parseInt(request.getParameter("Id")));
warehouse.setWname(request.getParameter("Name"));
WarehouseService service = new WarehouseServiceImpl();
try {
if(service.add(warehouse)){
response.sendRedirect(request.getContextPath()+"/warehouseListServlet");
}else {
request.setAttribute("isExist","True");
request.getRequestDispatcher("/addWarehouse.jsp").forward(request,response);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
这里用isExist判断是否有id重复的仓库,如果isExist=True,表示ID重复,在页面显示"This employee id already exists"。
<% String isExistStr = "";%>
<%if(request.getAttribute("isExist")!=null){ %>
<%if(request.getAttribute("isExist").equals("True")){%>
<%isExistStr = "This employee id already exists";%>
<%}%>
<%}%>
<h5 style="text-align: center;"><span style="color: red; "><%= isExistStr %></span></h5>
3.4 展示仓库
这里也主要是另一个同学负责的。
package com.example._2eg.servlet;
import com.example._2eg.service.WarehouseService;
import com.example._2eg.service.impl.WarehouseServiceImpl;
import com.example._2eg.vo.Warehouse;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
@WebServlet(name = "WarehouseListServlet", value = "/warehouseListServlet")
public class WarehouseListServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//查询
WarehouseService wservice = new WarehouseServiceImpl();
List warehouses = null;
try {
warehouses = wservice.findAll();
} catch (SQLException e) {
e.printStackTrace();
}
//将查到的存入request域
request.setAttribute("warehouses", warehouses);
//转发
request.getRequestDispatcher("/list.jsp").forward(request, response);
}
}
findAll:查找数据库中所有仓库,将其存储到warehouses,之后遍历。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<!-- 网页使用的语言 -->
<html lang="en-US">
<head>
<!-- 指定字符集 -->
<meta charset="utf-8">
<!-- 使用Edge最新的浏览器的渲染方式 -->
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<!-- viewport视口:网页可以根据设置的宽度自动进行适配,在浏览器的内部虚拟一个容器,容器的宽度与设备的宽度相同。
width: 默认宽度与设备的宽度相同
initial-scale: 初始的缩放比,为1:1 -->
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
<title>Warehouse Management</title>
<!-- 1. 导入CSS的全局样式 -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- 2. jQuery导入,建议使用1.9以上的版本 -->
<script src="js/jquery-2.1.0.min.js"></script>
<!-- 3. 导入bootstrap的js文件 -->
<script src="js/bootstrap.min.js"></script>
<style type="text/css">
td, th {
text-align: center;
}
</style>
<script>
function deleteWarehouse(id){
//安全提示
if(confirm("Are you sure to delete?")){
//访问路径
location.href="${pageContext.request.contextPath}/warehouseDeleteServlet?uid="+id;
}
}
window.onload = function(){
//给删除选中按钮添加单击事件
document.getElementById("delSelected").onclick = function(){
if(confirm("Are you sure to delete?")){
var flag = false;
//判断是否有选中条目
var cbs = document.getElementsByName("uid");
for (var i = 0; i < cbs.length; i++) {
if(cbs[i].checked){
//有一个条目选中了
flag = true;
break;
}
}
if(flag){//有条目被选中
//表单提交
document.getElementById("form").submit();
}
}
}
//1.获取第一个cb
document.getElementById("firstCb").onclick = function(){
//2.获取下边列表中所有的cb
var cbs = document.getElementsByName("uid");
//3.遍历
for (var i = 0; i < cbs.length; i++) {
//4.设置这些cbs[i]的checked状态 = firstCb.checked
cbs[i].checked = this.checked;
}
}
}
</script>
</head>
<body>
<div class="container">
<h3 style="text-align: center">Warehouse Information</h3>
<div style="float: right;margin: 5px;">
<a class="btn btn-primary" href="${pageContext.request.contextPath}/addWarehouse.jsp">Add</a>
<a class="btn btn-primary" href="javascript:void(0);" id="delSelected">Delete</a>
</div>
<form id="form" action="${pageContext.request.contextPath}/warehouseDeleteServlet" method="post">
<table border="1" class="table table-bordered table-hover">
<tr class="success">
<th><input type="checkbox" id="firstCb"></th>
<th>Id</th>
<th>Name</th>
</tr>
<c:forEach items="${warehouses}" var="warehouse" varStatus="s">
<tr>
<td><input type="checkbox" name="uid" value="${warehouse.warehouse_id}"></td>
<td>${warehouse.warehouse_id}</td>
<td>${warehouse.warehouse_name}</td>
<td><a class="btn btn-default btn-sm" href="${pageContext.request.contextPath}/findWarehouseServlet?id=${warehouse.warehouse_id}">Edit</a>
<a class="btn btn-default btn-sm" href="javascript:deleteWarehouse(${warehouse.warehouse_id});">Delete</a>
<a class="btn btn-default btn-sm" href="${pageContext.request.contextPath}/productListServlet?id=${warehouse.warehouse_id}">Check</a>
</td>
</tr>
</c:forEach>
</table>
<input class="btn btn-default" type="button" value="back" onclick="javascript:history.go(-1)">
</form>
</div>
</body>
</html>
3.5 删除仓库
删除数据库中的对应仓库。
package com.example._2eg.servlet;
import com.example._2eg.service.WarehouseService;
import com.example._2eg.service.impl.WarehouseServiceImpl;
import com.example._2eg.vo.Warehouse;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/warehouseDeleteServlet")
public class WarehouseDeleteServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1. 获取id
String[] ids = req.getParameterValues("uid");
//2. 调用service删除
WarehouseService service =new WarehouseServiceImpl();
try {
service.deleteById(ids);
} catch (Exception e) {
e.printStackTrace();
}
//3. 跳转到查询所有用户记录的Servlet
resp.sendRedirect(req.getContextPath()+"/warehouseListServlet");
}
}
标签:总结,Bulid,com,2eg,Design,._,import,servlet,javax 来源: https://www.cnblogs.com/LindholmX/p/16512988.html