zoukankan      html  css  js  c++  java
  • 初学java 学生管理系统——v04版本 改用web

    接上次的版本,把上次的修改成为基于web的项目.
    总算是可以脱离命令行,使用jsp作为前端,能有一个友好的页面了.

    上次既然已经开始用三层架构的模式了,这次也就接着使用三层架构,把之前的拿过来继续改改用.

    首先核心要修改的就是controller层,因为要改成web项目,controller层负责的就是用户交互,收集用户输入的信息,展示信息,而这些东西都要交给servlet和jsp完成.

    又因为不适合在jsp中编写复杂逻辑,所以就直接用servlet来充当controller层.

    service层和dao层暂时先不用修改.瞬间体会到了三层架构的优势,每层都各司其职,修改只需要修改相关的层,不相关的完全可以不用修改.

    既然用了web,就单纯的想给这个系统添加一个用户登录的功能,以此练习一下用户登录相关的功能.

    index.jsp就简简单单的给个超链接指向登录页面login.jsp

    <a href="${pageContext.request.contextPath}/login.jsp">去登录</a>
    

    其中通过EL表达式${pageContext.request.contextPath}动态获取虚拟路径,之后拼接login.jsp的路径即可跳转过去.

    在login.jsp中也就一个简单的表单,用来做登录信息的提交.

    <form action="${pageContext.request.contextPath}/LoginServlet" method="post">
        <div>
            <label for="username">用户名</label>
            <input type="text" name="username" id="username"/>
        </div>
        <div>
            <label for="password">密码</label>
            <input type="password" name="password" id="password"/>
        </div>
        <div>
            <input type="submit" value="提交">
        </div>
    </form>
    

    表单肯定是直接提交到servlet中的,所以action直接指向LoginServlet,当然这个路径也通过EL表达式拼接绝对路径.

    LoginServlet需要继承HttpServlet.并且重写doPost和doGet方法.Tomcat会在用户访问的时候,自动调用HttpServlet中的service
    方法的,而在HttpServlet的service方法中,又根据请求的信息,分别调用doPost和doGet方法.

    在LoginServlet中,主要要做的事便是

    1. 接收请求数据
    2. 打包数据
    3. 扔给service层处理业务
    4. 接收service层的返回结果
    5. 根据结果判断应该怎么做
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
        // 接收请求的数据
        Map<String, String[]> map = req.getParameterMap();
        // 使用BeanUtils封装数据
        User user = new User();
        try {
            BeanUtils.populate(user, map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 将对象交给service并处理业务
        UserService service = new UserService();
        User loginUser = service.login(user);
        if (loginUser != null) {
            // 登录成功 把user的信息在会话共享
            req.getSession().setAttribute("user", loginUser);
            // 重定向到首页
            resp.sendRedirect(req.getContextPath()+"/index.jsp");
        }else{
            // 登录失败 转发并返回提示信息
            req.setAttribute("msg", "用户名或密码错误");
            req.getRequestDispatcher("/login.jsp").forward(req,resp);
        }
    }
    

    既然要返回失败信息,那么就给login.jsp页面添加一个div用来展示返回的信息.

    <%-- 登录错误提示信息显示 --%>
    <div style="color: red;font-weight: bold">
        ${requestScope.msg}
    </div>
    

    这里使用EL表达式,获取请求域中的msg属性,因为在Servlet中,把错误消息就塞在msg属性中.

    既然有了登录功能,那就把index.jsp也修改一下,增加一个登录后的页面变化.

    <%--未登录则需要登录,登陆后看到功能页面--%>
    <c:if test="${empty sessionScope.user}">
        <a href="${pageContext.request.contextPath}/login.jsp">去登录</a><br>
    </c:if>
    <c:if test="${not empty sessionScope.user}">
        <div>欢迎&nbsp;${sessionScope.user.username}</div>
        <div>功能列表</div>
        <div>
            <a href="${pageContext.request.contextPath}/ShowAllStudentServlet">查看学生</a>
        </div>
        <div>
            <a href="${pageContext.request.contextPath}/ExitServlet">退出登录</a>
        </div>
    </c:if>
    

    这里登录页面的变化使用了jstl的if标签,当test属性值为true时,显示if标签内的元素.

    至此,用户登录的controller层基本上搞定了.service,dao这两层和学生管理的部分差不多,就略掉不写在这了.其中的User类也就仅仅包含id,username,password三个属性.

    后续待更,学生管理部分的Servlet有点多,慢慢更吧.

  • 相关阅读:
    批处理集锦——(5)使用dir查找文件
    批处理集锦——(4)2>nul和1>nul是什么意思?
    python3循环遍历嵌套字典替换指定值
    selenium对浏览器自动截图
    linux 安装mysql8以及远程连接步骤(图文并茂)
    Allure 自动化测试报告使用详解
    allure安装教程以及遇到的坑
    pytest接口自动化快速设置接口全局host
    pytest报错警告处理一:DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
    python3.x中 pytest之fixture
  • 原文地址:https://www.cnblogs.com/yao-xi/p/14007591.html
Copyright © 2011-2022 走看看