仓库

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>首页</title>
<style>
    .a{
        font-size: 26px;
        margin-top: 20px;
    }
</style>
</head>
<body>
    <div align="center">
        <h1 style="color: red;">库存物资管理系统</h1>
        <div class="a">
            <a href="add.jsp">物品信息录入</a>
        </div>
        <div class="a">
            <a href="HoldServlet?method=list">物品信息修改</a>
        </div>
        <div class="a">
            <a href="del.jsp">物品信息删除</a>
        </div>
        <div class="a">
            <a href="search.jsp">物品信息查询</a>
        </div>
    </div>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
    .a{
        margin-top: 20px;
    }
    .b{
        font-size: 20px;
         160px;
        color: white;
        background-color: greenyellow;
    }
</style>
</head>
<body>
    <%
         Object message = request.getAttribute("message");
         if(message!=null && !"".equals(message)){
     
    %>
         <script type="text/javascript">
              alert("<%=request.getAttribute("message")%>");
         </script>
    <%} %>
    <div align="center">
        <h1 style="color: red;">物品信息录入</h1>
        <a href="index.jsp">返回主页</a>
        <form action="HoldServlet?method=add" method="post" onsubmit="return check()">
            <div class="a">
                商品名称<input type="text" id="name" name="name"/>
            </div>
            <div class="a">
                公司<input type="text" id="company" name="company" />
            </div>
            <div class="a">
                型号<input type="text" id="type" name="type" />
            </div>
            <div class="a">
                规格<input type="text" id="spac" name="spac"/>
            </div>
            <div class="a">
                <button type="submit" class="b">保&nbsp;&nbsp;&nbsp;&nbsp;存</button>
            </div>
        </form>
    </div>
    <script type="text/javascript">
        function check() {
            var name = document.getElementById("name");;
            var company = document.getElementById("company");
            var type = document.getElementById("type");
            var spac = document.getElementById("spac");
            
            //非空
            if(name.value == '') {
                alert('商品名称为空');
                name.focus();
                return false;
            }
            if(company.value == '') {
                alert('公司为空');
                company.focus();
                return false;
            }
            if(type.value == '') {
                alert('型号为空');
                type.focus();
                return false;
            }
            if(spac.value == '') {
                alert('规格为空');
                spac.focus();
                return false;
            }
            

            

        }
    </script>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
    .a{
        margin-top: 20px;
    }
    .b{
        font-size: 20px;
         160px;
        color: white;
        background-color: greenyellow;
    }
    .tb, td {
        border: 1px solid black;
        font-size: 22px;
    }
</style>
</head>
<body>
    <div align="center">
        <h1 style="color: red;">课程信息列表</h1>
        <a href="index.jsp">返回主页</a>
        <table class="tb">
            <tr>
                <td>id</td>
                <td>课程名称</td>
                <td>任课教师</td>
                <td>上课地点</td>
            </tr>
            <!-- forEach遍历出adminBeans -->
            <c:forEach items="${courses}" var="item" varStatus="status">
                <tr>
                    <td>${item.id}</td>
                    <td><a>${item.name}</a></td>
                    <td>${item.teacher}</td>
                    <td>${item.classroom}</td>
                </tr>
            </c:forEach>
        </table>
    </div>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
    .a{
        margin-top: 20px;
    }
    .b{
        font-size: 20px;
         160px;
        color: white;
        background-color: greenyellow;
    }
    .tb, td {
        border: 1px solid black;
        font-size: 22px;
    }
</style>
</head>
<body>
    <div align="center">
        <h1 style="color: red;">物品信息删除</h1>
        <a href="index.jsp">返回主页</a>
        <table class="tb">
            <tr>
                <td>物品名称</td>
                <td>${hold.name}</td>
            </tr>
            <tr>
                <td>公司</td>
                <td>${hold.company}</td>
            </tr>
            <tr>
                <td>型号</td>
                <td>${hold.type}</td>
            </tr>
            <tr>
                <td>规格</td>
                <td>${hold.spac}</td>
            </tr>
        </table>
        <div class="a">
            <a onclick="return check()" href="HoldServlet?method=del&id=${hold.id}">删&nbsp;&nbsp;&nbsp;&nbsp;除</a>
        </div>
    </div>
    <script type="text/javascript">
        function check() {
            if (confirm("真的要删除吗?")){
                return true;
            }else{
                return false;
            }
        }
    </script>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
    .a{
        margin-top: 20px;
    }
    .b{
        font-size: 20px;
         160px;
        color: white;
        background-color: greenyellow;
    }
</style>
</head>
<body>
    <%
         Object message = request.getAttribute("message");
         if(message!=null && !"".equals(message)){
     
    %>
         <script type="text/javascript">
              alert("<%=request.getAttribute("message")%>");
         </script>
    <%} %>
    <div align="center">
        <h1 style="color: red;">物品信息修改</h1>
        <a href="index.jsp">返回主页</a>
        <form action="HoldServlet?method=update" method="post" onsubmit="return check()">
            <div class="a">
                物品名称<input type="text" id="name" name="name" value="${hold.name}"/>
            </div>
            <div class="a">
                公司<input type="text" id="company" name="company" value="${hold.company}"/>
            </div>
            <div class="a">
                型号<input type="text" id="type" name="type" value="${hold.type}"/>
            </div>
            <div class="a">
                规格<input type="text" id="spac" name="spac" value="${hold.spac}"/>
            </div>
            
            <input type="hidden" id="id" name="id" value="${hold.id}"/>
            <div class="a">
                <button type="submit" class="b">修&nbsp;&nbsp;&nbsp;&nbsp;改</button>
            </div>
        </form>
    </div>
    <script type="text/javascript">
        function check() {
            var name = document.getElementById("name");;
            var company = document.getElementById("comapny");
            var type = document.getElementById("type");
            var spac = document.getElementById("spac");
            
            //非空
            if(name.value == '') {
                alert('物品名称为空');
                name.focus();
                return false;
            }
            if(company.value == '') {
                alert('公司为空');
                company.focus();
                return false;
            }
            if(type.value == '') {
                alert('型号为空');
                type.focus();
                return false;
            }
            if(spac.value == '') {
                alert('规格为空');
                spac.focus();
                return false;
            }
            
        
        }
    </script>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
    .a{
        margin-top: 20px;
    }
    .b{
        font-size: 20px;
         160px;
        color: white;
        background-color: greenyellow;
    }
</style>
</head>
<body>
    <div align="center">
        <h1 style="color: red;">物品信息查询</h1>
        <a href="index.jsp">返回主页</a>
        <form action="HoldServlet?method=search" method="post" onsubmit="return check()">
            <div class="a">
                物品名称<input type="text" id="name" name="name"/>
            </div>
            <div class="a">
                公司<input type="text" id="company" name="company" />
            </div>
            <div class="a">
                型号<input type="text" id="type" name="type" />
            </div>
            <div class="a">
                规格<input type="text" id="spac" name="spac" />
            </div>
            <div class="a">
                <button type="submit" class="b">查&nbsp;&nbsp;&nbsp;&nbsp;询</button>
            </div>
        </form>
    </div>
    <script type="text/javascript">
        function check() {
            var name = document.getElementById("name");;
            var company = document.getElementById("company");
            var type = document.getElementById("type");
            var spac = document.getElementById("spac");
            
            //非空
            if(name.value == '' && company.value == '' && type.value == ''&& spac.value=='') {
                alert('请填写一个条件');
                return false;
            }
        }
    </script>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
    .a{
        margin-top: 20px;
    }
    .b{
        font-size: 20px;
         160px;
        color: white;
        background-color: greenyellow;
    }
    .tb, td {
        border: 1px solid black;
        font-size: 22px;
    }
</style>
</head>
<body>
    <div align="center">
        <h1 style="color: red;">物品信息列表</h1>
        <a href="index.jsp">返回主页</a>
        <table class="tb">
            <tr>
                <td>id</td>
                <td>物品名称</td>
                <td>公司</td>
                <td>型号</td>
                <td>规格</td>
            </tr>
            <!-- forEach遍历出adminBeans -->
            <c:forEach items="${holds}" var="item" varStatus="status">
                <tr>
                    <td>${item.id}</td>
                    <td><a>${item.name}</a></td>
                    <td>${item.company}</td>
                    <td>${item.type}</td>
                    <td>${item.spac}</td>
                </tr>
            </c:forEach>
        </table>
    </div>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
    .a{
        margin-top: 20px;
    }
    .b{
        font-size: 20px;
         160px;
        color: white;
        background-color: greenyellow;
    }
    .tb, td {
        border: 1px solid black;
        font-size: 22px;
    }
</style>
</head>
<body>
    <%
         Object message = request.getAttribute("message");
         if(message!=null && !"".equals(message)){
     
    %>
         <script type="text/javascript">
              alert("<%=request.getAttribute("message")%>");
         </script>
    <%} %>
    <div align="center">
        <h1 style="color: red;">物品信息列表</h1>
        <a href="index.jsp">返回主页</a>
        <table class="tb">
            <tr>
                <td>id</td>
                <td>物品名称</td>
                <td>公司</td>
                <td>型号</td>
                <td>规格</td>
                <td align="center" colspan="2">操作</td>
            </tr>
            <c:forEach items="${holds}" var="item">
                <tr>
                    <td>${item.id}</td>
                    <td>${item.name}</td>
                    <td>${item.company}</td>
                    <td>${item.type}</td>
                    <td>${item.spac}</td>
                    <td><a href="HoldServlet?method=getholdbyid&id=${item.id}">修改</a></td>
                </tr>
            </c:forEach>
        </table>
    </div>
</body>
</html>
package com.hold.servlet;

import java.io.IOException;
import java.util.List;

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 com.hold.entity.Hold;
import com.hold.service.HoldService;

@WebServlet("/HoldServlet")
public class HoldServlet extends HttpServlet {
    
    private static final long serialVersionUID = 1L;

    HoldService service = new HoldService();
    
    /**
     *
     */
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String method = req.getParameter("method");
        if ("add".equals(method)) {
            add(req, resp);
        } else if ("del".equals(method)) {
            del(req, resp);
        } else if ("update".equals(method)) {
            update(req, resp);
        } else if ("search".equals(method)) {
            search(req, resp);
        } else if ("getholdbyid".equals(method)) {
            getHoldById(req, resp);
        } else if ("getholdbyname".equals(method)) {
            getHoldByName(req, resp);
        } else if ("list".equals(method)) {
            list(req, resp);
        }
    }

    /**
     * 
     * @param req
     * @param resp
     * @throws IOException 
     * @throws ServletException 
     */
    private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        req.setCharacterEncoding("utf-8");
        String name = req.getParameter("name");
        String company = req.getParameter("company");
        String type = req.getParameter("type");
        String spac = req.getParameter("spac");
        Hold hold = new Hold(name, company, type,spac);
        
        
        if(service.add(hold)) {
            req.setAttribute("message", "录入成功");
            req.getRequestDispatcher("add.jsp").forward(req,resp);
        } else {
            req.setAttribute("message", "录入失败");
            req.getRequestDispatcher("add.jsp").forward(req,resp);
        }
    }
    
    /**
     * 
     * @param req
     * @param resp
     * @throws ServletException 
     */
    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
        req.setCharacterEncoding("utf-8");
        List<Hold> holds = service.list();
        req.setAttribute("holds", holds);
        req.getRequestDispatcher("list.jsp").forward(req,resp);
    }

    /**
     * Course
     * @param req
     * @param resp
     * @throws ServletException 
     */
    private void getHoldById(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
        req.setCharacterEncoding("utf-8");
        int id = Integer.parseInt(req.getParameter("id"));
        Hold hold = service.getHoldById(id);
        req.setAttribute("hold", hold);
        req.getRequestDispatcher("detail2.jsp").forward(req,resp);
    }

    /**
     * 
     * @param req
     * @param resp
     * @throws IOException
     * @throws ServletException 
     */
    private void getHoldByName(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
        req.setCharacterEncoding("utf-8");
        String name = req.getParameter("name");
        Hold hold = service.getHoldByName(name);
        if(hold == null) {
            req.setAttribute("message", "名字重复");
            req.getRequestDispatcher("del.jsp").forward(req,resp);
        } else {
            req.setAttribute("hold", hold);
            req.getRequestDispatcher("detail.jsp").forward(req,resp);
        }
    }
    
    /**
     *
     * @param req
     * @param resp
     * @throws IOException
     * @throws ServletException 
     */
    private void del(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
        req.setCharacterEncoding("utf-8");
        int id = Integer.parseInt(req.getParameter("id"));
        service.del(id);
        req.setAttribute("message", "删除成功");
        req.getRequestDispatcher("del.jsp").forward(req,resp);
    }
    
    /**
    
     * @param req
     * @param resp
     * @throws IOException
     * @throws ServletException 
     */
    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
        req.setCharacterEncoding("utf-8");
        int id = Integer.parseInt(req.getParameter("id"));
        String name = req.getParameter("name");
        String company = req.getParameter("company");
        String type = req.getParameter("type");
        String spac = req.getParameter("spac");
        Hold hold = new Hold(id, name, company, type,spac);
        
        service.update(hold);
        req.setAttribute("message", "修改成功");
        req.getRequestDispatcher("HoldServlet?method=list").forward(req,resp);
    }
    
    /**
     *
     * @param req
     * @param resp
     * @throws ServletException 
     */
    private void search(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
        req.setCharacterEncoding("utf-8");
        String name = req.getParameter("name");
        String company = req.getParameter("company");
        String type = req.getParameter("type");
        String spac = req.getParameter("spac");
        List<Hold> holds = service.search(name, company, type,spac);
        req.setAttribute("holds", holds);
        req.getRequestDispatcher("searchlist.jsp").forward(req,resp);
    }
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
    .a{
        margin-top: 20px;
    }
    .b{
        font-size: 20px;
         160px;
        color: white;
        background-color: greenyellow;
    }
    .tb, td {
        border: 1px solid black;
        font-size: 22px;
    }
</style>
</head>
<body>
    <div align="center">
        <h1 style="color: red;">课程信息列表</h1>
        <a href="index.jsp">返回主页</a>
        <table class="tb">
            <tr>
                <td>id</td>
                <td>课程名称</td>
                <td>任课教师</td>
                <td>上课地点</td>
            </tr>
            <!-- forEach遍历出adminBeans -->
            <c:forEach items="${courses}" var="item" varStatus="status">
                <tr>
                    <td>${item.id}</td>
                    <td><a>${item.name}</a></td>
                    <td>${item.teacher}</td>
                    <td>${item.classroom}</td>
                </tr>
            </c:forEach>
        </table>
    </div>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
    .a{
        margin-top: 20px;
    }
    .b{
        font-size: 20px;
         160px;
        color: white;
        background-color: greenyellow;
    }
</style>
</head>
<body>
    <div align="center">
        <h1 style="color: red;">课程信息查询</h1>
        <a href="index.jsp">返回主页</a>
        <form action="CourseServlet?method=search" method="post" onsubmit="return check()">
            <div class="a">
                课程名称<input type="text" id="name" name="name"/>
            </div>
            <div class="a">
                任课教师<input type="text" id="teacher" name="teacher" />
            </div>
            <div class="a">
                上课地点<input type="text" id="classroom" name="classroom" />
            </div>
            <div class="a">
                <button type="submit" class="b">查&nbsp;&nbsp;&nbsp;询</button>
            </div>
        </form>
    </div>
    <script type="text/javascript">
        function check() {
            var name = document.getElementById("name");;
            var teacher = document.getElementById("teacher");
            var classroom = document.getElementById("classroom");
            
            //非空
            if(name.value == '' && teacher.value == '' && classroom.value == '') {
                alert('请填写一个条件');
                return false;
            }
        }
    </script>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>首页</title>
<style>
    .a{
        font-size: 26px;
        margin-top: 20px;
    }
</style>
</head>
<body>
    <div align="center">
        <h1 style="color: red;">课程基本信息管理系统</h1>
        <div class="a">
            <a href="add.jsp">课程信息录入</a>
        </div>
        <div class="a">
            <a href="CourseServlet?method=list">课程信息修改</a>
        </div>
        <div class="a">
            <a href="del.jsp">课程信息删除</a>
        </div>
        <div class="a">
            <a href="search.jsp">课程信息查询</a>
        </div>
    </div>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
    .a{
        margin-top: 20px;
    }
    .b{
        font-size: 20px;
         160px;
        color: white;
        background-color: greenyellow;
    }
</style>
</head>
<body>
    <%
         Object message = request.getAttribute("message");
         if(message!=null && !"".equals(message)){
     
    %>
         <script type="text/javascript">
              alert("<%=request.getAttribute("message")%>");
         </script>
    <%} %>
    <div align="center">
        <h1 style="color: red;">课程信息修改</h1>
        <a href="index.jsp">返回主页</a>
        <form action="CourseServlet?method=update" method="post" onsubmit="return check()">
            <div class="a">
                课程名称<input type="text" id="name" name="name" value="${course.name}"/>
            </div>
            <div class="a">
                任课教师<input type="text" id="teacher" name="teacher" value="${course.teacher}"/>
            </div>
            <div class="a">
                上课地点<input type="text" id="classroom" name="classroom" value="${course.classroom}"/>
            </div>
            <input type="hidden" id="id" name="id" value="${course.id}"/>
            <div class="a">
                <button type="submit" class="b">修&nbsp;&nbsp;&nbsp;改</button>
            </div>
        </form>
    </div>
    <script type="text/javascript">
        function check() {
            var name = document.getElementById("name");;
            var teacher = document.getElementById("teacher");
            var classroom = document.getElementById("classroom");
            
            //非空
            if(name.value == '') {
                alert('课程名称为空');
                name.focus();
                return false;
            }
            if(teacher.value == '') {
                alert('教师为空');
                teacher.focus();
                return false;
            }
            if(classroom.value == '') {
                alert('上课地点为空');
                classroom.focus();
                return false;
            }
            
            //教师
            if(teacher.value != '王建民' && teacher.value != '王辉' && teacher.value != '刘丹' && teacher.value != '刘立嘉' && teacher.value != '杨子光'){
                alert('教师名称错误');
                return false;
            }
            
            //教室
            if(!/^基教/.test(classroom.value) && !/^一教/.test(classroom.value) && !/^二教/.test(classroom.value) && !/^三教/.test(classroom.value)) {
                alert('上课地点错误');
                return false;
            }
        }
    </script>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
    .a{
        margin-top: 20px;
    }
    .b{
        font-size: 20px;
         160px;
        color: white;
        background-color: greenyellow;
    }
    .tb, td {
        border: 1px solid black;
        font-size: 22px;
    }
</style>
</head>
<body>
    <div align="center">
        <h1 style="color: red;">课程信息删除</h1>
        <a href="index.jsp">返回主页</a>
        <table class="tb">
            <tr>
                <td>课程名称</td>
                <td>${course.name}</td>
            </tr>
            <tr>
                <td>任课教师</td>
                <td>${course.teacher}</td>
            </tr>
            <tr>
                <td>上课地点</td>
                <td>${course.classroom}</td>
            </tr>
        </table>
        <div class="a">
            <a onclick="return check()" href="CourseServlet?method=del&id=${course.id}">删&nbsp;&nbsp;&nbsp;除</a>
        </div>
    </div>
    <script type="text/javascript">
        function check() {
            if (confirm("真的要删除吗?")){
                return true;
            }else{
                return false;
            }
        }
    </script>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
    .a{
        margin-top: 20px;
    }
    .b{
        font-size: 20px;
         160px;
        color: white;
        background-color: greenyellow;
    }
</style>
</head>
<body>
    <%
         Object message = request.getAttribute("message");
         if(message!=null && !"".equals(message)){
     
    %>
         <script type="text/javascript">
              alert("<%=request.getAttribute("message")%>");
         </script>
    <%} %>
    <div align="center">
        <h1 style="color: red;">课程信息删除</h1>
        <a href="index.jsp">返回主页</a>
        <form action="CourseServlet?method=getcoursebyname" method="post" onsubmit="return check()">
            <div class="a">
                课程名称<input type="text" id="name" name="name"/>
            </div>
            <div class="a">
                <button type="submit" class="b">查&nbsp;&nbsp;&nbsp;找</button>
            </div>
        </form>
    </div>
    <script type="text/javascript">
        function check() {
            var name = document.getElementById("name");;
            
            //非空
            if(name.value == '') {
                alert('课程名称为空');
                name.focus();
                return false;
            }
        }
    </script>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
    .a{
        margin-top: 20px;
    }
    .b{
        font-size: 20px;
         160px;
        color: white;
        background-color: greenyellow;
    }
</style>
</head>
<body>
    <%
         Object message = request.getAttribute("message");
         if(message!=null && !"".equals(message)){
     
    %>
         <script type="text/javascript">
              alert("<%=request.getAttribute("message")%>");
         </script>
    <%} %>
    <div align="center">
        <h1 style="color: red;">课程信息录入</h1>
        <a href="index.jsp">返回主页</a>
        <form action="CourseServlet?method=add" method="post" onsubmit="return check()">
            <div class="a">
                课程名称<input type="text" id="name" name="name"/>
            </div>
            <div class="a">
                任课教师<input type="text" id="teacher" name="teacher" />
            </div>
            <div class="a">
                上课地点<input type="text" id="classroom" name="classroom" />
            </div>
            <div class="a">
                <button type="submit" class="b">保&nbsp;&nbsp;&nbsp;存</button>
            </div>
        </form>
    </div>
    <script type="text/javascript">
        function check() {
            var name = document.getElementById("name");;
            var teacher = document.getElementById("teacher");
            var classroom = document.getElementById("classroom");
            
            //非空
            if(name.value == '') {
                alert('课程名称为空');
                name.focus();
                return false;
            }
            if(teacher.value == '') {
                alert('教师为空');
                teacher.focus();
                return false;
            }
            if(classroom.value == '') {
                alert('上课地点为空');
                classroom.focus();
                return false;
            }
            
            //教师
            if(teacher.value != '王建民' && teacher.value != '王辉' && teacher.value != '刘丹' && teacher.value != '刘立嘉' && teacher.value != '杨子光'){
                alert('教师名称错误');
                return false;
            }
            
            //教室
            if(!/^基教/.test(classroom.value) && !/^一教/.test(classroom.value) && !/^二教/.test(classroom.value) && !/^三教/.test(classroom.value)) {
                alert('上课地点错误');
                return false;
            }
        }
    </script>
</body>
</html>
原文地址:https://www.cnblogs.com/sljslj/p/10146662.html