美文网首页
jsp+servlet+jdbc对数据库的增删改查

jsp+servlet+jdbc对数据库的增删改查

作者: june_fbdd | 来源:发表于2017-06-02 18:26 被阅读0次

    项目结构:

    image.png

    jsp(最后正确的)写法:

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
        
    <%  
        String path = request.getContextPath();  
    %>  
    <%-- <%=path %> --%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>数据库增删改查</title>
    <script type="text/javascript">  
        function dosubmit() {  
            var th = document.form1;  
            if (th.id.value == "") {  
                alert("用户名不能为空");  
                th.id.focus();  
                return;  
            }  
      
            if (th.username.value == "") {  
                alert("姓名 不能为空");  
                th.username.focus();  
                return;  
            }  
      
            if (th.age.value == "") {  
                alert("密码不能为空");  
                th.age.focus();  
                return;  
            }  
            th.action="<%=path%>/xxx/Action"  
            th.submit();  
      
        }  
    </script>  
    </head>
    <body>
      <form name="form1" method="post" >
      ID: <input type="text" name="id"/>
            姓名:<input type="text" name="username"/>
            年龄:<input type="text" name="age"/>        
          <!-- <input type="submit" value="确定"/>
       -->
      
      <!--  <a href="JavaScript:dosubmit();">
       <input type="button" value="确定"/>
       </a>
     -->
     
     <input type="button" value="确定" onclick="javascript:dosubmit();">
     
      </form>
    </body>
    </html>
    

    数据从jsp页面输入,点击“确定”后触发dosubmit()提交输入信息到Action.java。

    Action.java片段:

    public void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            String path = request.getContextPath();
            request.setCharacterEncoding("utf-8");
            response.setContentType("text/html; charset=utf-8");
            PrintWriter out = response.getWriter();
            String id = request.getParameter("id");
            String username = request.getParameter("username");
            String age = request.getParameter("age");
            System.out.println("id = " + id + "  username = " + username + "  age = " + age);
            List<Object> params = new ArrayList<Object>();
            params.add(id);
            params.add(username);
            params.add(age);
            boolean flag = service.registerUser(params);
            if(flag){
                out.println("注册成功");
                response.sendRedirect(path + "/success.jsp");
            }else{
                out.println("注册失败");
                response.sendRedirect(path + "/fail.jsp");
    
            }
            out.flush();
            out.close();
        }
    

    通过request.getParameter获取信息,然后通过Service接口调用Dao,通过jdbc向数据库里存数据。

    遇到的问题:

    最开始1.jsp里的确定按钮是这样写的:

    <a href="JavaScript:dosubmit();">
       <input type="submit" value="确定"/>
       </a>
    

    Action.java里加断点,然后params参数里直接传数据,可以保存成功。但从1.jsp里输入的时候就保存不成功,并且没有走到doPost方法里。证明从servlet-->Dao-->jdbc是通的,但jsp-->servlet不通。所以浏览器进入F12模式检查post的数据和Request URL是否正确。检查结果post数据没有问题,但Request URL是localhost:8080/Data。jsp里的路径写的是<%=path%>/xxx/Action,和web.xml里url pattern一致,证明jsp里并没有触发dosubmit()里的th.action,锁定问题范围:检查确定按钮和dosubmit()有没有绑定。目测<a href="JavaScript:dosubmit();">标签和<input>标签是绑定不了的?所以改成了:

     <input type="button" value="确定" onclick="javascript:dosubmit();">
    

    最后测试成功。

    假如1.jsp写成这样:

     <form name="form1" method="post" action="<%=path%>/xxx/Action">
      ID: <input type="text" name="id"/>
            姓名:<input type="text" name="username"/>
            年龄:<input type="text" name="age"/>        
          <!-- <input type="submit" value="确定"/>
       -->
      
       <input type="submit" value="确定"/>
     
     <!-- <input type="button" value="确定" onclick="javascript:dosubmit();"> -->
     
      </form>
    

    那么信息可以存入数据库,但不是经过dosubmit(),所以姓名或年龄可以为空。

    相关文章

      网友评论

          本文标题:jsp+servlet+jdbc对数据库的增删改查

          本文链接:https://www.haomeiwen.com/subject/bciufxtx.html