其他分享
首页 > 其他分享> > 小型人口普查系统1

小型人口普查系统1

作者:互联网

项目需求

1.通过Java Web写出主页面、信息登记页面、信息修改页面、信息删除页面和信息查询页面等

2.通过连接数据库,实现项目的增删改查功能

 

代码分析

1.主页面

要求显示人口登记、修改人口信息、查询人口信息、人口信息浏览五个子菜单

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>首页</title>
    <style type="text/css">
      a{
        text-decoration: none;
        display: block;
        text-align: center;
        color: #ff8dcf;
      }
    </style>
  </head>
  <body>
    <h1 align="center">人口普查系统</h1>
    <a href="register.jsp" text->人口登记</a> <br>
    <a href="change.jsp">修改人口信息</a><br>
    <a href="delete.jsp">删除人口信息</a><br>
    <a href="queryindex.jsp">查询人口信息</a><br>
    <a href="browser.jsp">人口信息浏览</a><br>
  </body>
</html>

 

 

 

 

2.人口登记页面

①完成添加人口信息登记,基本信息和填报限制如下表所示

户 别

家庭户 集体户(单选框)

住房类型

家庭住宅、集体住所、工作地住所、其他住宅、无住宅(单选框)

本户现住房面积

XXXXX平方米(文本框,只能输入这个整数)

本户现住房间数

XXXXX间(文本框,只能输入这个整数)

户主姓名

XXXXX(文本框)

身份证号码

XXXXXXXXXXXXXXXXXX(判断身份证位数13位并且只能录入数字、最后一位可以录入X)

性别

男/女(单选框)

民族

(文本框)

受教育程度

研究生、大学本科、大学专科、高中、初中、小学、未上过学(下拉框实现)

 

②点击“提交”按钮,保存成功则跳转到人口基本信息浏览界面,新录入的信息置顶显示。失败则提示错误信息,返回当前页面

 

register.jsp

<%--
  Created by IntelliJ IDEA.
  User: lenovo
  Date: 2020/11/10
  Time: 14:02
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>人口登记页面</title>
    <script type="text/javascript">
         function btnFun() {
             var patt1 = /^\d+\.\d+$/;
             var patt2 = /^\d{17}(\d|X|x)$/;
             var areaObj = document.getElementById("area");
             var roomNumberObj = document.getElementById("roomnumber");
             var id = document.getElementById("id");
             var areaText = areaObj.value;
             var roomNumberText = roomNumberObj.value;
             var idText = id.value;
             if (patt1.test(areaText) || patt1.test(roomNumberText)) {
                 alert("整数输入有误");
                 return false;
             } else if (patt2.test(idText)==false){
                 alert("身份证号输入错误")
                 return false;
             } else {
                 return true;
             }
        }
    </script>
</head>
<body>
    <form action="registerServlet" method="post">
        <table align="center">
            <tr>
                <td>户别:</td>
                <td>
                    <input type="radio" name="hubie" value="家庭户">家庭户
                    <input type="radio" name="hubie" value="集体户">集体户
                </td>
            </tr>
            <tr>
                <td>住房类型:</td>
                <td>
                    <input type="radio" name="type" value="家庭住宅">家庭住宅
                    <input type="radio" name="type" value="集体住宅">集体住宅
                    <input type="radio" name="type" value="工作地住所">工作地住所
                    <input type="radio" name="type" value="其他住宅">其他住宅
                    <input type="radio" name="type" value="无住宅">无住宅
                </td>
            </tr>
            <tr>
                <td>本户现住房面积:</td>
                <td><input type="text" name="area" id="area">平方米</td>
            </tr>
            <tr>
                <td>本户现住房间数</td>
                <td><input type="text" name="roomnumber" id="roomnumber"></td>
            </tr>

            <tr>
                <td>户主姓名:</td>
                <td><input type="text" name="name"> </td>
            </tr>

            <tr>
                <td>身份证号码:</td>
                <td><input type="text" name="id" id="id"></td>
            </tr>

            <tr>
                <td>性别:</td>
                <td>
                    <input type="radio" name="sex" value="男">男
                    <input type="radio" name="sex" value="女">女
                </td>
            </tr>

            <tr>
                <td>民族:</td>
                <td><input type="text" name="nation"> </td>
            </tr>

            <tr>
                <td>受教育程度:</td>
                <td>
                    <select name="education">
                        <option value="研究生">研究生</option>
                        <option value="大学本科">大学本科</option>
                        <option value="大学专科">大学专科</option>
                        <option value="高中">高中</option>
                        <option value="初中">初中</option>
                        <option value="小学">小学</option>
                        <option value="未上学">未上学</option>
                    </select>
                </td>
            </tr>

            <tr>
                <td colspan="2" align="center">
                    <input type="submit" value="提交" onclick="return btnFun()">
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

 

3.修改人口信息页面

输入户主姓名,显示其余信息,可对身份证号码、性别、民族、受教育程度进行修改;如果该名户主信息数据库不存在,则提示“该户主信息不存在”

 

change.jsp 输入用户姓名,提交后查询用户信息

<%--
  Created by IntelliJ IDEA.
  User: lenovo
  Date: 2020/11/10
  Time: 14:41
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <form action="changeServlet" method="post">
        <table align="center">
            <tr>
                <td>请输入需要修改的用户姓名:</td>
                <td>
                    <input type="text" name="name">
                </td>
            </tr>

            <tr>
                <td>
                    <input type="submit" value="提交">
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

change02.jsp 将查询到的结果输出值页面,并提供修改窗口

<%--
  Created by IntelliJ IDEA.
  User: lenovo
  Date: 2020/11/10
  Time: 15:14
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<table align="center" border="1" cellspacing="0">
    <tr>
        <td>住房类型</td>
        <td>${sessionScope.type}</td>
    </tr>
    <tr>
        <td>本户现住房面积</td>
        <td>${sessionScope.area}</td>
    </tr>
    <tr>
        <td>本户现住房间数</td>
        <td>${sessionScope.roomnumber}</td>
    </tr>
    <tr>
        <td>户主姓名</td>
        <td>${sessionScope.name}</td>
    </tr>
    <tr>
        <td>身份证号码</td>
        <td>${sessionScope.id}</td>
    </tr>
    <tr>
        <td>性别</td>
        <td>${sessionScope.sex}</td>
    </tr>
    <tr>
        <td>民族</td>
        <td>${sessionScope.nation}</td>
    </tr>
    <tr>
        <td>受教育程度</td>
        <td>${sessionScope.education}</td>
    </tr>
</table>
<br>
<h4 align="center" style="text-decoration: cadetblue">请在下面填写修改信息</h4>
<form action="changeServlet02" method="post">
    <table align="center">
        <tr>
            <td>户别:</td>
            <td>
                <input type="radio" name="hubie" value="家庭户">家庭户
                <input type="radio" name="hubie" value="集体户">集体户
            </td>
        </tr>
        <tr>
            <td>住房类型:</td>
            <td>
                <input type="radio" name="type" value="家庭住宅">家庭住宅
                <input type="radio" name="type" value="集体住宅">集体住宅
                <input type="radio" name="type" value="工作地住所">工作地住所
                <input type="radio" name="type" value="其他住宅">其他住宅
                <input type="radio" name="type" value="无住宅">无住宅
            </td>
        </tr>
        <tr>
            <td>本户现住房面积:</td>
            <td><input type="text" name="area" value="${sessionScope.area}">平方米 </td>
        </tr>
        <tr>
            <td>本户现住房间数</td>
            <td><input type="text" name="roomnumber" value="${sessionScope.roomnumber}"> </td>
        </tr>

        <tr>
            <td>户主姓名:</td>
            <td><input type="text" name="name" value="${sessionScope.name}"> </td>
        </tr>

        <tr>
            <td>身份证号码:</td>
            <td><input type="text" name="id" value="${sessionScope.id}"></td>
        </tr>

        <tr>
            <td>性别:</td>
            <td>
                <input type="radio" name="sex" value="man">男
                <input type="radio" name="sex" value="woman">女
            </td>
        </tr>

        <tr>
            <td>民族:</td>
            <td><input type="text" name="nation" value="${sessionScope.nation}"> </td>
        </tr>

        <tr>
            <td>受教育程度:</td>
            <td>
                <select name="education">
                    <option value="研究生">研究生</option>
                    <option value="大学本科">大学本科</option>
                    <option value="大学专科">大学专科</option>
                    <option value="高中">高中</option>
                    <option value="初中">初中</option>
                    <option value="小学">小学</option>
                    <option value="未上学">未上学</option>
                </select>
            </td>
        </tr>

        <tr>
            <td colspan="2" align="center">
                <input type="submit" value="修改">
            </td>
        </tr>
    </table>
</form>
</body>
</html>

若修改成功,则返回change_success.jsp页面

<html>
<head>
    <title>Title</title>
</head>
<body>
<h3>更改成功</h3>
<a href="index.jsp">返回首页</a>
</body>
</html>

若修改失败,则返回change_wrong.jsp页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h3>更改失败</h3>
<a href="index.jsp">返回首页</a>
</body>
</html>

其中所需要的两个Servlet代码如下:

1.ChangeServlet

package com.gazikel.servlet;

import com.gazikel.dao.CensusDaoImpl;
import com.gazikel.pojo.Census;

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 java.io.IOException;

public class ChangeServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*req.setCharacterEncoding("UTF-8");
        String username = req.getParameter("username");
        CensusDaoImpl c = new CensusDaoImpl();
        Census census = c.queryForAllByName(username);
        HttpSession session = req.getSession();
        session.setAttribute("type", census.getType());
        session.setAttribute("area", census.getArea());
        session.setAttribute("roomnumber", census.getRoomNumber());
        session.setAttribute("name", census.getName());
        session.setAttribute("id", census.getId());
        session.setAttribute("sex", census.getSex());
        session.setAttribute("nation", census.getNation());
        session.setAttribute("eduction", census.getEducation());
        req.getRequestDispatcher("change02.jsp").forward(req, resp);*/
        req.setCharacterEncoding("UTF-8");
        String username = req.getParameter("name");
        //System.out.println(username);
        CensusDaoImpl c = new CensusDaoImpl();
        HttpSession session = req.getSession();
        Census census = c.queryForAllByName(username);
        //System.out.println(census);
        session.setAttribute("type", census.getType());
        session.setAttribute("area", census.getArea());
        session.setAttribute("roomnumber", census.getRoomnumber());
        session.setAttribute("name", census.getName());
        session.setAttribute("id", census.getId());
        session.setAttribute("sex", census.getSex());
        session.setAttribute("nation", census.getNation());
        session.setAttribute("eduction", census.getEducation());
        /*req.setAttribute("type", census.getType());
        req.setAttribute("area", census.getArea());
        req.setAttribute("roomnumber", census.getRoomNumber());
        req.setAttribute("name", census.getName());
        req.setAttribute("id", census.getId());
        req.setAttribute("sex", census.getSex());
        req.setAttribute("nation", census.getNation());
        req.setAttribute("eduction", census.getEducation());*/
        //System.out.println(census.getType()+census.getEducation());
        req.getRequestDispatcher("change02.jsp").forward(req, resp);
    }
}

 

2.ChangeServlet02

package com.gazikel.servlet;

import com.gazikel.dao.CensusDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class ChangeServlet02 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("UTF-8");
        String type = req.getParameter("type");
        String area = req.getParameter("area");
        String roomnumber = req.getParameter("roomnumber");
        String name = req.getParameter("name");
        String id = req.getParameter("id");
        String sex = req.getParameter("sex");
        String nation = req.getParameter("nation");
        String education = req.getParameter("education");
        CensusDaoImpl c = new CensusDaoImpl();
        int i = c.changeByName(name, type, area, roomnumber, id, sex, nation, education);
        if (i >0){
            req.getRequestDispatcher("change_success.jsp").forward(req, resp);
        } else{
            req.getRequestDispatcher("change_wrong.jsp").forward(req,resp);
        }
    }
}

 

4.删除人口信息页面

录入户主姓名,显示详细信息后,点击“删除”按钮,弹出提示框,提示“是否确认删除该互补信息”,确认后删除该信息

delete.jsp

<%--
  Created by IntelliJ IDEA.
  User: lenovo
  Date: 2020/11/10
  Time: 14:38
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>删除人口信息页面</title>
</head>
<body>
    <form action="deleteServlet02" method="post">
        <table align="center">
            <tr>
                <td>
                    请输入需要删除户主姓名:
                </td>
                <td>
                    <input type="text" name="username">
                </td>
            </tr>
            <tr>
                <td>
                    <input type="submit" value="查询" >
                </td>
            </tr>
        </table>
    </form>

</body>
</html>

delete02.jsp显示详细信息

<%--
  Created by IntelliJ IDEA.
  User: lenovo
  Date: 2020/11/10
  Time: 15:05
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <script type="text/javascript">
        function fun() {
            alert("是否确认删除?");
        }
    </script>
</head>
<body>
    住房类型:${sessionScope.type}<br>
    本户现住房面积:${sessionScope.area}<br>
    本户现住房间数:${sessionScope.roomnumber}<br>
    户主姓名:${sessionScope.name}<br>
    身份证号码:${sessionScope.id}<br>
    性别:${sessionScope.sex}<br>
    民族:${sessionScope.nation}<br>
    受教育程度:${sessionScope.education}<br>
  <form action="deleteServlet" method="post">
    <table>
        <tr>
            <td>
                <input type="hidden" value="${requestScope.name}" >
            </td>
        </tr>
        <tr >
            <td colspan="2" ><input type="submit" value="删除" onclick="fun()"> </td>
        </tr>
    </table>
</form>
</body>
</html>

若删除成功,则返回delete_success.jsp

<%--
  Created by IntelliJ IDEA.
  User: lenovo
  Date: 2020/11/10
  Time: 15:09
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <h3>删除成功</h3>
    <a href="index.jsp">返回首页</a>
</body>
</html>

若删除失败,则返回delete_wrong.jsp

<%--
  Created by IntelliJ IDEA.
  User: lenovo
  Date: 2020/11/10
  Time: 14:56
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <h3>删除失败</h3><br>
    <h2 style="color: red">可能用户名不存在哦</h2>
    <a href="index.jsp">返回首页</a>
</body>
</html>

 

其中的两个Servlet如下:

1.DeleteServlet

package com.gazikel.servlet;

import com.gazikel.dao.CensusDaoImpl;
import com.gazikel.pojo.Census;

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 java.io.IOException;

public class DeleteServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        HttpSession session = req.getSession();
        String username = (String) session.getAttribute("name");
        System.out.println(username);
        CensusDaoImpl s = new CensusDaoImpl();
        int i = s.deleteByName(username);
        if (i > 0){
            req.getRequestDispatcher("delete_success.jsp").forward(req,resp);
        } else{
            req.getRequestDispatcher("delete_wrong.jsp").forward(req, resp);
        }
    }
}

2.DeleteServlet02

package com.gazikel.servlet;

import com.gazikel.dao.CensusDaoImpl;
import com.gazikel.pojo.Census;

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 java.io.IOException;

public class DeleteServlet02 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String username = req.getParameter("username");
        System.out.println(username);
        CensusDaoImpl c = new CensusDaoImpl();
        HttpSession session = req.getSession();
        Census census = c.queryForAllByName(username);
        System.out.println(census);
        session.setAttribute("type", census.getType());
        session.setAttribute("area", census.getArea());
        session.setAttribute("roomnumber", census.getRoomnumber());
        session.setAttribute("name", census.getName());
        session.setAttribute("id", census.getId());
        session.setAttribute("sex", census.getSex());
        session.setAttribute("nation", census.getNation());
        session.setAttribute("eduction", census.getEducation());
        req.getRequestDispatcher("delete02.jsp").forward(req, resp);
    }
}

 

 

增删改的操作其实都是差不多的。

明天在更查询的操作,有一点小难度。

还有今天忘了写出来数据库的连接操作,以及最基本的增删改查操作。以后会补充出来的。

标签:census,小型,系统,req,setAttribute,session,import,人口普查,servlet
来源: https://www.cnblogs.com/Gazikel/p/13982103.html