zoukankan      html  css  js  c++  java
  • MVC案例——注册用户

    页面使用重定向可以避免表单的重复提交问题

    如果页面中需要使用域对象中的属性,必须使用转发。此外都使用重定向。

    逻辑关系:

    点击超链——>add.jsp(接收meg信息,接收参数的值)——>add.do(判断用户名是否存在)——存在(放入meg信息到request域中)——>add.jsp

                                           ——不存在(重定向)——>success.jsp

    步骤:

    1.在query.jsp中点击新建用户的超链接,跳转页面为当前目录的add.jsp

    2.add.jsp内容

    <body>
    <%
        Object meg = request.getAttribute("message");
        if (meg!=null){
            %>
            <br>
            <b style="color: red"><%=meg%></b>
    <br><br>
    <%
        }
    %>
    
    <form action="add.do" method="post">
        <table>
            <tr>
                <td>CustomerName:</td>
                <td><input type="text" name="name"  value="<%=request.getParameter("name")==null?"":request.getParameter("name")%>"></td>
            </tr>
            <tr>
                <td>CustomerPassword:</td>
                <td><input type="password" name="password" value="<%=request.getParameter("password")==null?"":request.getParameter("password")%>"></td>
            </tr>
            <tr>
                <td colspan="2"><input type="submit" value="添加" ></td>
            </tr>
        </table>
    </form>
    </body>
    

      

    3.add.do

      private void add(HttpServletRequest req, HttpServletResponse resp) {
            //1.获取表单参数:name,address,phone
            String name = req.getParameter("name");
            String password = req.getParameter("password");
    
            //2.检验name是否存在:
            //2.1调用CustomerDAO的getCountWithName(String name)获取name在数据库中是否存在
            long count  = customerDAO.getCountWithName(name);
    
            //2.2若返回值大于0,则响应add.jsp页面:
            //通过转发的方式来响应add.jsp
            if (count>0){
                //2.2.1要求在add.jsp页面显示一个错误信息:用户名name已经被占用,请重新选择
                req.setAttribute("message","用户名"+name+"已经被占用,请重新选择!");
                //2.2.2add.jsp的表单值可以回显
                //通过value="<%= request.getParameter("name")==null?"" : request.getParameter("name")%>"
                try {
                    req.getRequestDispatcher("add.jsp").forward(req,resp);
                } catch (ServletException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return;
            }
    
            Customer customer = new Customer(name,password);
            customerDAO.save(customer);
            try {
                resp.sendRedirect("success.jsp");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    

      

    4.success.jsp

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
        <b>ADD SUCCESS</b>
        <br><br>
        <a href="query.jsp">return</a>
    </body>
    </html>
    

      

    效果图:

  • 相关阅读:
    Docker用途 & 和tomcat的区别
    Ubuntu安装Redis
    Ubuntu查看和设置Root账户
    Oracle常用语句
    Redis知识总结
    Blazor学习笔记01: 使用BootstrapBlazor组件 创建一个具有单表维护功能的表格页面
    NET Core之积沙成塔01: 解决Visual Studio 2019 代码提示为英文
    MySQL系统自带的数据库information schema
    Windows安装mysql方法
    数据库之概念
  • 原文地址:https://www.cnblogs.com/yangHS/p/11137993.html
Copyright © 2011-2022 走看看