0
点赞
收藏
分享

微信扫一扫

基于javaweb+jsp的宠物店信息管理系统

书呆鱼 2022-03-30 阅读 51

JavaWeb JavaBean JSP MVC MySQL Tomcat JavaScript Bootstrap.

基础JSP+Servlet或JSP+SSM(Spring、SpringMVC、MyBatis)框架或JSP+SSM+Maven(pom.xml)框架或SpringBoot…均可

开发工具:eclipse/idea/myeclipse/sts等均可配置运行

适用

课程设计,大作业,毕业设计,项目练习,学习演示等

    <br>
    <div class="index-content-operation">
        <button class="btn btn-pill btn-line btn-info btn-sm" <c:if test="${loginUser.userType != '管理员'}">disabled="disabled" title="没有权限!!!"</c:if> οnclick="window.location.href='pet_add.jsp'">添加</button>
        <div class="index-content-operation-search"><input id="search_keyword" placeholder="昵称" type="text" name="search_keyword"/><input type="hidden" id="searchColumn" name="searchColumn" value="pet_name"/><button class="btn btn-pill btn-line btn-warning btn-sm" onclick="searchList()">搜索</button></div>
    </div>
    <br>
    <table class="table table-striped table-hover table-bordered">
        <thead>
        <tr class="index-content-table-th">
            <th>昵称</th>
            <th>类型</th>
            <th>性别</th>
            <th>年龄</th>
            <th>主人</th>
            <th>主人电话</th>
            <th>备注</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        <c:forEach items="${list}" var="vo">
            <tr class="index-content-table-td">
                vo.setUserSex(rs.getString("user_sex"));
                vo.setUserPhone(rs.getString("user_phone"));
                vo.setUserText(rs.getString("user_text"));
                vo.setUserType(rs.getString("user_type"));
            }
            c.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return vo;
    }

    //@Override
    public Map<String, Object> list(Map<String, Object> params) {
            </tr>
            <tr>
                <td width="12%">性别:</td>
                <td>
                        <input name="userSex" type="radio" value="" checked="checked"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        <input name="userSex" type="radio" value=""/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                </td>
            </tr>
            <tr>
                <td width="12%">手机:</td><td><input class="index-content-table-td-add" type="text" id="userPhone" name="userPhone" value=""/></td>
            </tr>
            <tr>
                    <td width="12%">备注:</td><td><textarea id="userText" name="userText" style="width: 60%; height: 100px;padding: 0px 17px;" placeholder="请输入内容......"></textarea></td>
            </tr>
            <tr>
                <td width="12%">类型:</td>
                <td>
                        <input name="userType" type="radio" value="管理员" checked="checked"/>&nbsp;&nbsp;&nbsp;管理员&nbsp;&nbsp;&nbsp;&nbsp;
                        <input name="userType" type="radio" value="普通用户"/>&nbsp;&nbsp;&nbsp;普通用户&nbsp;&nbsp;&nbsp;&nbsp;
                </td>
            </tr>
        </table>
            <th>类型</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        <c:forEach items="${list}" var="vo">
            <tr class="index-content-table-td">
                <td>${vo.username}</td>
                <td>${vo.realName}</td>
                <td>${vo.userSex}</td>
                <td>${vo.userPhone}</td>
                <td title="${vo.userText}">
                <c:choose>
                    <c:when test="${fn:length(vo.userText) > 19}">
                        <c:out value="${fn:substring(vo.userText, 0, 19)}..."/>
                    </c:when>
                    <c:otherwise>
                        <c:out value="${vo.userText}"/>
                    </c:otherwise>
                </c:choose>
                </td>
                <td>${vo.userType}</td>
                <td>
                    <button class="btn btn-pill btn-line btn-default btn-sm" style="padding: 0px 1px;" onclick="window.location.href='UserServlet?action=get&id=${vo.id}'">详情</button>&nbsp;
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //过滤编码
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String action = Util.decode(request, "action");
        if ("add".equals(action)) {//增加
            Pet vo = new Pet();
            //取出页面传进来的各个数据,并设置到Pet对象的属性里
            vo.setPetName(Util.decode(request, "petName"));
            vo.setPetType(Util.decode(request, "petType"));
            vo.setPetSex(Util.decode(request, "petSex"));
            vo.setPetAge(Util.decode(request, "petAge"));
            vo.setPetMaster(Util.decode(request, "petMaster"));
            vo.setPetMasterPhone(Util.decode(request, "petMasterPhone"));
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户模块的Servlet控制层,负责接收页面传过来的请求参数,根据action参数的值来确定页面要执行的具体操作<br>
 * 而后再调用UserService业务层的方法来处理具体的业务,最后将处理完成的结果返回或跳转至相应页面
 */
//@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {

    /**
     * 处理Post请求
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
            userService.add(vo);
            request.getSession().setAttribute("alert_msg", "注册成功!用户名:[" + username + "]");
            request.getRequestDispatcher("login.jsp").forward(request, response);
        } else if ("logout".equalsIgnoreCase(action)) {//登出
            HttpSession session = request.getSession();
            User user = (User) session.getAttribute("loginUser");
            if (user != null) {
                session.removeAttribute("loginUser");
            }
            response.sendRedirect("login.jsp");
        } else if ("validationCode".equalsIgnoreCase(action)) {
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    // 返回一个随机颜色(Color对象)
    private Color getRandomColor(int minColor, int maxColor) {
        Random random = new Random();
        // 保存minColor最大不会超过255
        if (minColor > 255)
            minColor = 255;
        //  保存minColor最大不会超过255
        if (maxColor > 255)
            maxColor = 255;
        //  获得红色的随机颜色值
        int red = minColor + random.nextInt(maxColor - minColor);
        //  获得绿色的随机颜色值
        int green = minColor + random.nextInt(maxColor - minColor);
        //  获得蓝色的随机颜色值
        int blue = minColor + random.nextInt(maxColor - minColor);
        return new Color(red, green, blue);
    }
                <c:choose>
                    <c:when test="${fn:length(vo.petText) > 19}">
                        <c:out value="${fn:substring(vo.petText, 0, 19)}..."/>
                    </c:when>
                    <c:otherwise>
                        <c:out value="${vo.petText}"/>
                    </c:otherwise>
                </c:choose>
                </td>
                <td>
                    <button class="btn btn-pill btn-line btn-default btn-sm" style="padding: 0px 1px;" onclick="window.location.href='PetServlet?action=get&id=${vo.id}'">详情</button>&nbsp;
                    <button class="btn btn-pill btn-line btn-danger btn-sm" style="padding: 0px 1px;"
                            <c:if test="${loginUser.userType != '管理员'}">disabled="disabled" title="没有权限!!!"</c:if>
                    οnclick="window.location.href='PetServlet?action=editPre&id=${vo.id}'">编辑</button>&nbsp;
                    <button class="btn btn-pill btn-line btn-success btn-sm" style="padding: 0px 1px;" <c:if test="${loginUser.userType != '管理员'}">disabled="disabled" title="没有权限!!!"</c:if> οnclick="if(window.confirm('将要删除:${vo.petName}?'))window.location.href='PetServlet?action=delete&id=${vo.id}'">删除</button>
                </td>
            </tr>
        </c:forEach>
        </tbody>
    </table>
    <div style="float: right;padding-right: 10px;color: #515151;"><jsp:include page="split.jsp"/></div>
</div>
</body>
            String sql = "delete from `t_notice` where id = " + id;
            s.execute(sql);
            s.close();
            c.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    //@Override
    public Notice get(Serializable id) {
        Notice vo = null;
        try {
            Connection c = Util.getConnection();
            Statement s = c.createStatement();
            String sql = "select * from `t_notice` where id = " + id;
            ResultSet rs = s.executeQuery(sql);
            PreparedStatement ps = c.prepareStatement(sql);
            
            ps.setString(1, vo.getNoticeName());
            ps.setString(2, vo.getNoticeText());
            ps.setString(3, vo.getNoticeType());
            ps.setString(4, vo.getCreateDate());
            ps.setLong(5, vo.getId());
            ps.execute();
            ps.close();
            c.close();
        } catch (Exception e) {
            e.printStackTrace();
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 公告模块的DAO层(数据层)的具体实现类,对NoticeDAO接口中定义的增删改查等抽象方法作出具体的功能实现
 */
public class NoticeDAOImpl implements NoticeDAO {

    //@Override
    public void add(Notice vo) {
        String sql = "insert into `t_notice` (`notice_name`,`notice_text`,`notice_type`,`create_date`) values(?,?,?,?)";
        try {
            Connection c = Util.getConnection();
            PreparedStatement ps = c.prepareStatement(sql);
            
            ps.setString(1, vo.getNoticeName());
            ps.setString(2, vo.getNoticeText());
        try {
            if (isMessyCode(str)) {
                str = new String(str.getBytes("ISO-8859-1"), "UTF-8");
            }
            if (isMessyCode(str)) {
                str = new String(str.getBytes("GB2312"), "UTF-8");
            }
            if (isMessyCode(str)) {
                str = new String(str.getBytes("GBK"), "UTF-8");
            }
            if (isMessyCode(str)) {
                str = new String(str.getBytes("UTF-8"), "ISO-8859-1");
     * @throws ServletException
     * @throws IOException
     */
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    // 返回一个随机颜色(Color对象)
    private Color getRandomColor(int minColor, int maxColor) {
<html>
<head>
    <meta charset="utf-8"/>
    <title>修改宠物</title>
    <link rel="stylesheet" type="text/css" href="css/index.css"/>
</head>
<body>
<jsp:include page="menu.jsp"/>
<div class="index-content">
    <div class="index-content-operation">
        <a class="info-detail">修改宠物</a>
        <br>
        <br>
    </div>
    <br>
                    vo.setUserType(rs.getString("user_type"));
                    list.add(vo);
                }
            String sqlCount = "select count(*) from `t_user` where 1=1 " + condition;
            ps = c.prepareStatement(sqlCount);
            rs = ps.executeQuery();
            if (rs.next()) {
                totalCount = rs.getInt(1);
            }
            rs.close();
            ps.close();
            c.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Map<String, Object> result = new HashMap();
        result.put("list", list);
        result.put("totalCount", totalCount);
        return result;
    }
}
package com.demo.dao.impl;
            ps.close();
            c.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //@Override
    public boolean delete(long id) {
        try {
            Connection c = Util.getConnection();
            Statement s = c.createStatement();
        if ("add".equals(action)) {//增加
            Pet vo = new Pet();
            //取出页面传进来的各个数据,并设置到Pet对象的属性里
            vo.setPetName(Util.decode(request, "petName"));
            vo.setPetType(Util.decode(request, "petType"));
            vo.setPetSex(Util.decode(request, "petSex"));
            vo.setPetAge(Util.decode(request, "petAge"));
            vo.setPetMaster(Util.decode(request, "petMaster"));
            vo.setPetMasterPhone(Util.decode(request, "petMasterPhone"));
            vo.setPetText(Util.decode(request, "petText"));
            PetService petService = new PetServiceImpl();
            //调用Service层增加方法(add),增加记录
                <td width="12%">性别:</td>
                <td>
                        <input name="userSex" type="radio" value="" checked="checked"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        <input name="userSex" type="radio" value=""/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                </td>
            </tr>
            <tr>
                <td width="12%">手机:</td><td><input class="index-content-table-td-add" type="text" id="userPhone" name="userPhone" value=""/></td>
            </tr>
            <tr>
                    <td width="12%">备注:</td><td><textarea id="userText" name="userText" style="width: 60%; height: 100px;padding: 0px 17px;" placeholder="请输入内容......"></textarea></td>
            </tr>
            <tr>
                <td width="12%">类型:</td>
                <td>
                        <input name="userType" type="radio" value="管理员" checked="checked"/>&nbsp;&nbsp;&nbsp;管理员&nbsp;&nbsp;&nbsp;&nbsp;
                        <input name="userType" type="radio" value="普通用户"/>&nbsp;&nbsp;&nbsp;普通用户&nbsp;&nbsp;&nbsp;&nbsp;
                </td>
import java.util.Random;

//@WebServlet("/AuthServlet")
public class AuthServlet extends HttpServlet {
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //过滤编码
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String action = Util.decode(request, "action");
        if ("login".equalsIgnoreCase(action)) {//登录
            String username = Util.decode(request, "username");
            String password = Util.decode(request, "password");
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        //移除错误提示
        session.removeAttribute("alert_msg");
        //登录拦截
        String uri = request.getRequestURI();
        String action = request.getParameter("action");
        if (uri.endsWith("login.jsp") || uri.endsWith("register.jsp") || "register".equalsIgnoreCase(action) || "validationCode".equalsIgnoreCase(action) || "login".equalsIgnoreCase(action) || uri.endsWith(".css") || uri.endsWith(".js") || uri.endsWith(".png") || uri.endsWith(".jpg")) {
            chain.doFilter(request, response);
            return;
        } else if (session.getAttribute("loginUser") == null) {
            session.setAttribute("alert_msg", "错误:请先登录!");
            response.sendRedirect("login.jsp");
            return;
        }
        chain.doFilter(request, response);
    }

            g.setColor(getRandomColor(180, 250));// 随机设置要填充的颜色
            g.fillRect(0, 0, width, height);//  填充图形背景
            //  设置初始字体
            g.setFont(new Font("Times New Roman", Font.ITALIC, height));
            g.setColor(getRandomColor(120, 180));// 随机设置字体颜色
            //  用于保存最后随机生成的验证码
            StringBuilder validationCode = new StringBuilder();
            //  验证码的随机字体
            String[] fontNames = {"Times New Roman", "Book antiqua", "Arial"};
            for (int i = 0; i < 4; i++) {
                //  随机设置当前验证码的字符的字体
                g.setFont(new Font(fontNames[random.nextInt(3)], Font.ITALIC, height));
                //  随机获得当前验证码的字符
                char codeChar = codeChars.charAt(random.nextInt(charsLength));
                validationCode.append(codeChar);
                //  随机设置当前验证码字符的颜色
                g.setColor(getRandomColor(10, 100));
                //  在图形上输出验证码字符,x和y都是随机生成的
                g.drawString(String.valueOf(codeChar), 16 * i + random.nextInt(7), height - random.nextInt(6));
            }
            HttpSession session = request.getSession();
            session.setMaxInactiveInterval(5 * 60);
            //  将验证码保存在session对象中,key为validation_code
            session.setAttribute("validationCode", validationCode.toString());
     *
     * @return 如果连接成功则返回连接对象,如果连接失败返回null
     */
    public static Connection getConnection() throws Exception {
        Class.forName(DBDRIVER);
        return DriverManager.getConnection(DBURL, DBUSER, PASSWORD);
    }
    /**
     * 测试连接是否成功
     *
     * @param args
            Connection c = Util.getConnection();
            Statement s = c.createStatement();
            String sql = "delete from `t_user` where id = " + id;
            s.execute(sql);
            s.close();
            c.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    //@Override
    public User get(Serializable id) {
        User vo = null;
        try {
            Connection c = Util.getConnection();
            Statement s = c.createStatement();
    private int pageNum;//当前页,从请求那边传过来。
    private int pageSize;//每页显示的数据条数。
    //需要计算得来
    private int totalPage;    //总页数,通过totalRecord和pageSize计算可以得来
    //开始索引,也就是我们在数据库中要从第几行数据开始拿,有了startIndex和pageSize,
    //就知道了limit语句的两个数据,就能获得每页需要显示的数据了
    private int startIndex;
    //分页显示的页数,比如在页面上显示1,2,3,4,5页,start就为1,end就为5,这个也是算过来的
    private int start;
    private int end;

    private String servlet;//查询时要请求的接口
    private String searchColumn;//待模糊查询的列
    private String keyword;//待模糊查询的关键字
                    </c:when>
                    <c:otherwise>
                        <c:out value="${vo.petText}"/>
                    </c:otherwise>
                </c:choose>
                </td>
                <td>
                    <button class="btn btn-pill btn-line btn-default btn-sm" style="padding: 0px 1px;" onclick="window.location.href='PetServlet?action=get&id=${vo.id}'">详情</button>&nbsp;
                    <button class="btn btn-pill btn-line btn-danger btn-sm" style="padding: 0px 1px;"
                            <c:if test="${loginUser.userType != '管理员'}">disabled="disabled" title="没有权限!!!"</c:if>
                    οnclick="window.location.href='PetServlet?action=editPre&id=${vo.id}'">编辑</button>&nbsp;
                    <button class="btn btn-pill btn-line btn-success btn-sm" style="padding: 0px 1px;" <c:if test="${loginUser.userType != '管理员'}">disabled="disabled" title="没有权限!!!"</c:if> οnclick="if(window.confirm('将要删除:${vo.petName}?'))window.location.href='PetServlet?action=delete&id=${vo.id}'">删除</button>
                </td>
            </tr>
        </c:forEach>
        </tbody>
    </table>
    <div style="float: right;padding-right: 10px;color: #515151;"><jsp:include page="split.jsp"/></div>
</div>
</body>

运行环境

Java≥6、Tomcat≥7.0、MySQL≥5.5

开发工具

eclipse/idea/myeclipse/sts等均可配置运行

技术框架

JavaWeb JavaBean JSP MVC MySQL Tomcat JavaScript Bootstrap.

基础JSP+Servlet或JSP+SSM(Spring、SpringMVC、MyBatis)框架或JSP+SSM+Maven(pom.xml)框架或SpringBoot…均可

适用

课程设计,大作业,毕业设计,项目练习,学习演示等

功能说明

登录、注册、退出、用户模块、公告模块、宠物模块的增删改查管理

20220319001431

20220319001432

20220319001433

20220319001434

20220319001435

20220319001436

20220319001437

20220319001438

20220319001439

document

↖[获取源码方式]见左侧

举报

相关推荐

0 条评论