抬头仰望星空,是否能发现自己的渺小。

伪斜杠青年

人们总是混淆了欲望和理想

JAVA WEB MVC设计模式之封装实例 – 9.23

折腾挺久的,先上目录结构文件结构,代码不全上,只贴部分

听别人说这是MVC设计模式中的内容,具体没怎么去查,刚刚接触规范化难免会觉得很麻烦,但是熟悉后,你会感觉到这种模式的好处

培哥的源代码写了一个登录类,所以我才可能比较快的上手

然后我放上Regist类吧,在培哥的登录基础上模仿的,加入了Session验证机制,注册后的每一步操作都需要验证是否已经登录。避免有人绕过登录直接删除或者更改系统数据

package com.lckiss.action;

import java.sql.ResultSet;
import java.util.List;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.lckiss.service.UserService;

public class UserRegist extends HttpServlet {

    private static final long serialVersionUID = 1L;

    /**
     * @author 14436242
     */    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) {
        String name = req.getParameter("name");
        String pwd = req.getParameter("pwd");

        // 内部输出一次看从网页获取的数据是否错误
        System.out.println(name);
        System.out.println(pwd);

        try {
            // 业务逻辑
            UserService us = new UserService();
            List users = us.QueryAll();
            String SE = "SELECT SEQ.NEXTVAL FROM DUAL";
            ResultSet NextV = us.ExecuteQuerry(SE);
            int V = 0;
            while (NextV.next()) {
                V = NextV.getInt("NEXTVAL");
            }

            String sql = "insert into emp (EMPNO,ename,job) values(SEQ.NEXTVAL,'" + name + "','" + pwd + "')";
            int rs = us.ExecuteUpdate(sql);

            if (rs >= 1) {
                req.setAttribute("NextV", V - 1);

                // 将注册的帐号密码存入Session中,方便后面验证是否已登录
                HttpSession session = req.getSession();
                session.setAttribute("name", name);
                session.setAttribute("pwd", pwd);
            }

            // 返回响应
            req.setAttribute("users", users);
            req.setAttribute("flag", true);
            req.getRequestDispatcher("view").forward(req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

接下来是删除操作类

package com.lckiss.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lckiss.service.UserService;

public class DeletSQL extends HttpServlet {

    private static final long serialVersionUID = 1L;

    /**
     * @author 14436242
     * @throws IOException
     * @throws ServletException
     */
    @Override
    public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String empno = req.getParameter("empno");

        // 拿到登录时Session中的帐号密码验证是否已登录
        String name = (String) req.getSession().getAttribute("name");
        String pwd = (String) req.getSession().getAttribute("pwd");
        boolean flag;
        try {
            // 业务逻辑
            UserService us = new UserService();
            // 验证帐号合法性
            flag = us.loginByname(name, pwd);
            if (us.UserID(empno)) {

                String sql = "delete from emp where EMPNO=" + empno + "";
                int rs = us.ExecuteUpdate(sql);
                if (rs >= 1) {
                    req.setAttribute("DeleV", empno);
                }
                List users = us.QueryAll();
                // 返回响应
                req.setAttribute("users", users);
                req.setAttribute("flag", flag);
                req.getRequestDispatcher("view").forward(req, resp);
            } else {

                req.setAttribute("flag", false);
                req.getRequestDispatcher("view").forward(req, resp);
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

还有一个更新类

package com.lckiss.action;

import java.util.List;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.lckiss.service.UserService;

public class UpdateSQL extends HttpServlet {

    /**
     * @author 14436242
     */    private static final long serialVersionUID = 1L;

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) {
        try {
            // 设置格式,防止乱码 必须在流前定义
            resp.setContentType("text/html;charset=GBK");
            // 从传输过来的链接中获取empno值
            String empno = req.getParameter("empno");
            String id = req.getParameter("id");
            // 测试if条件
            System.out.println(id);

            // 拿到登录时Session中的帐号密码验证是否已登录
            String name = (String) req.getSession().getAttribute("name");
            String pwd = (String) req.getSession().getAttribute("pwd");

            UserService us = new UserService();
            // 验证帐号合法性
            boolean flag = us.loginByname(name, pwd);
            // 检查更改元素是否存在
            boolean rs = us.UserID(empno);

            if (rs && "0".equals(id)) {
                HttpSession session = req.getSession();
                // 将empno存储到session中
                session.setAttribute("no", empno);
                // 定向到更新页面,返回数据ID给JSP页面
                req.setAttribute("ID", empno);
                req.getRequestDispatcher("./index.jsp").forward(req, resp);
                // 内部打印,方便检测是否出错
                System.out.println(empno);
            } else if (id == null) {
                // 从session中拿到上次的no
                String pno = (String) req.getSession().getAttribute("no");
                String rename = req.getParameter("name");
                String repwd = req.getParameter("pwd");
                System.out.println(pno + "-" + rename + "-" + repwd);
                // 传入相关信息 准备更新
                String sql = "update emp set ename='" + rename + "',job='" + repwd + "' where EMPNO=" + pno + "";
                int res = us.ExecuteUpdate(sql);
                if (res > 0) {
                    req.setAttribute("UpdateV", pno);
                }
                System.out.println(res);
                List users = us.QueryAll();
                req.setAttribute("users", users);
                req.setAttribute("flag", flag);
                req.getRequestDispatcher("view").forward(req, resp);
            } else {
                req.setAttribute("flag", false);
                req.getRequestDispatcher("view").forward(req, resp);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

以上都有注释,以前的操作可以弄懂的看这个应该是不费力的,Dao Util我做了一定的修改,有想研究的下这个包吧

不再做解释,因为前面不懂后面也不好讲了,有问题直接问我吧。

关于后面的View类,request中的3个参数,用于分别判断3个操作,操作成功后分别显示操作数据到页面上,主要是保证一个empno的可见性

方便知道修改了谁,删除了谁,更改了谁。

package com.lckiss.action;

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

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lckiss.entity.User;

/**
 * @author 14436242
 *
 */public class View extends HttpServlet {

    private static final long serialVersionUID = 1L;

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 1.设置应答类型
        response.setContentType("text/html;charset=utf-8");
        // 2.获得输出流
        PrintWriter out = response.getWriter();
        // 3.接收业务逻辑的返回结果
        Boolean flag = (Boolean) request.getAttribute("flag");
        List users = (List) request.getAttribute("users");
        Object NextV = request.getAttribute("NextV");
        Object DeleV = request.getAttribute("DeleV");
        Object UpdateV = request.getAttribute("UpdateV");

        System.out.println(flag);

        if (flag) {
            // 3.查询所有信息
            // 查询数据库表中所有数据
            if (NextV != null) {
                out.println("<font color='red'>数据" + NextV + "已插入成功</font>");
            }

            if (DeleV != null) {
                out.println("<font color='red'>数据" + DeleV + "已删除成功</font>");
            }
            if (UpdateV != null) {
                out.println("<font color='red'>数据" + UpdateV + "已修改成功</font>");
            }

            out.println("<p>编号 , 姓名 , 工作 , HIREDATA , SAL , COMM , DEPTNO.</p>");

            for (Object obj : users) {
                User user = (User) obj;
                String l = "" + user.getEmpno() + "," + user.getEname() + "," + user.getJob() + "," + user.getHiredata()
                        + "," + user.getMgr() + "," + user.getComm() + "," + user.getDeptno() + "";
                String del = "http://localhost/day_1/del?empno=" + user.getEmpno() + "";
                String up = "http://localhost/day_1/update?empno=" + user.getEmpno() + "&id=0";
                out.println("<p>" + l + "-" + "<a href='" + del + "'>删除</a>" + "-" + "<a href='" + up + "'>更新</a>"
                        + "</p>");
            }
            out.println("---------<a href='http://localhost/day_1/main.html'>点击返回主页</a>---------");
        } else {
            out.println("<h4><font color='red'>操作失败,请检查成员是否存在并重试或联系管理员</font></h4>");
        }
    }
}

随便上张图

差不多了,自行领悟了,我看懂就行了,一个个的函数解释,还是百度实在。

 抄袭无益  Anr-C 2016.10.16


0条评论

发表评论